简体   繁体   English

如何创建分组条 plot

[英]How to create a grouped bar plot

The goal here is to create a grouped bar plot, not subplots like the image below这里的目标是创建一个分组条 plot,而不是像下图这样的子图

Is there a simple way to create a grouped bar plot in Python?有没有一种简单的方法可以在 Python 中创建分组条 plot? Right now I get separate bar plots, instead of separate bars on one plot.现在我得到单独的条形图,而不是一个 plot 上的单独条形图。

import pandas as pd

df = pd.DataFrame([['g1', 'c1', 10], ['g1', 'c2', 12], ['g1', 'c3', 13], ['g2', 'c1', 8], ['g2', 'c2', 10], ['g2', 'c3', 12]], columns=['group', 'column', 'val'])

  group column  val
0    g1     c1   10
1    g1     c2   12
2    g1     c3   13
3    g2     c1    8
4    g2     c2   10
5    g2     c3   12
    

df.groupby(['group']).plot(kind='bar')

在此处输入图像描述

Pandas will show grouped bars by columns. Pandas 将按列显示分组条。 Entries in each row but different columns will constitute a group in the resulting plot.每行但不同列中的条目将在结果图中构成一个组。 Hence you need to "reshape" your dataframe to have the "group" as columns.因此,您需要“重塑”数据框以将“组”作为列。 In this case you can pivot like在这种情况下,您可以像

df.pivot("column", "group", "val")

producing生产

group   g1  g2
column        
c1      10   8
c2      12  10
c3      13  12

Plotting this will result in a grouped bar chart.绘制这将产生一个分组的条形图。

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame([['g1','c1',10],['g1','c2',12],['g1','c3',13],['g2','c1',8],
                   ['g2','c2',10],['g2','c3',12]],columns=['group','column','val'])

df.pivot("column", "group", "val").plot(kind='bar')

plt.show()

在此处输入图片说明

You can simply do this using the code given below:您可以使用下面给出的代码简单地执行此操作:

import pandas as pd
import matplotlib.pyplot as plt

positive_values = [20, 17.5, 40]
negative_values = [15, 8, 70]
index = ['Precision', 'Recall', 'f1-score',]
df = pd.DataFrame({'Positive Values': positive_values,
                    'Negative Values': negative_values}, index=index)
ax = df.plot.bar(rot=0, color={"Positive Values": "green", "Negative Values": "red"})

Output:输出:

输出

Plotly express is one of the best visualisation packages I've recently used. Plotly express 是我最近使用的最好的可视化包之一。 It allows you to generate visualisations without needing to perform massive data transformations.它允许您生成可视化,而无需执行大量数据转换。

# initial dataframe
df = pd.DataFrame([['g1','c1',10],['g1','c2',12],['g1','c3',13],['g2','c1',8],
                   ['g2','c2',10],['g2','c3',12]],columns=['group','column','val'])

df.head()

   group column val
0   g1   c1     10
1   g1   c2     12
2   g1   c3     13
3   g2   c1      8
4   g2   c2     10
5   g2   c3     12

No need to transform data, directly use plotly express:无需转换数据,直接使用plotly快递:

import plotly.express as px
fig = px.bar(df, x="column", y="val",
             color='group', barmode='group',text="val",
             height=400)
fig.show()

分组条形图

  • Given a dataframe of long data, as shown in the OP, an implementation that does not require transforming the dataframe is to useseaborn.barplot with the hue parameter.给定一个长数据的数据帧,如 OP 中所示,不需要转换数据帧的实现是使用带有hue参数的seaborn.barplot
  • seaborn is a high-level API for matplotlib seabornmatplotlib的高级 API
  • Tested with seaborn 0.11.1 and matplotlib 3.4.2使用seaborn 0.11.1matplotlib 3.4.2测试
import pandas as pd
import seaborn as sns

# the sample dataframe from the OP
df = pd.DataFrame([['g1', 'c1', 10], ['g1', 'c2', 12], ['g1', 'c3', 13], ['g2', 'c1', 8], ['g2', 'c2', 10], ['g2', 'c3', 12]], columns=['group', 'column', 'val'])

# plot with seaborn barplot
sns.barplot(data=df, x='column', y='val', hue='group')

在此处输入图片说明

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

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