简体   繁体   English

Python Pandas - 从数据框中按类别绘制多个条形图

[英]Python Pandas - Plotting multiple Bar plots by category from dataframe

I have dataframe which looks like我有看起来像的数据框

df = pd.DataFrame(data={'ID':[1,1,1,2,2,2], 'Value':[13, 12, 15, 4, 2, 3]})

Index ID Value
0   1   13
1   1   12
2   1   15
3   2   4
4   2   2
5   2   3

and I want to plot it by the IDs (categories) so that each category would have different bar plot,我想按 ID(类别)绘制它,以便每个类别都有不同的条形图,
so in this case I would have two figures,所以在这种情况下,我会有两个数字,
one figure with bar plot of ID=1,一个带有 ID=1 条形图的图形,
and second separate figure bar plot of ID=2.和 ID=2 的第二个单独的图形条形图。

Can I do it (preferably without loops) with something like df.plot(y='Value', kind='bar') ?我可以用df.plot(y='Value', kind='bar')东西来做(最好没有循环df.plot(y='Value', kind='bar')吗?

2 options are possible, one using matplotlib and the other seaborn that you should absolutely now as it works well with Pandas.有 2 个选项是可能的,一个使用 matplotlib,另一个使用 seaborn,你现在绝对应该使用它,因为它与 Pandas 配合得很好。

Pandas with matplotlib带有 matplotlib 的 Pandas

You have to create a subplot with a number of columns and rows you set.您必须使用您设置的许多列和行创建一个子图。 It gives an array axes in 1-D if either nrows or ncols is set to 1, or in 2-D otherwise.如果nrowsncols设置为 1,则它以 1-D 形式给出数组axes ,否则以 2-D 形式给出。 Then, you give this object to the Pandas plot method.然后,您将此对象提供给 Pandas 绘图方法。

If the number of categories is not known or high, you need to use a loop.如果类别数未知或很高,则需要使用循环。

import pandas as pd
import matplotlib.pyplot as plt

fig, axes = plt.subplots( nrows=1, ncols=2, sharey=True )

df.loc[ df["ID"] == 1, 'Value' ].plot.bar( ax=axes[0] )
df.loc[ df["ID"] == 2, 'Value' ].plot.bar( ax=axes[1] )

plt.show()

输出 matplotlib/Pandas

Pandas with seaborn与 seaborn 的熊猫

Seaborn is the most amazing graphical tool that I know. Seaborn是我所知道的最神奇的图形工具。 The function catplot enables to plot a series of graph according to the values of a column when you set the argument col .当您设置参数col时,函数catplot可以根据列的值绘制一系列图形。 You can select the type of plot with kind .您可以使用kind选择绘图kind

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

sns.set_style('white')

df['index'] = [1,2,3] * 2
sns.catplot(kind='bar', data=df, x='index', y='Value', col='ID')
plt.show()

输出 Pandas/Seaborn

I added a column index in order to compare with the df.plot.bar .我添加了一个列index以与df.plot.bar进行比较。 If you don't want to, remove x='index' and it will display an unique bar with errors.如果您不想,删除x='index'它将显示一个带有错误的唯一栏。

Seaborn 按类别组合值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM