Home > Back-end >  How to create a plot with 4 conditions with different colors
How to create a plot with 4 conditions with different colors

Time:06-15

I tried using

ax = pintar_campo('grey')

df_suarez.plot(kind='scatter', x='x<=17', y='y', color='light blue', ax=ax)
df_suarez.plot(kind='scatter', x='x>17 & x<=50', y='y', color='blue', ax=ax)
df_suarez.plot(kind='scatter', x='x>50 & x<=83', y='y', color='dark blue', ax=ax)
df_suarez.plot(kind='scatter', x='x>83', y='y', color='purple', ax=ax)

ax.set_title("Los eventos de Luís Suárez")

I have 4 zones and which zone have your condition:

  • Zone 1: x <= 17
  • Zone 2: x > 17 and x <=50
  • Zone 3: x>50 and x <=83
  • Zone 4: x>83

CodePudding user response:

You can filter the dataframe with your condition first.

df.query('x<=17').plot(kind='scatter', x='x', y='y', color='red', ax=ax)
df.query('x>17& x<=50').plot(kind='scatter', x='x', y='y', color='blue', ax=ax)
df.query('x>50& x<=83').plot(kind='scatter', x='x', y='y', color='black', ax=ax)
df.query('x>83').plot(kind='scatter', x='x', y='y', color='purple', ax=ax)
# pr
df[df['x']<=17].plot(kind='scatter', x='x', y='y', color='red', ax=ax)
  • Related