Python AxisSubplot,将显示坐标转换为实际坐标

Python AxisSubplot,将显示坐标转换为实际坐标,python,python-2.7,matplotlib,plot,Python,Python 2.7,Matplotlib,Plot,我试图使用AxisSubplot对象将一些显示坐标转换为真实坐标,以便在绘图上绘制形状。问题是我在任何地方都找不到文档或AxisSubplot,我看到了,但无法找出AxisSubplot对象中到底包含了什么 我的绘图坐标按时间x高度显示,因此可能需要一组坐标 [ ['03:42:01', 2.3] , ['03:42:06', 3.4] , ...] 在我的显示功能中,我将子地块的轴格式化如下: fig.get_xaxis().set_major_locator(mpl.dates.A

我试图使用
AxisSubplot
对象将一些显示坐标转换为真实坐标,以便在绘图上绘制形状。问题是我在任何地方都找不到文档或
AxisSubplot
,我看到了,但无法找出
AxisSubplot
对象中到底包含了什么

我的绘图坐标按时间x高度显示,因此可能需要一组坐标

[ ['03:42:01', 2.3] , ['03:42:06', 3.4] , ...]
在我的显示功能中,我将子地块的轴格式化如下:

    fig.get_xaxis().set_major_locator(mpl.dates.AutoDateLocator())
    fig.get_xaxis().set_major_formatter(mpl.dates.DateFormatter('%H:%M:%S'))
现在,当我想使用上面的集合显示多边形时,如何将该日期字符串转换为绘图坐标

    points = [['03:42:01', 1], ['03:43:01', 2.1], ['03:21:01', 1]]
    polygon = plt.Polygon(points)
    fig.add_patch(polygon)
当然,这给了我一个错误
ValueError:invalid literal for float():03:42:01
。有人知道怎么做吗?下面是一个打印轴的示例:


您似乎有两个问题:

  • 您找不到
    AxesSubplot
    对象的文档

    这是因为它只在运行时创建。它继承自。您将在中找到更多详细信息

  • 您希望以日期/时间作为x坐标打印多边形:

    为此,matplotlib需要知道坐标表示日期/时间。有一些绘图函数可以处理日期时间对象(例如,
    plot\u date
    ),但通常您必须注意这一点

    Matplotlib使用自己的内部日期表示形式(浮动天数),但在
    Matplotlib.dates
    模块中提供了必要的转换函数。在您的情况下,您可以按如下方式使用它:

    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()
    
    结果:


  • 这至少是两个问题。我建议用一个10x10的随机样本数据数组制作一个最小的可运行示例。对于多边形,您正在处理从日期到浮动的
    变换--matplotlib有一个我们通常不会想到的标准同构,但看起来您会想到。因此,制作示例并查看,其中也包括轴/图形/数据差异。@SyntacticFructose这能解决您的问题/回答您的问题吗?