gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:06:52 25 4
gpt4 key购买 nike

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

enter image description here

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

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

我也尝试添加 title() Pandas 末呼唤.plot()方法,但这会导致错误:'numpy.ndarray' object has no attribute '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 时,如何在 pandas plot() 方法中编辑标题大小和位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61542232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com