y 轴刻度固定为第二个 y 轴刻度在特定数据点由 matplotlib

y axis ticks fixed to second y axis ticks at certain data point by matplotlib

我得到了 2 个 y 值列表。

正如您在下图中看到的红线,y1 刻度中的 6 和 y2 刻度中的 30 需要处于同一水平水平(或看起来相同)。

是否可以使用 maplotlib 实现此目的?

 list_y1 = [
 7.31, 7.30,7.29, 7.29, 7.44, 8.05, 7.46, 7.79, 8.35, 8.95, 
 8.95, 8.37, 7.51, 8.34, 8.39, 8.41, 8.46, 8.69, 8.52, 8.51, 
 8.46, 8.97, 9.35, 9.32, 9.24, 9.46, 9.22, 9.16, 9.08, 9.00, 
 8.82, 8.57, 8.66, 8.79, 8.47, 8.58, 8.62, 8.66, 9.19, 9.20, 
 9.13, 9.43 ]

list_y2 = [ 
 30.37, 30.51, 30.62, 30.52, 31.31, 33.82, 31.41, 32.78, 35.29, 37.85, 
 37.85, 35.36, 31.81, 35.35, 35.56, 35.61, 35.94, 36.36, 36.10, 35.81, 
 35.35, 37.14, 38.49, 38.34, 38.04, 38.90, 37.84, 37.58, 37.25, 36.90, 
 36.17, 35.12, 35.53, 36.06, 34.73, 35.15, 35.30, 34.64, 36.76, 36.81, 
 36.54, 37.71 ]

list_x = [
 100Q1, 100Q2, 100Q3, 100Q4, 101Q1,101Q2, 101Q3, 101Q4, 102Q1, 102Q2,
 102Q3, 102Q4, 103Q1, 103Q2, 103Q3,103Q4, 104Q1, 104Q2, 104Q3, 104Q4,
 105Q1, 105Q2, 105Q3, 105Q4, 106Q1,106Q2, 106Q3, 106Q4, 107Q1, 107Q2,
 107Q3, 107Q4, 108Q1, 108Q2, 108Q3,108Q4, 109Q1, 109Q2, 109Q3, 109Q4,
 110Q1, 110Q2 ]

最简单的方法是将两个轴的 y 限制设置为 (4, 10)(25, 40),其中:

ax1.set_ylim(4, 10)
ax2.set_ylim(25, 40)

完整代码

import matplotlib.pyplot as plt
from matplotlib import cm

list_y1 = [7.31, 7.30, 7.29, 7.29, 7.44, 8.05, 7.46, 7.79, 8.35, 8.95,
           8.95, 8.37, 7.51, 8.34, 8.39, 8.41, 8.46, 8.69, 8.52, 8.51,
           8.46, 8.97, 9.35, 9.32, 9.24, 9.46, 9.22, 9.16, 9.08, 9.00,
           8.82, 8.57, 8.66, 8.79, 8.47, 8.58, 8.62, 8.66, 9.19, 9.20,
           9.13, 9.43]

list_y2 = [30.37, 30.51, 30.62, 30.52, 31.31, 33.82, 31.41, 32.78, 35.29, 37.85,
           37.85, 35.36, 31.81, 35.35, 35.56, 35.61, 35.94, 36.36, 36.10, 35.81,
           35.35, 37.14, 38.49, 38.34, 38.04, 38.90, 37.84, 37.58, 37.25, 36.90,
           36.17, 35.12, 35.53, 36.06, 34.73, 35.15, 35.30, 34.64, 36.76, 36.81,
           36.54, 37.71]

list_x = ['100Q1', '100Q2', '100Q3', '100Q4', '101Q1', '101Q2', '101Q3', '101Q4', '102Q1', '102Q2',
          '102Q3', '102Q4', '103Q1', '103Q2', '103Q3', '103Q4', '104Q1', '104Q2', '104Q3', '104Q4',
          '105Q1', '105Q2', '105Q3', '105Q4', '106Q1', '106Q2', '106Q3', '106Q4', '107Q1', '107Q2',
          '107Q3', '107Q4', '108Q1', '108Q2', '108Q3', '108Q4', '109Q1', '109Q2', '109Q3', '109Q4',
          '110Q1', '110Q2']


fig, ax1 = plt.subplots()
ax2 = ax1.twinx()

cmap = cm.get_cmap('tab10')

ax1.plot(list_x, list_y1, color = cmap(0), linestyle = '--', linewidth = 2)
ax2.plot(list_x, list_y2, color = cmap(0.1), linestyle = '-', linewidth = 2)
plt.setp(ax1.xaxis.get_majorticklabels(), rotation = 90)
ax1.axhline(y = 6, color = 'red', linewidth = 0.5)

ax1.set_ylim(4, 10)
ax2.set_ylim(25, 40)

plt.show()

情节


一种更复杂的方法是遵循this example并定义2个函数从一个轴传递到另一个轴。请注意,第二个轴是 被动 :我总是在第一个轴上绘图,因此我需要转换 list_y2.

的值
def left2right(x):
    return 10/4*x + 15

def right2left(x):
    return 4/10*x - 6


fig, ax1 = plt.subplots(figsize = (10, 5))
ax2 = ax1.secondary_yaxis('right', functions = (left2right, right2left))


ax1.plot(list_x, list_y1, linestyle = '--', linewidth = 2)
ax1.plot(list_x, list(map(right2left, list_y2)), linestyle = '-', linewidth = 2)
plt.setp(ax1.xaxis.get_majorticklabels(), rotation = 90)
ax1.axhline(y = 6, color = 'red', linewidth = 0.5)

ax1.set_ylim(4, 10)
ax2.set_ylim(25, 40)

plt.show()

因为,无论如何,你必须设置轴限制,因为第二种方法更复杂,我建议第一种。