【发布时间】:2020-09-14 02:03:22
【问题描述】:
我有一个名为 for_plot 的日期框,我正在使用以下 sn-p 代码制作条形图:
import matlplotlib.pyplot as plt
f, ax = plt.subplots(1,1,figsize=(10,8))
x_1 = for_plot['res_code'] - 0.2
x_2 = for_plot['res_code']
pre = for_plot['n_indnota_pre']
post = for_plot['n_indnota_post']
ax.bar(x_1,pre, width = 0.2, color = 'red')
ax.bar(x_2,post, width = 0.2, color = 'green')
ax.set_xticks([0.9,1.9,2.9])
ax.set_xticklabels(['GEN','ST','SC'])
ax.legend(['pre_nota', 'post_nota'],loc = 1)
ax.xlabel('Constituency Type')
ax.ylabel('No of Independent Candidates')
ax.title('Average No Of Independent Candidates by Constituency Type')
plt.show()
我确实了解如何使用 matplotlib,但我有一些关于细微差别的问题:
- sn-p 第 1 行中的
f有什么作用?正如 sn-p 所代表的那样,f和ax代表什么? - 我无法使用
ax.xlabel('Constituency Type')添加轴标签和图表标题(如 sn-p 的最后 4 行),但在绘制其他图形时,如果我不首先调用subplot()行并使用plt.xlabel('Constituency Type'),它工作得非常好。为什么会这样?
我收到以下错误:
AttributeError: 'AxesSubplot' object has no attribute 'xlabel'
编辑1:
f.xlabel('Constituency Type')
f.ylabel('No of Independent Candidates')
f.title('Average No Of Independent Candidates by Constituency Type')
也不行。
AttributeError:'Figure' object has no attribute 'xlabel'
【问题讨论】:
标签: python matplotlib bar-chart data-visualization visualization