首页 > 解决方案 > 如何在 python 中使用相同的轴在同一个图形上绘制多个信号

问题描述

我在同一图中手动表示了 22 个信号

plt.plot(components + [0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1, 1.05, 1.1, 1.15, 1.2, 1.25, 1.3, 1.35, 1.4, 1.45, 1.5, 1.55])
plt.yticks([0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1, 1.05, 1.1, 1.15, 1.2, 1.25, 1.3, 1.35, 1.4, 1.45, 1.5, 1.55], ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21'])
plt.xlim([0, 15000])
plt.show()

在此处输入图像描述

但是当我尝试自动执行时。我没有成功。

for i in range(0, 21):
    plt.plot(components[:, i])

在此处输入图像描述

请问有没有人可以帮助我

标签: python

解决方案


我建议你看看这里的例子https://matplotlib.org/faq/howto_faq.html?highlight=multiple%20plots#multiple-y-axis-scales

源代码是:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(111)
t = np.arange(0.01, 10.0, 0.01)
s1 = np.exp(t)
ax1.plot(t, s1, 'b-')
ax1.set_xlabel('time (s)')
ax1.set_ylabel('exp')

ax2 = ax1.twinx()
s2 = np.sin(2*np.pi*t)
ax2.plot(t, s2, 'r.')
ax2.set_ylabel('sin')
plt.show()

您可以看到他们使用单独ax2的,但最终结果是将其放在一个图表上。.twinx()您可以通过或选择在绘图之间保持相同的 x 比例或相同的 y 比例.twiny()


推荐阅读