python - AxisSubplot , convert display coordiantes into real coordiantes -
i'm trying use axissubplot
object convert display coordinates real coordinates drawing shapes onto plot. problem can't find documentation or axissubplot
anywhere , see axis can't figure out on earth contained in axissubplot
object.
my plot coordinates displayed time x altitude, display wise set may
[ ['03:42:01', 2.3] , ['03:42:06', 3.4] , ...]
in display function format axis of subplot so:
fig.get_xaxis().set_major_locator(mpl.dates.autodatelocator()) fig.get_xaxis().set_major_formatter(mpl.dates.dateformatter('%h:%m:%s'))
now when want display polygon using example set above, how can convert date string plot cooridnates?
points = [['03:42:01', 1], ['03:43:01', 2.1], ['03:21:01', 1]] polygon = plt.polygon(points) fig.add_patch(polygon)
and of course gives me error valueerror: invalid literal float(): 03:42:01
. know how this? here's example of plot axis looks like:
you seem have 2 issues:
you can't find documentation of
axessubplot
object.that because created @ runtime. inherits
subplotbase
. you'll find more details in this answer (to same question).you plot polygon dates/times x coordinates:
for matplotlib needs know coordinates represent dates/times. there plotting functions can handle datetime objects (e.g.
plot_date
), in general have take care of that.matplotlib uses own internal representation of dates (floating number of days), provides necessary conversion functions in
matplotlib.dates
module. in case use follows:import matplotlib.pyplot plt import numpy np import matplotlib.dates mdates datetime import datetime # original data p = [['03:42:01', 1], ['03:43:01', 2.1], ['03:21:01', 1]] # convert points p_converted = np.array([[mdates.date2num(datetime.strptime(x, '%h:%m:%s')), y] x,y in p]) # create figure , axis fig, ax = plt.subplots(1) # build polygon , add polygon = plt.polygon(p_converted) ax.add_patch(polygon) # set proper axis limits (with 5 minutes margins in x, 0.5 in y) x_mrgn = 5/60./24. y_mrgn = 0.5 ax.set_xlim(p_converted[:,0].min() - x_mrgn, p_converted[:,0].max() + x_mrgn) ax.set_ylim(p_converted[:,1].min() - y_mrgn, p_converted[:,1].max() + y_mrgn) # assign date locators , formatters ax.xaxis.set_major_locator(mdates.autodatelocator()) ax.xaxis.set_major_formatter(mdates.dateformatter('%h:%m:%s')) # show figure plt.show()
result:
Comments
Post a Comment