Python 在子地块上绘制可变数量的系列和数据预测,包括轴标签、格式和线颜色

Python 在子地块上绘制可变数量的系列和数据预测,包括轴标签、格式和线颜色,python,pandas,matplotlib,plot,Python,Pandas,Matplotlib,Plot,我正在写一个程序,它获取数据,然后使用时间序列预测来预测下一个,比如说,300个数据点的数据值 但是,只有满足特定条件的数据才会被绘制,因此没有为add_subplot()方法定义子图的数量。我知道plot.subplots()函数,但是 fig, (ax1, ax2) = plt.subplots(1, 2) 这意味着两张图肯定会被绘制出来,我需要改变具体的数量,比如一个参数 以下是当前代码的简化版本,导致每个绘图位于单独的窗口中: fig = plt.figure() # creates

我正在写一个程序,它获取数据,然后使用时间序列预测来预测下一个,比如说,300个数据点的数据值

但是,只有满足特定条件的数据才会被绘制,因此没有为add_subplot()方法定义子图的数量。我知道plot.subplots()函数,但是

fig, (ax1, ax2) = plt.subplots(1, 2)
这意味着两张图肯定会被绘制出来,我需要改变具体的数量,比如一个参数

以下是当前代码的简化版本,导致每个绘图位于单独的窗口中:

fig = plt.figure()  # creates a figure instance for the final graph output

plots = 1  # indicates the total number of plots to plot, starting from 1
           # passed as a parameter to the add_subplot() function

for data in dataSet:
    forecast(data, fig, plots)

plt.figure(fig.number)

plt.show()
以及功能:

import matplotlib.pyplot as plt
import matplotlib.ticker as tick
from pandas import Series
from statsmodels.tsa.holtwinters import ExponentialSmoothing

def forecast(data, superFigure, plotNumber):

    index = range(0, len(data))

    plotData = Series(data, index)

    # fit the data values into a specific model:
    modelFit = ExponentialSmoothing(plotData, trend="add").fit()

    # forecast for the next 300 points:
    modelForecast = modelFit.forecast(300)

    if [condition]:

        # plot the original data points:
        points = plotData.plot(marker='x', color='black', label='Base Data')
        points.set_xlim(0, len(data) + 300)

        # plot the forecast in a different colour:
        modelForecast.plot(marker='x', ax=points, color='blue', label='Forecasted Data')

        plt.title("Plot Title")
        plt.xlabel("X Axis")
        plt.ylabel("Y Axis")

        # format the axes, adding thousand separator
        points.get_xaxis().set_major_formatter(
            tick.FuncFormatter(lambda x, p: format(int(x), ',')))
        points.get_yaxis().set_major_formatter(
            tick.FuncFormatter(lambda x, p: format(int(x), ',')))

        plt.legend()
        plt.show()
这将生成多个图形,例如(实际标签已被剪切)

不幸的是,在查看下一个图形之前,您必须关闭每个图形,我希望每个图形都在一个页面上可见

我尝试将“if[condition]”中的代码更改为:

这些更改生成的图形与左上角的其他图形完全相同,我得到了“MatplotlibDeprecationWarning:使用与以前的轴相同的参数添加轴当前重用以前的实例”警告

如果我将“superFigure.add_子图(10,10,plotNumber)”更改为“superFigure.add_子图(20,20,plotNumber)”,我还将得到“用户警告:未应用紧密布局。紧密布局无法使轴宽度小到足以容纳所有轴装饰”

然后我尝试将其更改为:

if [condition]:

    fig, ax = plt.subplots()

    plotData.plot(marker='x', ax=ax, color='black', label='Base Data')
    modelForecast.plot(marker='x', ax=ax, color='blue', label='Forecasted Data')

    ax.set([...])
    ax.legend()

    plt.show()
假设它不会生成所需的输出,因为它会在每次调用forecast()时重新创建图形,除非图形窗口可以包含多个图形

我有时也会收到以下警告:

运行时警告:已打开20多个数字。创造的数字 通过pyplot接口(
matplotlib.pyplot.figure
)保留 直到显式关闭,可能会消耗太多内存

  • 图,ax=plt.子批次()

如何创建包含所有格式并同时显示在一个窗口中的子图?

要将数据帧绘制到轴
ax
,请使用
df.plot(…,ax=ax)
。我尝试设置ax=ax,但没有改变任何东西。当然,
ax
应该是您要使用的子批。要将数据帧打印到axes
ax
,请使用
df.plot(…,ax=ax)
。我尝试设置ax=ax,但没有改变任何东西。当然,
ax
应该是您要使用的子批。
if [condition]:

    fig, ax = plt.subplots()

    plotData.plot(marker='x', ax=ax, color='black', label='Base Data')
    modelForecast.plot(marker='x', ax=ax, color='blue', label='Forecasted Data')

    ax.set([...])
    ax.legend()

    plt.show()