如何在matplotlib中绘制一条轴外的线(图中坐标)?

时间:2011-02-16 20:19:29

标签: python matplotlib plot

Matplotlib有一个函数,用于在图形坐标(.figtext())

中写入文本

有没有办法对绘画线做同样的事情?

特别是我的目标是绘制线条,将y轴上的一些刻度组合在一起。

1 个答案:

答案 0 :(得分:15)

这样做:

from matplotlib import pyplot, lines
import numpy

x = numpy.linspace(0,10,100)
y = numpy.sin(x)*(1+x)

fig = pyplot.figure()
ax = pyplot.subplot(111)
ax.plot(x,y,label='a')

# new clear axis overlay with 0-1 limits
ax2 = pyplot.axes([0,0,1,1], axisbg=(1,1,1,0))

x,y = numpy.array([[0.05, 0.1, 0.9], [0.05, 0.5, 0.9]])
line = lines.Line2D(x, y, lw=5., color='r', alpha=0.4)
ax2.add_line(line)

pyplot.show()

但是如果你想与刻度线对齐,那么为什么不使用绘图坐标?