在同一地块中添加两个yticklabel

发布于 2025-01-31 22:51:04 字数 267 浏览 0 评论 0 原文

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

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

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

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

I have a plot in matplotlib with a custom text displayed in the yticks positions at the left side using:

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

Now, i want to add, at the right side of the plot, another set of tick labels, at the same y positions, but with different text labels.

Any suggestions on how to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橪书 2025-02-07 22:51:04

这是以下 matplotlib示例

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()

This is an adaption of the following 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()

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文