简体   繁体   English

如何按组的唯一值 plot?

[英]How to plot by groups of unique values?

I have this df:我有这个 df:

      CODE   MONTH   PP  ORDER
0     000130  01    1.8    1 
1     000130  02    5.2    1 
2     000130  03    4.4    1
3     000130  04    2.3    1
4     000130  05    0.6    1
     ...  ..  ...
1555  158313  08    0.0    1
1556  158313  09    0.0    1
1557  158313  10    0.0    1
1558  158313  11    0.0    1
1559  158313  12    0.6    1
0     000130  01    1.2    2
1     000130  02    6.2    2
2     000130  03    4.8    2
3     000130  04    1.3    2
4     000130  05    1.6    2
     ...  ..  ...
1555  158313  08    2.2    2
1556  158313  09    1.1    2
1557  158313  10    0.1    2
1558  158313  11    2.0    2
1559  158313  12    2.1    2

I want to plot df['MONTH'] and df['PP'] by code.我想通过代码 plot df['MONTH']df['PP'] I want to plot the values of df['MONTH'] and df['PP'] with code 000130 and order 1, and the second values of df['MONTH'] and df['PP'] with code 000130 and order 2, and the third etc etc in the same graph.我想要 plot df['MONTH']df['PP']的值与代码 000130 和顺序 1,以及df['MONTH']df['PP']的第二个值与代码 000130 和顺序2,以及同一张图中的第三个等等。 I want to do this by each code.我想通过每个代码来做到这一点。 So i tried this code:所以我尝试了这段代码:

for code, dfs in df.groupby('CODE'):
    fig, ax = plt.subplots(figsize=(30, 15))
    
    
    plt.plot(df.iloc[0:12]['PP'],'o--',color='black')
    plt.plot(df.iloc[12:24]['PP'],'o-',color='blue', marker='o')
    plt.plot(df.iloc[24:36]['PP'],'o-',color='green', marker='o')
    plt.plot(df.iloc[36:48]['PP'],'o-',color='yellow', marker='o')
    plt.plot(df.iloc[48:60]['PP'],'o-',color='red', marker='o')
    plt.plot(df.iloc[60:72]['PP'],'o-',color='orange', marker='o')
    plt.plot(df.iloc[72:84]['PP'],'o-',color='brown', marker='o')

But it doesn't plot correctly.但它不正确 plot 。 Would you mind to help me?你愿意帮我吗?

Thanks in advance.提前致谢。

It is not clear if you want everything plotted on the same figure or each plot separately.目前尚不清楚您是要将所有内容绘制在同一个图形上还是分别绘制在每个 plot 上。 If you want everything on a different plot, this plots for each code/order the month on the x axis an the pp on the y axis.如果你想要不同的 plot 上的所有内容,这会为每个代码/订单绘制 x 轴上的月份和 y 轴上的 pp。

groups = df.groupby(['CODE','ORDER'])
for i in groups.groups:
    plt.figure()
    groups.get_group(i)[['MONTH', 'PP']].plot(x='MONTH')
    plt.ylabel('PP')
    plt.title(f'Code{i[0]} = Order {i[1]}')
plt.show()

Or, more pythonic (same result):或者,更多 pythonic(相同的结果):

df.groupby(['CODE','ORDER'])['MONTH','PP'].plot(x='MONTH')
plt.show()

If you want everything in the same figure instead:如果您希望所有内容都在同一个图中:

fig, ax = plt.subplots(figsize=(10,8))
for code in df.CODE.unique():
    for order in df.ORDER.unique():
        df.loc[(df.CODE==code)&(df.ORDER==order)][['MONTH','PP']].plot(x='MONTH', y='PP', ax=ax, label=str(code)+'-'+str(order))
        ax.set_ylabel('PP')
        ax.set_title('*****', size=10)
plt.show()

Output: Output:

在此处输入图像描述

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

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