在同一图中创建 2 个具有共同 y 范围 Python 的线图

Create 2 line plots in the same figure with common y range Python

我想在同一个图中绘制两条线图:

import matplotlib.pyplot as plt

x1 = [0.53884, 0.53878, 0.53898, 0.53662, 0.53748, 0.5398, 0.53894, 0.53732, 0.53744, 0.54052, 0.54402, 0.54178] 
x2 = [54.9, 54.9, 54.9, 54.9, 54.9, 54.9, 54.9, 54.9, 54.9, 54.9, 54.9, 54.9]

x = range(len(x1))

top_lim = max( max(x1), max(x2) ) + 0.001
bottom_lim = min( min(x1), min(x2) ) - 0.001
plt.ylim(bottom_lim, top_lim)

plt.plot(x, x1)
plt.plot(x, x2 ,color='r')

plt.show()

然而,这给了我一个空白图。 我怎样才能得到正确的情节?

所以你的代码中的问题是 ylim 你可以尝试以下操作:

import matplotlib.pyplot as plt

x1 = [0.53884, 0.53878, 0.53898, 0.53662, 0.53748, 0.5398, 0.53894, 0.53732, 0.53744, 0.54052, 0.54402, 0.54178]
x2 = [54.9, 54.9, 54.9, 54.9, 54.9, 54.9, 54.9, 54.9, 54.9, 54.9, 54.9, 54.9]

x = list(range(len(x1)))

top_lim = max( max(x1), max(x2) ) + 5
bottom_lim = min( min(x1), min(x2) ) - 5
plt.ylim(bottom_lim, top_lim)

plt.plot(x, x1)
plt.plot(x, x2 ,color='r')

plt.show()

由于 x1x2 之间的值然后两条线都位于图的边缘,因此您看不到它。