matplotlib - pyplot and semilogarithmic scale: how to draw a circle with transform -
i want have circle in plot made pyplot, need use log scale on x axis.
i do:
ax = plt.axes() point1 = plt.circle((x,y), x, color='r',clip_on=false, transform = ax.transaxes, alpha = .5) plt.xscale('log') current_fig = plt.gcf() current_fig.gca().add_artist(point1)
as see, want radius of circle equal x coordinate.
my problem if use, written here, transaxes
, circle circle (otherwise stretched on x , looks ellipse cut in half), x coordinate 0. if, on other hand, use transdata
instead of transaxes
, correct value x coordinate, circle stretched again , cut in half.
i don't mind stretching don't cutting, want @ least full ellipse.
any idea how obtain want?
probably easiest way use plot marker instead of circle
. example:
ax.plot(x,y,marker='o',ms=x*5,mfc=(1.,0.,0.,0.5),mec='none')
this give circle 'circular' , centred on correct x,y coordinate, although size bear no relationship x , y scales. if care centre position can mess around ms=
until looks right.
a more general way construct new composite transformation circle - should take @ this tutorial on transformations. basically, transformation figure data space constructed this:
transdata = transscale + (translimits + transaxes)
where transscale
handles nonlinear (e.g. logarithmic) scaling of data, translimits
maps x- , y-limits of data unit space of axes, , transaxes
maps corners of axes bounding box display space.
you want keep circle looking circle/ellipse (i.e. not distort according logarithmic scaling of x-axis), still want translate correct centre location in data coordinates. in order that, can construct scaled translation, combine translimits
, transaxes
:
from matplotlib.transforms import scaledtranslation ax = plt.axes(xscale='log') x,y = 10,0 ax.set_ylim(-11,11) ax.set_xlim(1e-11,1e11) # use axis scale tform figure out how far translate circ_offset = scaledtranslation(x,y,ax.transscale) # construct composite tform circ_tform = circ_offset + ax.translimits + ax.transaxes # create circle centred on origin, apply composite tform circ = plt.circle((0,0),x,color='r',alpha=0.5,transform=circ_tform) ax.add_artist(circ) plt.show()
obviously scaling in x-axis going bit weird , arbitrary - you'll need play around how construct transformation in order want.
Comments
Post a Comment