Search code examples
pythonmatplotlibsubplotfig

Showing two figures using matplotlib problem


I am trying to depict two figures simultaneously in two different plots. I am getting though only one figure (the first one in particular).

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt
df = pd.read_excel("My excel.xlsx")
A1 = some value 
A2 = value 
A20 = value 
Z = value 

fig = plt.figure()
ax = fig.add_subplot()
ax.plot(Z,A1)
ax.plot(Z,A2)
ax.plot(Z,A3)
ax.plot(Z,A4)
ax.plot(Z,A5)
ax.plot(Z,A6)
ax.plot(Z,A20) 
plt.legend(loc = 'upper left')
plt.figure()
plt.show()

fig = plt.figure()
ax.plot = fig.add_subplot()
B1 = value 
B2 = value 
B3 = value 
B4 = value 
B20 = value 

ax.plt(Z,B1)
ax.plt(Z,B2)
ax.plt(Z,B20)
plt.legend(loc= 'upper left')
plt.figure()
plt.show()

I've seen a relevant post dealing with the same issue but was not able to come to a conclusion. Any help is welcome. Thank you in advance!


Solution

  • Here's a link that might help you creating multiple subplots : https://matplotlib.org/stable/gallery/subplots_axes_and_figures/subplots_demo.html
    I would also suggest to use list as arguments with plt.plot :
    Use

    ax.plt([Z, Z, Z], [B1, B2, B20])
    
    

    instead of

    ax.plt(Z,B1)
    ax.plt(Z,B2)
    ax.plt(Z,B20)