【问题标题】:Subplot of Windrose in matplotlibmatplotlib 中 Windrose 的子图
【发布时间】:2017-03-11 08:44:06
【问题描述】:

我正在尝试用风玫瑰的 4 个子图制作一个图形,但我意识到风玫瑰只有这样的轴:ax = WindroseAxes.from_ax() 那么,我怎样才能用风玫瑰绘制子图呢?

【问题讨论】:

    标签: python matplotlib windrose


    【解决方案1】:

    有两种解决方案:

    (a) 从矩形创建坐标轴

    首先这里已经有一个类似的问题:How to add specific axes to matplotlib subplot?

    在那里,解决方案是创建一个矩形rect,其中包含图中新子图轴的坐标,然后调用ax = WindroseAxes(fig, rect)

    一个更容易理解的例子是

    from windrose import WindroseAxes
    from matplotlib import pyplot as plt
    import numpy as np
    ws = np.random.random(500) * 6
    wd = np.random.random(500) * 360
    
    fig=plt.figure()
    rect=[0.5,0.5,0.4,0.4] 
    wa=WindroseAxes(fig, rect)
    fig.add_axes(wa)
    wa.bar(wd, ws, normed=True, opening=0.8, edgecolor='white')
    
    plt.show()
    

    (b) 添加投影

    现在创建这个矩形可能会很烦人,如果能够使用 matplotlib 子图功能会更好。
    here 提出的一项建议是将 WindroseAxes 注册为 matplotlib 的投影。为此,您需要编辑site-packages/windrose中的windrose.py文件如下:

    1. 在文件开头包含一个导入 from matplotlib.projections import register_projection
    2. 然后添加一个名称变量:

      class WindroseAxes(PolarAxes):
          name = 'windrose'
          ...
      
    3. 最后,在 windrose.py 的末尾添加:

      register_projection(WindroseAxes)
      

    完成后,您可以使用 matplotlib 轴的投影参数轻松创建 windrose 轴:

    from matplotlib import pyplot as plt
    import windrose
    import matplotlib.cm as cm
    import numpy as np
    
    ws = np.random.random(500) * 6
    wd = np.random.random(500) * 360
    
    fig = plt.figure()
    ax = fig.add_subplot(221, projection="windrose")
    
    ax.contourf(wd, ws, bins=np.arange(0, 8, 1), cmap=cm.hot)
    
    ax.legend(bbox_to_anchor=(1.02, 0))
    plt.show()
    

    【讨论】:

      【解决方案2】:

      要使子图具有相同的比例(例如,对于每月数据),只需在 add_subplot 函数中添加 rmax 参数。对我来说工作:

      ax = fig.add_subplot(nrows, ncols, month, projection="windrose", rmax = 50)
      

      【讨论】:

        猜你喜欢
        • 2019-11-23
        • 2021-10-31
        • 2018-08-31
        • 2020-12-15
        • 2016-09-25
        • 2016-01-07
        • 2019-03-22
        • 2016-06-01
        • 2022-08-16
        相关资源
        最近更新 更多