我有一个简单的线状图,需要将y轴刻度从(默认)左侧移到右侧。对如何做到这一点有什么想法吗?
发布于 2012-04-28 01:11:43
使用ax.yaxis.tick_right()
例如:
from matplotlib import pyplot as plt
f = plt.figure()
ax = f.add_subplot(111)
ax.yaxis.tick_right()
plt.plot([2,3,4,5])
plt.show()
发布于 2012-12-05 20:40:12
对于右侧标签,请使用ax.yaxis.set_label_position("right")
,即:
f = plt.figure()
ax = f.add_subplot(111)
ax.yaxis.tick_right()
ax.yaxis.set_label_position("right")
plt.plot([2,3,4,5])
ax.set_xlabel("$x$ /mm")
ax.set_ylabel("$y$ /mm")
plt.show()
发布于 2013-12-10 05:39:24
华金的答案是有效的,但有一个副作用,那就是从轴的左侧移除刻度。要解决此问题,请通过调用set_ticks_position('both')
来跟踪tick_right()
。修改后的示例:
from matplotlib import pyplot as plt
f = plt.figure()
ax = f.add_subplot(111)
ax.yaxis.tick_right()
ax.yaxis.set_ticks_position('both')
plt.plot([2,3,4,5])
plt.show()
结果是绘图的两侧带有刻度线,但刻度线标签位于右侧。
https://stackoverflow.com/questions/10354397
复制相似问题