尝试在Pandas水平条形图中标注x和y轴时出错

时间:2019-07-17 17:33:45

标签: python pandas matplotlib

我正在尝试在水平条形图上设置x和y标签,但出现此错误。请帮助我。

这是我的图形代码:

alt_method[['Jan_avg','Jan_count']].sort_values(by=['Jan_avg','Jan_count'],ascending=True).plot.barh(xlim = (0,60000),ylim = (6300.0,64289.0))

alt_method.set_xlabel('Shipment')

这是我的错误信息:

  

AttributeError: 'DataFrame' object has no attribute 'set_xlabel'

2 个答案:

答案 0 :(得分:1)

var query=paymentsCollection.Descendants("Payment") .Select(p=>new Payment{ PaymentId=(string)p.Element("PaymentId"), PaymentAmount= (decimal)p.Element("PaymentAount"), Invoices=p.Elements("Invoice") .Select(i=> new Invoice{ Id=(string)i.Element("Id"), InvoiceAmount=(decimal)i.Element("InvoiceAmount") } ).ToList() } ).ToList(); 是一个数据框。您可以使用alt_method来绘制DataFrame,但是要设置轴标签,则应使用通过.plot()(通常缩写为matplotlib.pyplot)可用的绘制命令:

plt

可重现的示例:

import matplotlib.pyplot as plt

df.plot() # per your code
plt.ylabel('ylabel goes here')
plt.show()

答案 1 :(得分:0)

另一种方法是抓住plot返回的轴手柄,然后将标签设置为

ax = alt_method[['Jan_avg','Jan_count']].sort_values(by=['Jan_avg','Jan_count'],ascending=True).plot.barh(xlim = (0,60000),ylim = (6300.0,64289.0))

ax.set_xlabel('Shipment')