Home > Enterprise >  How to plot slices of a df rows?
How to plot slices of a df rows?

Time:03-10

I have this 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]

I want to plot df['PP'] data of every 12 rows so I did this code:

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

Is there a more efficient way to do this?

CodePudding user response:

You could build a dictionary the maps colors to markers and enumerate over 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])

Output:

enter image description here

CodePudding user response:

You can use plotly library

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