如何为我的数据集创建多行图?

时间:2017-08-07 13:42:39

标签: python pandas matplotlib dataframe seaborn

我有以下pandas DataFrame df

df = pd.DataFrame(columns=["Event1", "Event2", "Event3"], 
                                data=[[15,1,22],
                                      [16,1.26,80],
                                      [27,0,15]])

df = df.set_index([["Series1", "Series2", "Series3"]])

我想创建一个X轴包含Event1Event2Event3的多线图,而Y轴应该是相应的数值。应该有3个系列:Series1Series2Series3

如何在x中定义yhuesns.pointplot(x=???, y=???, hue=???,data=df)

plt.figure(figsize=(12,8))
ax = sns.pointplot(x=???, y=???, hue=???,data=df)
ax.grid(b=True, which='major', color='#d3d3d3', linewidth=1.0)
ax.grid(b=True, which='minor', color='#d3d3d3', linewidth=0.5)
plt.show()

3 个答案:

答案 0 :(得分:2)

将数据透视表的df重新组织为经典数据框并根据需要绘制:

import matplotlib.pylab as plt
import pandas as pd
import seaborn as sns

df = pd.DataFrame(columns=["Event1", "Event2", "Event3"], 
                                data=[[15,1,22],
                                      [16,1.26,80],
                                      [27,0,15]])
df = df.set_index([["Series1", "Series2", "Series3"]])
print(df)

# reorganize df to classic table
df2=df.stack().reset_index()
df2.columns = ['Series','Event','Values']
print(df2)

plt.figure(figsize=(12,8))
ax = sns.pointplot(x='Event', y='Values', hue='Series',data=df2)
ax.grid(b=True, which='major', color='#d3d3d3', linewidth=1.0)
ax.grid(b=True, which='minor', color='#d3d3d3', linewidth=0.5)
plt.show()

enter image description here

DF2:

    Series   Event  Values
0  Series1  Event1   15.00
1  Series1  Event2    1.00
2  Series1  Event3   22.00
3  Series2  Event1   16.00
4  Series2  Event2    1.26
5  Series2  Event3   80.00
6  Series3  Event1   27.00
7  Series3  Event2    0.00
8  Series3  Event3   15.00

答案 1 :(得分:1)

我不知道如何使用seaborn,但使用matplolib它是这样的:

for i in df.index.values:
    plt.plot(list(df.loc[i]))
plt.show()

在这种情况下,X ax将取值0,1和2,而Y ax将取值。最后只需要plt.show()将所有绘图都放在一个图中。在seaborn应该以相同的方式工作。

答案 2 :(得分:1)

获得所需图表的最简单方法是import pandas as pd df = pd.DataFrame(columns=["Event1", "Event2", "Event3"], data=[[15,1,22], [16,1.26,80], [27,0,15]]) df = df.set_index([["Series1", "Series2", "Series3"]]) df.T.plot()

div

amqp interop