Python:如何使用自定义刻度在 RHS 上仅添加第二个 y 轴?
Python: how can I add only a second y axis on the RHS with custom ticks?
我有一个不想碰的情节。
我只想在右侧添加第二个 y 轴,它有自己独特的自定义刻度值。根本不想改剧情
剧情使用imshow:
plt.imshow(image)
而ny是y轴上的最大点数
我想添加第二个 y 轴,如下所示:
ax_ft = plt.twinx()
custom_yticks = [0, 2000, 4000, 6000, 8000, 10000, 12000]
ax_ft.yticks([0,ny/6,2*ny/6,3*ny/6,4*ny/6,5*ny/6,ny], custom_yticks)
ax_ft.set_ylabel("Height [feet]", fontsize=18)
但它 returns 错误:“Axes 对象没有属性 'yticks.'
那么我如何在 RHS 上添加带有这 7 个刻度值的 y 轴呢?我认为 twinx() 会让 ax_ft 像 plt 一样工作,我可以创建自定义轴吗?
这是 MCVE 的一些设置代码:
img = np.random.randint(0, 255, size=(100, 150))
ny = img.shape[0]
custom_labels = [0, 2000, 4000, 6000, 8000, 10000, 12000]
custom_locs = np.linspace(0, ny - 1, 7)
如果您想在图像边缘而不是像素中心之间移动,另一种方法是将 custom_locs
定义为 np.linspace(0, ny, 7) - 0.5
。
有几种方法可以解决这个问题。首先是尝试使用pyplot:
plt.imshow(img)
plt.twinx()
plt.yticks(custom_locs, custom_labels)
plt.ylabel("Height [feet]", fontsize=18)
请注意,坐标轴的界限并未绑定在一起,因此绘图并未按照您的实际要求进行。诀窍是使用 Axes.secondary_yaxis
。那时,你正在使用 object-oriented API,所以我建议从一开始就使用它:
fig, ax = plt.subplots(constrained_layout=True)
ax.imshow(img)
ax1 = ax.secondary_yaxis('right')
ax1.set_yticks(custom_locs)
ax1.set_yticklabels(custom_labels)
ax1.set_ylabel("Height [feet]", fontsize=18)
请注意,对您的尝试的明确回答在 ``.
文档的 Notes 部分给出
Calling this function with no arguments (e.g. yticks()
) is the pyplot equivalent of calling get_yticks
and get_yticklabels
on the current axes. Calling this function with arguments is the pyplot equivalent of calling set_yticks
and set_yticklabels
on the current axes.
如果您希望轴沿与主轴相反的方向上升,您可以执行以下两项操作之一。您可以更改位置和标签,或者可以将 functions
参数用于 secondary_yaxis
:
ax1 = ax.secondary_yaxis('right', functions=(lambda x: len(img) - x - 1, lambda x: len(img) - x - 1))
我有一个不想碰的情节。 我只想在右侧添加第二个 y 轴,它有自己独特的自定义刻度值。根本不想改剧情
剧情使用imshow:
plt.imshow(image)
而ny是y轴上的最大点数
我想添加第二个 y 轴,如下所示:
ax_ft = plt.twinx()
custom_yticks = [0, 2000, 4000, 6000, 8000, 10000, 12000]
ax_ft.yticks([0,ny/6,2*ny/6,3*ny/6,4*ny/6,5*ny/6,ny], custom_yticks)
ax_ft.set_ylabel("Height [feet]", fontsize=18)
但它 returns 错误:“Axes 对象没有属性 'yticks.' 那么我如何在 RHS 上添加带有这 7 个刻度值的 y 轴呢?我认为 twinx() 会让 ax_ft 像 plt 一样工作,我可以创建自定义轴吗?
这是 MCVE 的一些设置代码:
img = np.random.randint(0, 255, size=(100, 150))
ny = img.shape[0]
custom_labels = [0, 2000, 4000, 6000, 8000, 10000, 12000]
custom_locs = np.linspace(0, ny - 1, 7)
如果您想在图像边缘而不是像素中心之间移动,另一种方法是将 custom_locs
定义为 np.linspace(0, ny, 7) - 0.5
。
有几种方法可以解决这个问题。首先是尝试使用pyplot:
plt.imshow(img)
plt.twinx()
plt.yticks(custom_locs, custom_labels)
plt.ylabel("Height [feet]", fontsize=18)
请注意,坐标轴的界限并未绑定在一起,因此绘图并未按照您的实际要求进行。诀窍是使用 Axes.secondary_yaxis
。那时,你正在使用 object-oriented API,所以我建议从一开始就使用它:
fig, ax = plt.subplots(constrained_layout=True)
ax.imshow(img)
ax1 = ax.secondary_yaxis('right')
ax1.set_yticks(custom_locs)
ax1.set_yticklabels(custom_labels)
ax1.set_ylabel("Height [feet]", fontsize=18)
请注意,对您的尝试的明确回答在 ``.
文档的 Notes 部分给出Calling this function with no arguments (e.g.
yticks()
) is the pyplot equivalent of callingget_yticks
andget_yticklabels
on the current axes. Calling this function with arguments is the pyplot equivalent of callingset_yticks
andset_yticklabels
on the current axes.
如果您希望轴沿与主轴相反的方向上升,您可以执行以下两项操作之一。您可以更改位置和标签,或者可以将 functions
参数用于 secondary_yaxis
:
ax1 = ax.secondary_yaxis('right', functions=(lambda x: len(img) - x - 1, lambda x: len(img) - x - 1))