How to draw a line outside of an axis in matplotlib (in figure coordinates)?


Question

Matplotlib has a function that writes text in figure coordinates (.figtext())

Is there a way to do the same but for drawing lines?

In particular my goal is to draw lines to group some ticks on the y-axis together.

1
14
2/16/2011 8:19:29 PM

Accepted Answer

This will do it:

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()

But if you want to align with ticks, then why not use plot coordinates?

16
2/16/2011 9:35:34 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon