python - 当 subplots=True 时,如何在 pandasplot() 方法中编辑标题大小和位置?

标签 python pandas matplotlib

我正在比较 9 家商店的销售数据:

enter image description here

但是标题很小,似乎在图表内。我不知道如何改变这种行为或字体大小。

使用 plt.title 不起作用 - 它只能编辑最终的子图标题。

我还尝试在 pandas .plot() 方法的末尾添加 title() 调用,但这会导致错误:'numpy. ndarray'对象没有属性'title'

如何编辑标题字体大小和顶部位置?

我的代码:

# plot the data
df.set_index('local_date').plot(kind='line', subplots=True, grid=True,figsize=(30,20),lw=3.5,linestyle='--', title="Sample Data (Unit)",
    layout=(4, 3), sharex=True, sharey=True, legend=False)

# edit the legends
[ax.legend(loc=2,prop={'size': 20}) for ax in plt.gcf().axes]

#plt.title(title,fontsize=20) # this only edits final plot title
plt.tight_layout()

以及pd.DataFrame.fromt_dict()的示例数据:

from pandas import Timestamp

myd = {'local_date': {0: Timestamp('2020-01-06 00:00:00'),
  1: Timestamp('2020-01-13 00:00:00'),
  2: Timestamp('2020-01-20 00:00:00'),
  3: Timestamp('2020-01-27 00:00:00'),
  4: Timestamp('2020-02-03 00:00:00'),
  5: Timestamp('2020-02-10 00:00:00'),
  6: Timestamp('2020-02-17 00:00:00'),
  7: Timestamp('2020-02-24 00:00:00'),
  8: Timestamp('2020-03-02 00:00:00'),
  9: Timestamp('2020-03-09 00:00:00'),
  10: Timestamp('2020-03-16 00:00:00'),
  11: Timestamp('2020-03-23 00:00:00'),
  12: Timestamp('2020-03-30 00:00:00')},
 'shop1': {0: 100.0,
  1: 156.0,
  2: 1231.0,
  3: 360.0,
  4: 250.0,
  5: 150.0,
  6: 1287.0,
  7: 397.0,
  8: 546.0,
  9: 270.0,
  10: 50.0,
  11: 1464.0,
  12: 280.0},
 'shop2': {0: 0.0,
  1: 430.0,
  2: 270.0,
  3: 900.0,
  4: 665.0,
  5: 750.0,
  6: 780.0,
  7: 360.0,
  8: 704.0,
  9: 480.0,
  10: 470.0,
  11: 0.0,
  12: 730.0},
 'shop3': {0: 222.0,
  1: 258.0,
  2: 255.0,
  3: 367.0,
  4: 332.0,
  5: 324.0,
  6: 269.0,
  7: 336.0,
  8: 492.0,
  9: 349.0,
  10: 329.0,
  11: 354.0,
  12: 581.0},
 'shop4': {0: 607.0,
  1: 140.0,
  2: 172.0,
  3: 89.0,
  4: 63.0,
  5: 700.0,
  6: 157.0,
  7: 401.0,
  8: 112.0,
  9: 180.0,
  10: 76.0,
  11: 104.0,
  12: 346.0},
 'shop5': {0: 51.0,
  1: 375.0,
  2: 78.0,
  3: 92.0,
  4: 59.0,
  5: 193.0,
  6: 137.0,
  7: 96.0,
  8: 331.0,
  9: 195.0,
  10: 291.0,
  11: 347.0,
  12: 190.0},
 'shop6': {0: 150.0,
  1: 225.0,
  2: 345.0,
  3: 150.0,
  4: 230.0,
  5: 0.0,
  6: 500.0,
  7: 150.0,
  8: 0.0,
  9: 230.0,
  10: 50.0,
  11: 270.0,
  12: 0.0},
 'shop7': {0: 39.0,
  1: 130.0,
  2: 278.0,
  3: 0.0,
  4: 0.0,
  5: 585.0,
  6: 175.0,
  7: 0.0,
  8: 360.0,
  9: 79.0,
  10: 135.0,
  11: 0.0,
  12: 0.0},
 'shop8': {0: 74.0,
  1: 66.0,
  2: 75.0,
  3: 95.0,
  4: 158.0,
  5: 50.0,
  6: 233.0,
  7: 117.0,
  8: 146.0,
  9: 142.0,
  10: 261.0,
  11: 30.0,
  12: 85.0},
 'shop9': {0: 74.0,
  1: 146.0,
  2: 166.0,
  3: 6.0,
  4: 75.0,
  5: 22.0,
  6: 33.0,
  7: 18.0,
  8: 15.0,
  9: 110.0,
  10: 150.0,
  11: 144.0,
  12: 561.0}}

最佳答案

整体图表标签由plt.gcf().suptitle()控制。

然后,为了将标题放在绘图之外,您可以执行以下操作(从此处: https://stackoverflow.com/a/45161551/42346 ):

plt.gcf().tight_layout(rect=[0, 0.03, 1, 0.95])

所以完整的例子是:

df.set_index('local_date').plot(kind='line', subplots=True, grid=True,figsize=(30,20),
             lw=3.5,linestyle='--', layout=(4, 3), sharex=True, sharey=True, legend=False) 

# edit the legends 
[ax.legend(loc=2,prop={'size': 20}) for ax in plt.gcf().axes] 

plt.gcf().tight_layout(rect=[0, 0.03, 1, 0.95])
plt.gcf().suptitle("Sample Data (Unit)",fontsize=20)
plt.show()

结果:
enter image description here

关于python - 当 subplots=True 时,如何在 pandasplot() 方法中编辑标题大小和位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61542232/

相关文章:

python-3.x - 对于来自 seaborn 的计数图,Matplotlib 的替代方案是什么?

python - Matplotlib savefig 背景总是透明的

python - 不显示绘图 : correct use of matplotlib. use()

python - Pandas 。将重复的行删除到另一个数据框

python - 具有矢量输出和 2D 图像图输入的 CNN(输入是数组)

python - Pandas 中的列名浮点为整数

python - nltk NER 单词提取

python - 从 Pandas Dataframe 中的字典列表中获取值

python - Pandas interpolate 0.24 版本后抛出无效填充方法错误

python - 比较文件之间的字段对,允许偏移