簡體   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