如何绘制 df 行的切片?

How to plot slices of a df rows?

我有这个df:

    CODE  MONTH  PP   ORDER
0   000130  01  1.8      7
1   000130  02  5.2      7
2   000130  03  4.4      7
3   000130  04  2.3      7
4   000130  05  0.6      7
..     ...  ..  ...    ...
7   000130  08  0.0      6
8   000130  09  0.0      6
9   000130  10  0.0      6
10  000130  11  0.0      6
11  000130  12  0.8      6

[84 rows x 4 columns]

我想绘制每 12 行的 df['PP'] 数据,所以我做了以下代码:

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')

有没有更有效的方法来做到这一点?

您可以使用 plotly

import plotly.express as px
# and you can add the column separate your data on that 
fig = px.line(df, x='Date & Time', y='column name', title=" test", color='column name')

您可以构建一个字典,将颜色映射到标记并枚举 colors:

import matplotlib.pyplot as plt
df = pd.DataFrame({'PP': np.random.rand(84)})

colors = ['black', 'blue', 'green', 'yellow', 'red', 'orange', 'brown']
markers = dict.fromkeys(colors[1:], ['o','-']) | {'black': ['o', '--']}
for i, color in enumerate(colors): 
    plt.plot(df.loc[12*i: 12*(i+1), 'PP'], linestyle=markers[color][-1], 
             color=color, marker=markers[color][0])

输出: