首页 > 解决方案 > 图表标题一一打印

问题描述

图表标题被一一打印。我怎么解决这个问题??

这是我的代码。

plt.figure(figsize=(25,10))
plt.subplots_adjust(left=0.125, bottom=0.1,  right=0.9, top=0.9, wspace=0.2, hspace=0.4)
n = 1

for idx, i in enumerate(top_10_df_event_copy['Code'].unique()):
    top_10_df_event_copy_list[i] = pd.Series(winsorize(top_10_df_event_copy_list[i].values, limits=[0, 0.1]))
    stl = seasonal_decompose(top_10_df_event_copy_list[i].values, freq=3)
    plt.title(f"{i}", fontsize=15)
    ax = plt.subplot(4,3,n)
    
    ax.plot(stl.seasonal + stl.trend)
    ax.plot(stl.observed, color='red', alpha=0.5)
    n += 1
    print(i)
    

plt.show()

i = [90001302, 90001341, 90001441, 90001443, 90001521, 90001541, 90001542, 90001582, 90001602, 90001622]

最后打印(i)

90001302 90001341 90001441 90001443 90001521 90001541 90001542 90001582 90001602 90001622

但是图形像这张图片一样打印。

10 个图中有 10 个标题,但实际上 10 个图中只输出了 9 个标题。

请帮助..

在此处输入图像描述

标签: pythonmatplotlib

解决方案


使用面向对象的界面来设置标题,而不是使用 pyplot 状态机:

from matplotlib import pyplot
fig = pyplot.figure(figsize=(25,10))

for i in range(10):
    ax = fig.add_subplot(4, 3, i+1)
    ax.set_title(f"This is axes #{i+1}")
    
fig.subplots_adjust(left=0.125, bottom=0.1,  right=0.9, top=0.9, wspace=0.2, hspace=0.4)

在此处输入图像描述


推荐阅读