[英]Group bar plot together Pandas plot
I am new with Python plotting, and I am trying to group bars together using a pandas dataframe.我是 Python 绘图的新手,我正在尝试使用 pandas dataframe 将条形组合在一起。
my input csv looks like this-- (test.csv)我的输入 csv 看起来像这样——(test.csv)
noc,matrix,perf
jump4,dw,7.5
jump4,t2d,7.7
mesh,dw,2
mesh,t2d,4
and python plotting script is--和 python 绘图脚本是--
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.backends.backend_pdf import PdfPages
import numpy as np
df = pd.read_csv("test.csv", sep=",")
pp = PdfPages('rtl_sim_perf.pdf')
# gca stands for 'get current axis'
rc = {"axes.spines.top" : False,
"axes.spines.right" : False,
"axes.edgecolor" : "black"}
plt.style.use(("ggplot", rc))
ax = plt.gca()
df.plot(x='matrix', y=['perf','noc'], ax=ax,kind='bar', edgecolor='k')
plt.xticks(fontsize=5,rotation=0)
plt.yticks(fontsize=5)
plt.legend(fontsize=7)
plt.tight_layout()
plt.savefig(pp, format='pdf')
pp.close()
and I get this result我得到了这个结果
What I want is to group bars based on
matrix
column, so that dw
are together for different noc
and t2d
together for different noc
.我想要的是根据
matrix
列对条形进行分组,以便dw
一起用于不同的noc
和t2d
一起用于不同的noc
。 And the legend shows me noc
type.传说向我展示了
noc
类型。 I am not sure how to do this.我不知道该怎么做。
IIUC, do you want? IIUC,你想要吗?
df.set_index(['matrix','noc'])['perf'].unstack().plot.bar()
Output: Output:
You can achieve this by reshaping your dataframe such that you have 'noc' as columns in your dataframe and using pandas plot. You can achieve this by reshaping your dataframe such that you have 'noc' as columns in your dataframe and using pandas plot.
Another way is using seaborn and you don't have to reshape the dataframe and use the 'hue' parameter.另一种方法是使用 seaborn 并且您不必重塑 dataframe 并使用“色调”参数。
import seaborn as sns
sns.barplot(x='matrix', y='perf', hue='noc', data=df)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.