我们进行实验,我们的示波器在同一屏幕上显示所有图,尽管每个变量的大小不同。是否可以使用实验数据在python中达到相同的效果?
我现在的代码和输出:
import random
x = [i for i in range(1,11,1)]
y1 = random.sample(range(100, 1000), 10)
y2 = random.sample(range(0, 10), 10)
plt.plot(x,y1,'-r')
plt.plot(x,y2,'-g')
plt.legend(['y1','y2'])
plt.show()
有一个非常简单的解决方案,就是只使用子图
import random
import matplotlib.pyplot as plt
x = [i for i in range(1,11,1)]
y1 = random.sample(range(100, 1000), 10)
y2 = random.sample(range(0, 10), 10)
ax1 = plt.subplot(211)
plt.plot(x,y1,'-r')
ax2 = plt.subplot(212,sharex=ax1)
plt.plot(x,y2,'-g')
ax1.get_shared_x_axes().join(ax1, ax2)
#make x axis on upper invisible
plt.setp(ax1.get_xaxis(), visible=False)
ax1.legend(['y1'])
ax2.legend(['y2'])
plt.show()
您可以使用以下方法从上部子图中删除底边界,从下部子图中删除上边界:
ax1.spines['bottom'].set_visible(False)
ax2.spines['top'].set_visible(False)
GridSpec可能会帮助您删除边距,但是我想应该有一种更简单的方法来删除两个子图之间的距离