繁体   English   中英

群吧 plot 合 Pandas plot

[英]Group bar plot together Pandas plot

我是 Python 绘图的新手,我正在尝试使用 pandas dataframe 将条形组合在一起。

我的输入 csv 看起来像这样——(test.csv)

noc,matrix,perf
jump4,dw,7.5
jump4,t2d,7.7
mesh,dw,2
mesh,t2d,4

和 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()

我得到了这个结果

在此处输入图像描述 我想要的是根据matrix列对条形进行分组,以便dw一起用于不同的noct2d一起用于不同的noc 传说向我展示了noc类型。 我不知道该怎么做。

IIUC,你想要吗?

df.set_index(['matrix','noc'])['perf'].unstack().plot.bar()

Output:

在此处输入图像描述

You can achieve this by reshaping your dataframe such that you have 'noc' as columns in your dataframe and using pandas plot.


另一种方法是使用 seaborn 并且您不必重塑 dataframe 并使用“色调”参数。

import seaborn as sns
sns.barplot(x='matrix', y='perf', hue='noc', data=df)

Output: 在此处输入图像描述

暂无
暂无

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

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