Jas*_*pel 101 python matplotlib
我有一个简单的线图,需要将y轴刻度从绘图的(默认)左侧移动到右侧.有关如何做到这一点的任何想法?
joa*_*uin 173
使用 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()
Run Code Online (Sandbox Code Playgroud)
Die*_*ich 92
对于正确的标签使用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()
Run Code Online (Sandbox Code Playgroud)
小智 52
joaquin的回答是有效的,但是具有从轴的左侧去除刻度的副作用.要解决此问题,请跟进tick_right()
电话set_ticks_position('both')
.修改后的例子:
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()
Run Code Online (Sandbox Code Playgroud)
结果是两侧都有刻度的图,但右侧是刻度标签.
Tit*_*nne 20
只是有人问(就像我做的那样),当使用subplot2grid时,这也是可能的.例如:
import matplotlib.pyplot as plt
plt.subplot2grid((3,2), (0,1), rowspan=3)
plt.plot([2,3,4,5])
plt.tick_params(axis='y', which='both', labelleft='off', labelright='on')
plt.show()
Run Code Online (Sandbox Code Playgroud)
它会显示:
归档时间: |
|
查看次数: |
88950 次 |
最近记录: |