如果已经创建了第二个图,为什么不符合图1的格式?

问题描述 投票:0回答:1

我正在尝试在图形中绘制第二个对象,其中包含使用 pandas 从 .CSV 文件读取的值,并检测该文件包含多少天及其时间,以比较日与日以及每天从开始到结束的时间

self.excel2 = pandas.read_csv(self.Doc2, sep="\t", encoding="utf-16")
self.excel2.columns = ["Hour", "Date", "Value"]

for Hour in range(self.DayEndsValue1[0], self.DayEndsValue1[1]): 
    ValueX = self.excel1.loc[Hour, "Hour"]
    self.Plot1X.append(ValueX)

for Value in range(self.DayEndsValue1[0], self.DayEndsValue1[1]):
    ValueY = self.excel1.loc[Value, "Value"]
    self.Plot1Y.append(ValueY)

将读取的值附加到列表中,然后显示绘图,与第一个绘图的步骤相同。 乍一看如何 同一天 2 块地块 但是当我从下拉菜单中更改日期时,看起来像这样不同的日子,绘图显示两倍的 xtick 值 我想在同一个图上显示这两个图,例如:如果第 26 天从上午 11 点开始并于 23:55:99 结束,第 27 天从上午 0 点开始并于 23:59:99 结束,如何以相同的格式绘制这两个图在同一时间空间,2 个具有不同维度 X 和 Y 的图

已经格式化了,为什么不同时格式化为24小时格式? 我格式化这个是不是做错了什么?

我尝试使用

datetime.strptime
来格式化 X 值并将其发送到新的干净列表,如下所示:

for Hour2 in range(self.DayEndsValue2[Index2], self.DayEndsValue2[Index2 + 1]):
            ValueX2 = self.excel2.loc[Hour2, "Hour"]
            self.Plot2X.append(ValueX2)
            hour = dt.datetime.strptime(ValueX2, '%H:%M:%S')
            self.HoursDay2.append(hour.hour)
        self.returnedList2 = list(set(self.HoursDay2))

还尝试在绘图后使用

AutoDateLocation
set_major_formatter
中的
mdates
但没有任何反应

    self.FiguraGeneral = plt.figure(figsize=(12, 7), clear=True)
        self.FiguraGeneral.clear()

        self.FigureBoth = self.FiguraGeneral.add_subplot(111)
        self.FigureBoth.plot(self.Plot1X, self.Plot1Y, color="b", label="Presion 1")
        self.FigureBoth.scatter(self.Plot2X, self.Plot2Y, color="r", label="Presion 2")

        self.FigureBoth.set_title("PRESIONES")
        self.FigureBoth.set_xlabel("Hora")
        self.FigureBoth.set_ylabel("Presion Ms")

        locator = mdates.AutoDateLocator(minticks=12, maxticks=24)
        plt.gcf().axes[0].xaxis.set_major_locator(locator)
        #plt.gcf().axes[0].xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))
        plt.xticks(rotation=45)
        plt.gcf().autofmt_xdate()

附上最后一个示例:从第 27 天开始读取的小时数 以下是生成此图的代码:Drive 上的脚本

python matplotlib
1个回答
0
投票

根据这个论坛! 格式化日期定位器 XAxis 需要使用 strptime 进行格式化,从 documento excel 中提取值进行绘图。然后绘图已经完成,但没有格式,最后用 2 个无花果做一个 PLot,用于设置最后一步,使用:

  • 设置主要_格式化程序
  • 设置主要_定位器

就像这是我的代码工作和变化天检测从几天到几天,从不同的日子到几个小时!

        for Value in range(self.DayEndsValue1[Index], self.DayEndsValue1[Index + 1]):
        ValueY = self.excel1.loc[Value, "Value"]
        self.Plot1Y.append(ValueY)

    for Hour2 in range(self.DayEndsValue2[Index2], self.DayEndsValue2[Index2 + 1]):
        ValueX = dt.datetime.strptime(self.excel2.loc[Hour2, "Hour"], "%H:%M:%S")
        self.Plot2X.append(ValueX)
        self.HoursDay2.append(ValueX.hour)
    self.returnedList2 = list(set(self.HoursDay2))

    for Value2 in range(self.DayEndsValue2[Index2], self.DayEndsValue2[Index2 + 1]):
        ValueY2 = self.excel2.loc[Value2, "Value"]
        self.Plot2Y.append(ValueY2)


    fig, FigA = self.FiguraGeneral = plt.subplots(figsize=(12, 7), sharex=True)
    FigA.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))
    FigA.xaxis.set_major_locator(mdates.AutoDateLocator(minticks=12, maxticks=30))

    FigA.plot(self.Plot1X, self.Plot1Y, color="b", label="Presion 1")
    FigA.scatter(self.Plot2X, self.Plot2Y, color="r", label="Presion 2")

    plt.xticks(rotation=45)
    plt.gcf().autofmt_xdate()
© www.soinside.com 2019 - 2024. All rights reserved.