-1

I'm trying to use a AxisSubplot object to convert some display coordinates to real coordinates for drawing shapes onto the plot. The problem is I can't find documentation or AxisSubplot anywhere , I see Axis but can't figure out what on earth is contained in an AxisSubplot object.

My plot coordinates are displayed by Time x Altitude, so display wise a set may be

[ ['03:42:01', 2.3] , ['03:42:06', 3.4] , ...]

In my display function I format the axis of the subplot like so:

    fig.get_xaxis().set_major_locator(mpl.dates.AutoDateLocator())
    fig.get_xaxis().set_major_formatter(mpl.dates.DateFormatter('%H:%M:%S'))

Now when I want to display a polygon using for example the set above, how can I convert that date string to 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 this gives me the error ValueError: invalid literal for float(): 03:42:01. Would anyone know how to do this? Here's an example of what the plot axis looks like:

enter image description here

Cœur
  • 37,241
  • 25
  • 195
  • 267
Syntactic Fructose
  • 18,936
  • 23
  • 91
  • 177
  • 1
    This is at least two questions. I suggest making a minimal runnable example with, say, a 10x10 array of random sample data. For the polygon, you're dealing with the `transform` from dates to floats -- matplotlib has a standard isomorphism that we usually don't think about, but looks like you will. So make the example and check out the [transformations tutorial](http://matplotlib.org/users/transforms_tutorial.html?highlight=transformations), which covers axis/figure/data differences too. – cphlewis Jul 05 '15 at 21:59

1 Answers1

3

You seem to have two issues:

  1. You can't find the documentation of the AxesSubplot object.

    That is because it is only created at runtime. It inherits from SubplotBase. You'll find more details in this answer (to the same question).

  2. You would like to plot a polygon with dates/times as x coordinates:

    For that matplotlib needs to know that your coordinates represent dates/times. There are some plotting functions which can handle datetime objects (e.g. plot_date), but in general you have to take care of that.

    Matplotlib uses its own internal representation of dates (floating number of days), but provides the necessary conversion functions in the matplotlib.dates module. In your case you could use it as follows:

    import matplotlib.pyplot as plt
    import numpy as np
    import matplotlib.dates as mdates
    from datetime import datetime
    
    # your original data
    p = [['03:42:01', 1], ['03:43:01', 2.1], ['03:21:01', 1]]
    
    # convert the points 
    p_converted = np.array([[mdates.date2num(datetime.strptime(x, '%H:%M:%S')), y] 
            for x,y in p])
    
    # create a figure and an axis
    fig, ax = plt.subplots(1)
    
    # build the polygon and add it
    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 and formatters 
    ax.xaxis.set_major_locator(mdates.AutoDateLocator())
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))
    
    # show the figure
    plt.show()
    

    Result:

    enter image description here

Community
  • 1
  • 1
hitzg
  • 12,133
  • 52
  • 54