下面是我绘制函数的代码,我需要将“X”和“Y”标签移到第一个象限,按照惯例,它们被放置在相应的箭头附近。这是怎么做到的?在
import pylab as p
import numpy as n
from mpl_toolkits.axes_grid import axislines
def cubic(x) :
return x**3 + 6*x
def set_axes():
fig = p.figure(1)
ax = axislines.SubplotZero(fig, 111)
fig.add_subplot(ax)
for direction in ['xzero', 'yzero']:
ax.axis[direction].set_axisline_style('->', size=2)
ax.axis[direction].set_visible(True)
for direction in ['right', 'top', 'left', 'bottom']:
ax.axis[direction].set_visible(False)
ax.axis['xzero'].set_label('X')
ax.axis['yzero'].set_label('Y')
ax.axis['yzero'].major_ticklabels.set_axis_direction('right')
ax.axis['yzero'].set_axislabel_direction('+')
ax.axis['yzero'].label.set_rotation(-90)
ax.axis['yzero'].label.set_va('center')
set_axes()
X = n.linspace(-15,15,100)
Y = cubic(X)
p.plot(X, Y)
p.xlim(-5.0, 5.0)
p.ylim(-15.0, 15.0)
p.xticks(n.linspace(-5, 5, 11, endpoint=True))
p.grid(True)
p.show()
通常,要更改一个轴的标签位置(例如
ax.xaxis
)的位置,可以执行axis.label.set_position(xy)
。也可以只设置一个坐标,例如'轴x轴集(1) `。在在你的情况下,应该是:
然而,
axislines
(以及axisartist
或axes_grid
中的任何其他内容)是一个有些过时的模块(这就是axes_grid1
存在的原因)。在某些情况下,它不能正确地对事物进行子类化。所以,当我们试图设置标签的x和y位置时,没有任何变化!在一个快速的解决方法是使用
ax.annotate
在箭头的末端放置标签。不过,让我们先尝试用一种不同的方式来绘制(之后,我们将回到annotate
)的地方。在现在,你最好使用新的脊椎功能来完成你想要完成的任务。在
将x轴和y轴设置为“归零”非常简单:
^{pr2}$但是,我们仍然需要漂亮的箭头装饰。这有点复杂,但它只是两个调用,用适当的参数进行注释。在
(箭头的宽度由文本大小(或}会使箭头更宽一些。)
arrowprops
的可选参数)控制,因此如果愿意,将size=16
指定到{此时,最简单的方法是将“X”和“Y”标签添加为注释的一部分,不过设置它们的位置也可以。在
如果我们只传入一个label作为第一个参数而不是一个空字符串(并稍微改变一下对齐方式),我们会在箭头的末端得到漂亮的标签:
只需稍微多做一点工作(直接访问脊椎的变换),就可以generalize the use of annotate处理任何类型的脊椎对齐(例如“掉落”的脊椎等)。在
不管怎样,希望这能有所帮助。如果你愿意,你也可以get fancier with it。在
相关问题 更多 >
编程相关推荐