Групповая гистограмма вместе Pandas график - PullRequest
1 голос
/ 12 июля 2020

Я новичок в построении Python, и я пытаюсь сгруппировать столбцы вместе, используя pandas фрейм данных.

мой входной 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 были вместе для разных noc и t2d вместе для разных noc. А легенда показывает мне тип noc. Я не знаю, как это сделать.

1 Ответ

1 голос
/ 12 июля 2020

IIU C, хотите?

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

Вывод:

enter image description here

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.

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

Output: введите описание изображения здесь

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...