简体   繁体   中英

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

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM