本文介绍了matplotlib中Windrose的子图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用4个风玫瑰子图制作一个人物,但是我意识到风玫瑰只有这样的轴:ax = WindroseAxes.from_ax()那么,我怎么用风玫瑰画一个子图?

I am trying to make a figure with 4 subplots of windrose, But I realised that the windrose only have axis like this:ax = WindroseAxes.from_ax() So, how can I draw a subplots with windrose?

推荐答案

有两种解决方案:

首先,这里已经有一个类似的问题:如何将特定轴添加到matplotlib子图?

First of all there is a similar question already here: How to add specific axes to matplotlib subplot?

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

There, the solution is to create a rectangle rect with coordinates of the new subplot axes within the figure and then call ax = WindroseAxes(fig, rect)

一个更容易理解的例子是

An easier to understand example would be

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子图功能会更好.
此处提出的一项建议是,将WindroseAxes作为投影注册到matplotlib中.为此,您需要按照以下步骤在site-packages/windrose中编辑文件windrose.py:

(b) adding a projection

Now it may be rather annoying to create this rectangle and it would be much better to be able to use the matplotlib subplot functionality.
One suggestion that has been made here is to register the WindroseAxes as a projection into matplotlib. To this end, you need to edit the file windrose.py in the site-packages/windrose as follows:

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

  1. Include an import from matplotlib.projections import register_projection at the beginning of the file.
  2. Then add a name variable :

class WindroseAxes(PolarAxes):
    name = 'windrose'
    ...

  • 最后,在windrose.py的末尾,添加:

  • Finally, at the end of windrose.py, you add:

    register_projection(WindroseAxes)
    

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

    Once that is done, you can easily create your windrose axes using the projection argument to the matplotlib axes:

    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()
    

    这篇关于matplotlib中Windrose的子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    06-13 06:23