如何从列中使用x轴标签在 pandas 中生成直方图? [英] how to generate histogram in pandas with x-axis labels from column?

查看:76
本文介绍了如何从列中使用x轴标签在 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轴标签是类型"列?

Is there a pandas one-liner/fast way to generate a histogram where bars are based on the "count" column, and x-axis labels are the "genre" column?

推荐答案

是的,您可以选择要与xy关键字参数一起使用的列.您还可以使用kind='bar'选择所需的绘图类型(在这种情况下为历史记录).

Yes you can select the columns to be used with the x and y keyword arguments. You also select the kind of plot you want, in this case a hist, using kind='bar'.

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删除了图例,如果愿意,可以将其保留在其中.

Note that I've also used legend=False to remove the legend, you could leave this in if you so wish.

这篇关于如何从列中使用x轴标签在 pandas 中生成直方图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆