如何用p轴中的x轴标签在pandas中生成直方图?

时间:2015-03-24 11:01:22

标签: python pandas

鉴于以下数据框:

import pandas as pd
df = pd.read_json('{"genre":{"0":"Drama","1":"Comedy","2":"Action","3":"Thriller"},"count":{"0":1603,"1":1200,"2":503,"3":492}}')

是否有大熊猫单线/快速生成直方图,其中条形基于“计数”列,x轴标签是“类型”列?

1 个答案:

答案 0 :(得分:7)

是的,您可以选择要与xy关键字参数一起使用的列。您还可以使用kind='bar'选择所需的绘图类型,在本例中为hist。

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_json('{"genre":{"0":"Drama","1":"Comedy","2":"Action","3":"Thriller"},"count":{"0":1603,"1":1200,"2":503,"3":492}}')

df.plot(x='genre', y='count', kind='bar', legend=False)

plt.show()

请注意,我还使用legend=False删除了图例,如果您愿意,可以将其保留。

enter image description here