简体   繁体   English

使用 sharey= 删除子图上的 y 轴刻度标签<other_axis>

[英]Remove y-axis tick labels on subplot with sharey=<other_axis>

I'm trying to create a multi-plot figure with subplots on python 3.7 and matplotlib.我正在尝试在 python 3.7 和 matplotlib 上创建一个带有子图的多图图。 I have the plot set up where the first subplot (ax1) has ax1.set_ylim() and ax2=plt.subplot(122, sharey=ax1)我在第一ax1.set_ylim()图 (ax1) 有ax1.set_ylim()ax2=plt.subplot(122, sharey=ax1)地方设置了情节

However, I would like to turn off the y-tick labels on ax2.但是,我想关闭 ax2 上的 y-tick 标签。 When I do so via ax2.set_yticks([]) , the tickmarks on both subplots go away (probably because of the sharey ).当我通过ax2.set_yticks([])这样做时,两个子图上的刻度线都会消失(可能是因为sharey )。 See example below请参阅下面的示例

import matplotlib.pyplot as plt
import numpy as np

# create data 
data1 = np.random.rand(10, 10)
data2 = np.random.rand(10, 10)

# plot data
fig = plt.figure(figsize=(8, 4))
ax1 = plt.subplot(121)
ax1.imshow(data1)
ax1.set_ylim(0,5)

ax2 = plt.subplot(122, sharex=ax1, sharey=ax1)
ax2.imshow(data2)
ax2.set_yticks([])

plt.show()

Without setting ax2.set_yticks([]) , I get the following:没有设置ax2.set_yticks([]) ,我得到以下信息:带yticks But when I set ax2.set_yticks([]) , both axes go away:但是当我设置ax2.set_yticks([]) ,两个轴都会消失:没有标记

Is there a better way to do this?有一个更好的方法吗? I'd like to have the y-axis labels on the left plot but not on the right.我想在左侧图上有 y 轴标签,但不在右侧。

You can try this also:你也可以试试这个:

import matplotlib.pyplot as plt
import numpy as np

# create data 
data1 = np.random.rand(10, 10)
data2 = np.random.rand(10, 10)

# plot data
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4), sharey=True)
ax1.imshow(data1)
ax1.set_ylim(0,5)

ax2.imshow(data2)

plt.show()

Output:输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM