Matplotlib。设置标题 x 和 y 标签

poo*_*098 2 python matplotlib pandas

我正在尝试设置我的情节标题和 x 和 y 标签。总数是所有数字 float64。我正在尝试以尽可能简单的方式进行。

这是我的代码的一部分:

plt.close('all')
cand_all =pd.DataFrame({'Bachmann':[total15],
                        'Romney':[total2],
                        'Obama':[total3],
                        'Roemer': [total4],
                       'Pawlenty':[total5],
                       'Johnson':[total6],
                       'Paul':[total7],
                       'Santorum':[total8],
                       'Cain':[total9],
                       'Gingrich':[total10],
                       'McCotter':[total12],
                       'Huntsman':[total13],
                       'Perry':[total14]}) 



cand_all.plot.bar()
Run Code Online (Sandbox Code Playgroud)

rra*_*d88 5

您需要提供一个 axes ( ax) 参数。

ax=plt.subplot(111)
cand_all.plot.bar(ax=ax)
ax.set_xlabel('xlabel')
ax.set_ylabel('ylabel')
ax.set_title('title')
Run Code Online (Sandbox Code Playgroud)

请注意,对于pandas数据帧,索引名称(如果存在)将自动成为 x 标签。