简体   繁体   中英

How to overlay plots in python with matplotlib

I'm using two related packages that generate plots I want to overlay for comparison. I call a method called plot_spectro from each package which plots to plt. Then I must do plt.legend() and plt.show() to see them. What happens is two plots with the same data ranges appear, but I would like to overlay (superimpose) them.

import matplotlib.pyplot as plt

s.plot_spectro(xaxis=x, yaxis=y)

plt.xlim(-6,2)
plt.ylim(-2.5,2.5)

o1.plot_spectro(xaxis=x, yaxis=y,  color='b')

plt.xlim(-6,2)
plt.ylim(-2.5,2.5)

plt.legend()

plt.show()

Create an axis instance and pass it to both the plots as shown below

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

s.plot_spectro(xaxis=x, yaxis=y, ax=ax) # <--- pass ax=ax here
o1.plot_spectro(xaxis=x, yaxis=y,  color='b', ax=ax) # <--- pass ax=ax here

plt.xlim(-6,2)
plt.ylim(-2.5,2.5)

plt.legend()
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM