【问题标题】:matplotlib box on basemap map底图上的 matplotlib 框
【发布时间】:2014-09-01 02:16:41
【问题描述】:

我正在尝试在地图上以相对坐标(即 0 到 1)绘制一个框。原因是我的地图上有一个颜色条,但看不清楚。我想要一个透明的盒子在它后面。我看过添加补丁矩形(请参阅Draw rectangle (add_patch) in pylab mode),但这是在数据坐标中,在这张地图上不容易确定。我还找到了 axhspan,它使用 x 跨度的相对坐标,但 y 跨度的数据坐标。

有没有办法使用相对坐标在 matplotlib 轴对象中绘制一个框?

【问题讨论】:

    标签: graphics matplotlib coordinates matplotlib-basemap


    【解决方案1】:

    以下是一种将盒装文本添加到相对坐标的方法:

    #!/usr/bin/python3
    
    from matplotlib import pyplot as plt
    
    x = range(5)
    y = range(5)
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    ax.plot(x, y)
    
    ax.text(0.5, 0.5,
        "Relative coords!",
        horizontalalignment = 'center',
        backgroundcolor = "white",
        verticalalignment = 'center',
        bbox=dict(facecolor='white', edgecolor='green', alpha=0.65),
        transform = ax.transAxes,
        )
    
    fig.savefig("mwe.png")
    

    结果:

    编辑

    在给定相对坐标/尺寸的情况下仅绘制一个框,其中没有文本:

    #!/usr/bin/python3
    
    from matplotlib import pyplot as plt
    from matplotlib.patches import Rectangle
    
    x = range(5)
    y = range(5)
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    ax.plot(x, y, zorder=1)
    
    plt.gca().add_patch(Rectangle(
        (0.4, 0.4), # lower left point of rectangle
        0.2, 0.2,   # width/height of rectangle
        transform=ax.transAxes,
        facecolor="white",
        edgecolor='green',
        alpha=0.65,
        zorder=2,
        ))
    
    fig.savefig("mwe.png")
    

    结果:

    【讨论】:

    • 可以不加文字并指定框的宽高吗?
    猜你喜欢
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多