下面是我绘制函数的代码,我需要将"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()
Run Code Online (Sandbox Code Playgroud)
通常,要改变轴的(例如ax.xaxis)标签位置,您可以这样做axis.label.set_position(xy).或者你可以设置一个坐标,例如'ax.xaxis.set_x(1)`.
在你的情况下,它将是:
ax['xzero'].label.set_x(1)
ax['yzero'].label.set_y(1)
Run Code Online (Sandbox Code Playgroud)
但是,axislines(和axisartistor中的任何其他内容axes_grid)是一个有点过时的模块(这就是为什么axes_grid1存在).在某些情况下,它不会正确地对事物进行子类化.因此,当我们尝试设置标签的x和y位置时,没有任何变化!
一个快速的解决方法是用于ax.annotate在箭头的末端放置标签.但是,让我们先尝试以不同的方式制作情节(之后我们将最终回归annotate).
现在,您最好使用新的spines功能来完成您想要完成的任务.
将x和y轴设置为"归零"非常简单:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
for spine in ['left', 'bottom']:
ax.spines[spine].set_position('zero')
# Hide the other spines...
for spine in ['right', 'top']:
ax.spines[spine].set_color('none')
ax.axis([-4, 10, -4, 10])
ax.grid()
plt.show()
Run Code Online (Sandbox Code Playgroud)

但是,我们仍然需要漂亮的箭头装饰.这有点复杂,但它只是两个使用approriate参数进行注释的调用.
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
#-- Set axis spines at 0
for spine in ['left', 'bottom']:
ax.spines[spine].set_position('zero')
# Hide the other spines...
for spine in ['right', 'top']:
ax.spines[spine].set_color('none')
#-- Decorate the spins
arrow_length = 20 # In points
# X-axis arrow
ax.annotate('', xy=(1, 0), xycoords=('axes fraction', 'data'),
xytext=(arrow_length, 0), textcoords='offset points',
arrowprops=dict(arrowstyle='<|-', fc='black'))
# Y-axis arrow
ax.annotate('', xy=(0, 1), xycoords=('data', 'axes fraction'),
xytext=(0, arrow_length), textcoords='offset points',
arrowprops=dict(arrowstyle='<|-', fc='black'))
#-- Plot
ax.axis([-4, 10, -4, 10])
ax.grid()
plt.show()
Run Code Online (Sandbox Code Playgroud)

(箭头的宽度由文本大小(或其可选参数arrowprops)控制,因此如果您愿意,指定类似size=16的annotate内容会使箭头更宽一些.)
此时,最简单的方法是将"X"和"Y"标签添加为注释的一部分,尽管设置它们的位置也会起作用.
如果我们只是传入一个标签作为注释的第一个参数而不是一个空字符串(并稍微更改一下),我们将在箭头的末尾得到漂亮的标签:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
#-- Set axis spines at 0
for spine in ['left', 'bottom']:
ax.spines[spine].set_position('zero')
# Hide the other spines...
for spine in ['right', 'top']:
ax.spines[spine].set_color('none')
#-- Decorate the spins
arrow_length = 20 # In points
# X-axis arrow
ax.annotate('X', xy=(1, 0), xycoords=('axes fraction', 'data'),
xytext=(arrow_length, 0), textcoords='offset points',
ha='left', va='center',
arrowprops=dict(arrowstyle='<|-', fc='black'))
# Y-axis arrow
ax.annotate('Y', xy=(0, 1), xycoords=('data', 'axes fraction'),
xytext=(0, arrow_length), textcoords='offset points',
ha='center', va='bottom',
arrowprops=dict(arrowstyle='<|-', fc='black'))
#-- Plot
ax.axis([-4, 10, -4, 10])
ax.grid()
plt.show()
Run Code Online (Sandbox Code Playgroud)

只需要更多的工作(直接访问脊柱的变换),您可以概括使用注释来处理任何类型的脊柱对齐(例如"掉落"的脊柱等).
无论如何,希望有所帮助.如果你愿意的话,你也可以用它来获得更好的体验.
| 归档时间: |
|
| 查看次数: |
2988 次 |
| 最近记录: |