Home > Back-end >  How can I draw 2 subplots into same track?
How can I draw 2 subplots into same track?

Time:04-22

As you can see I'm trying to draw line graphs which gets its data from the dataframe that I created. I want users to see these drawings in the same page. In other words I want to get it as a single output. How can I do it? Thanks.

import matplotlib.pyplot as plt 
import pandas as pd


ax1 = plt.subplots(1,1, figsize=(15,5))

ax1 = df2['Su Seviyesi (m)'].plot(use_index= True)

ax1 = df2['KUYU KOTU (m)'].plot(use_index= True, label="KUYU KOTU")

plt.legend(ncol=3, loc="lower left")

ax1.set(xlabel='Tarih', ylabel='Su Seviyesi (m)')

ax1.get_ygridlines()

ax1.get_yticklines(minor=False)

ax2 = plt.subplots(1,1 ,figsize=(15,5))

ax2 = df2['NaHCO3'].plot(use_index= True, grid=True)

ax2 = df2['TA'].plot(use_index= True, grid=True)

ax2 = df2['Na2CO3'].plot(use_index= True, grid=True)

ax2.get_ygridlines()

ax2.get_yticklines(minor=False)

plt.legend(ncol=3, loc="upper right")

plt.grid()

Output :

Output

Output Expectation :

Expectation

CodePudding user response:

This is what I think you are trying to do.

import matplotlib.pyplot as plt 
import pandas as pd

# Note 1: Subplots returns axes objects, unlike `plt.subplot` 
#   which works more like the Matlab subplot command and needs 
#   to be called repeatedly to activate each axis
# Note 2: We need a figsize that is twice as high as one axis
# Note 3: For plots like this it helps to share an x axis
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(15,10), sharex=True)

# Note 4: Here I am passing through the target axis.
#   No need to capture the output here
df2['Su Seviyesi (m)'].plot(ax=ax1, use_index=True)
df2['KUYU KOTU (m)'].plot(ax=ax1, use_index=True, label="KUYU KOTU")

ax1.legend(ncol=3, loc="lower left")

ax1.set(xlabel='Tarih', ylabel='Su Seviyesi (m)')

# Note 5: We can easily plot multiple lines with one call to .plot 
#   if we just select the right columns.
df2[['NaHCO3', 'TA', 'Na2CO3']].plot(ax=ax2, use_index=True, grid=True)

ax2.legend(ncol=3, loc="upper right")

CodePudding user response:

I solved the problem like this. Thanks for your anwers.

import matplotlib.pyplot as plt 
import pandas as pd

fig, (ax1, ax2) = plt.subplots(2, figsize=(15,10))

fig.suptitle('{} Kuyusu Verileri'.format(e1_string))

ax1.plot(df2['NUMUNE'], df2['Su Seviyesi (m)'], label= 'Su Seviyesi (m)')

ax1.plot(df2['NUMUNE'], df2['KUYU KOTU (m)'], label= 'KUYU KOTU (m)')

ax1.get_ygridlines()

ax1.get_yticklines(minor=False)

ax1.legend(loc="upper right")

ax1.grid()

ax2.plot(df2['NUMUNE'], df2['NaHCO3'], label = '% NaHCO3' )

ax2.plot(df2['NUMUNE'], df2['Na2CO3'], label = '% Na2CO3' )

ax2.plot(df2['NUMUNE'], df2['TA'], label = '% TA' )

ax2.get_ygridlines()

ax2.get_yticklines(minor=False)

ax2.legend(loc="upper right")

ax2.grid()

  • Related