Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Plot several plots together as subplots

I try to plot several plots with multiple data series as subplots.

import pandas as pd
import matplotlib.pyplot as plt

df1 = pd.DataFrame({
    'Date':['2021-01-01', '2021-01-02', '2021-01-03', '2021-01-04', '2021-01-05'],
    'index':[0, 1, 2, 3, 4], 
    '02K W':[3.5, 0.1, 3, 'nan', 0.2], 
    '03K W':[4.2, 5.2, 2.5, 3.0, 0.6], 
    '04K W':[1.5, 2.6, 8.2, 4.2, 5.3]}) 

df2 = pd.DataFrame({
    'Date':['2021-01-01', '2021-01-02', '2021-01-03', '2021-01-04', '2021-01-05'],
    'index':[0, 1, 2, 3, 4], 
    '02K W':[3, 'nan', 5, 3, 5.2], 
    '03K W':[2.1, 2.9, 2.5, 3.9, 6.7], 
    '04K W':[1.8, 6.2, 5, 2.5, 3.7]}) 

df1['Date'] = pd.to_datetime(df1['Date'])
df1 = df1.set_index('index')

for col in df1.columns[1:]:
    plt.plot(df1['Date'], df1[col])

At the moment, I can only plot the first plot and I am not sure how I need to assign the for loop to the y-axis.

fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.plot(df1['Date'], y)
ax2.plot(df2['Date'], y)

Thanks a lot for the advise.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Is this what you are looking for?

fig, (ax1, ax2) = plt.subplots(1, 2)
for col in df1.columns[1:]:
    ax1.plot(df1['Date'], df1[col])
    ax2.plot(df2['Date'], df2[col])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading