简体   繁体   English

如何用 matplotlib 覆盖 python 中的图

[英]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.我从每个绘制到 plt 的 package 调用一个名为 plot_spectro 的方法。 Then I must do plt.legend() and plt.show() to see them.然后我必须执行 plt.legend() 和 plt.show() 才能看到它们。 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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