[英]Change y-axis ticks only on RHS
I'm looking to change the values at which there are ticks, for the right hand side y-axis (but NOT the left).我正在寻找更改右侧 y 轴(但不是左侧)的刻度值。
Here's a MWE:这是一个 MWE:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [1, 2, 3])
ax.tick_params(axis="y", which="both", labelright="on")
ax.tick_params(axis="y", which="both", labelleft="on", labelright="on")
ax.set_yticks([1, 2, 3])
plt.show()
With the set_yticks
command I get the behavior on both axes:使用
set_yticks
命令我得到了两个轴上的行为:
For reference, here are the default ticks:作为参考,以下是默认刻度:
I'm looking for something like我正在寻找类似的东西
ax.set_yticks([1, 2, 3], which='right')
so that I can have ticks every 0.25 on the left axis, and ticks every 1 on the right axis.这样我就可以在左轴上每 0.25 个刻度,在右轴上每 1 个刻度。 Any ideas?
有任何想法吗?
One possible solution is to "twin" the axis:一种可能的解决方案是“孪生”轴:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(1, 3)
y = np.linspace(1, 3)
fig, ax1 = plt.subplots()
ax1.plot(x, y)
ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis
# set the new ylimits to the be the same as the other
ax2.set_ylim(ax1.get_ylim())
ax2.set_yticks([1, 2, 3])
fig.tight_layout() # otherwise the right y-label is slightly clipped
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.