将两个 yticklabels 添加到同一图中,两者中的文本不同

Adding two yticklabels to the same plot with different text in both

我在 matplotlib 中有一个图,在左侧的 yticks 位置显示自定义文本,使用:

axs[0].set_yticks(list_with_tick_positions)
axs[0].set_yticklabels(list_with_text_labels_for_those_ticks)

现在,我想在图的右侧添加另一组刻度标签,在相同的 y 位置,但具有不同的文本标签。

关于如何实现这一点有什么建议吗?

这是对以下matplotlib example的改编:

import numpy as np
import matplotlib.pyplot as plt

fig, ax1 = plt.subplots()

list_with_tick_positions = [0, 1, 2, 3]
list_with_text_labels_for_those_ticks = ["a", "b", "c", "d"]

color = 'tab:red'
ax1.set_xlabel('x axis')
ax1.set_ylabel('y axis 1', color=color)
ax1.tick_params(axis='y', labelcolor=color)
ax1.set_yticks(list_with_tick_positions)
ax1.set_yticklabels(list_with_text_labels_for_those_ticks)

list_with_text_labels_for_those_ticks = ["A", "B", "C", "D"]
ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis

color = 'tab:blue'
ax2.set_ylabel('y axis 2', color=color)
ax2.tick_params(axis='y', labelcolor=color)
ax2.set_yticks(list_with_tick_positions)
ax2.set_yticklabels(list_with_text_labels_for_those_ticks)

fig.tight_layout()  # otherwise the right y-label is slightly clipped
plt.show()