Tuesday, 15 July 2014

python - Annotating dimensions in matplotlib -



python - Annotating dimensions in matplotlib -

i want annotate lengths in matplotlib figure. example, distance between points , b.

for this, think can either utilize annotate , figure out how supply start , end positions of arrow. or, utilize arrow , label point.

i tried utilize latter, can't figure out how 2-headed arrow:

from pylab import * in [0, 1]: j in [0, 1]: plot(i, j, 'rx') axis([-1, 2, -1, 2]) arrow(0.1, 0, 0, 1, length_includes_head=true, head_width=.03) # draws 1-headed arrow show()

how create 2-headed arrow? improve still, there (simpler) way of marking dimensions in matplotlib figures?

you can alter style of arrow using arrowstyle property, example

ax.annotate(..., arrowprops=dict(arrowstyle='<->'))

gives double headed arrow.

a finish illustration can found here 3rd way downwards page possible different styles.

as 'better' way of marking dimensions on plots cannot think of off top of head.

edit: here's finish illustration can utilize if it's helpful

import matplotlib.pyplot plt import numpy np def annotate_dim(ax,xyfrom,xyto,text=none): if text none: text = str(np.sqrt( (xyfrom[0]-xyto[0])**2 + (xyfrom[1]-xyto[1])**2 )) ax.annotate("",xyfrom,xyto,arrowprops=dict(arrowstyle='<->')) ax.text((xyto[0]+xyfrom[0])/2,(xyto[1]+xyfrom[1])/2,text,fontsize=16) x = np.linspace(0,2*np.pi,100) plt.plot(x,np.sin(x)) annotate_dim(plt.gca(),[0,0],[np.pi,0],'$\pi$') plt.show()

python matplotlib

No comments:

Post a Comment