Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting multiple graphs in separate axes from dataframe

I have a dataframe I want to plot.

The dataframe has five columns, one being used as the x-axis, and the other four columns as the y-axis values.

The current code in use is :

ax = plt.gca()

df.plot(kind='line',x='Vertical',y='Gr', color = 'brown', ax=ax)
df.plot(kind='line',x='Vertical',y='R', color='red', ax=ax)
df.plot(kind='line',x='Vertical',y='B', color='blue', ax=ax)
df.plot(kind='line',x='Vertical',y='Gb', color='cyan', ax=ax)

plt.show()

This outputs 4 graphs, all in the same axes. However, this makes the graphs not very readable, as the graphs can be very noisy and they overlap with each other a lot.

For example:

enter image description here

Is there a way to separate the four graphs into different axes so that I can read each graph separately, other than repeating the entire code four times?

like image 398
wookiekim Avatar asked Sep 19 '26 20:09

wookiekim


1 Answers

Try:

fig, ax = plt.subplots(2,2, figsize=(10,8))

df.plot(kind='line',x='Vertical',y='Gr', color = 'brown', ax=ax[0,0])
df.plot(kind='line',x='Vertical',y='R', color='red', ax=ax[0,1])
df.plot(kind='line',x='Vertical',y='B', color='blue', ax=ax[1,0])
df.plot(kind='line',x='Vertical',y='Gb', color='cyan', ax=ax[1,1])

plt.show()

OR

df[['Gr','R','B','Gb']].plot(subplots=True, figsize=(10,8))
like image 75
Scott Boston Avatar answered Sep 21 '26 10:09

Scott Boston



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!