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

2024-05-17 05:05:09 发布

您现在位置:Python中文网/ 问答频道 /正文

给定以下数据帧:

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}}')

有没有一种pandas单行/快速生成直方图的方法,其中条形图基于“count”列,x轴标签是“genre”列?在


Tags: 数据importjsonpandasdfreadascount
1条回答
网友
1楼 · 发布于 2024-05-17 05:05:09

是的,您可以选择要与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

相关问题 更多 >