python中使用windrose绘制玫瑰图

python中使用windrose绘制玫瑰图


        之前了解过如使用echarts对数据进行前端渲染,个人感觉echarts还是很强大的,但是有时候需要后台输出一些含有类似折线图,玫瑰图等报表时,类似echarts的前端渲染方式就不合适了,所以尝试使用python解决这个问题,后端提供数据,由python生成统计图,并执行图片保存,然后直接导入报表中。这样可以省去前端渲染工作,不需要用户去点开页面生成图片,更符合生成报表时使用。这里使用的是python的第三方库windrose。

安装

首先提供官方文档和github项目开源地址
        官方文档:https://windrose.readthedocs.io/en/latest/index.html
        官方地址:https://pypi.org/project/windrose/
        github:https://github.com/python-windrose/windrose

  1. 使用pip安装
# 使用pip安装
pip install windrose
# 更新到最新版本
pip install git+https://github.com/python-windrose/windrose
  1. 源码安装
git clone https://github.com/python-windrose/windrose
python setup.py install

使用例子

这里还是建议大家去看官方的例子,或者去github参考代码,我这里也是搬运过来的。

# 引入依赖
from windrose import WindroseAxes
from matplotlib import pyplot as plt
import matplotlib.cm as cm
import numpy as np

# 使用nmupy随机生成风速风向数组
ws = np.random.random(500) * 6
wd = np.random.random(500) * 360

# 绘图
ax = WindroseAxes.from_ax()
ax.bar(wd, ws, normed=True, opening=0.8, edgecolor='white')
ax.set_legend()

运行结果如下:
输出图片

参数简介

这里主要想说的是关于参数设置的内容,找了很多地方没有找到文档,最后查看源码,对源码中注释的内容加以说明。

  1. directin:风吹来的方向,以北为中心,正北方向为0°,顺时针旋转,范围是0°~360°
  2. var:要计算的变量值,通常是风速---->这里可以换成你想显示的数据内容
  3. nsector:扇区的数量,默认值为16(每个扇区22.5°)
  4. bins:legend的数量,默认为6,取值为参数2:var中最小值和最大值之间平均分成6份
  5. blowto:默认值为False,当为True的时候玫瑰图将旋转180°,显示风吹的位置。这里没有太明白有什么作用,从图上看,效果就是旋转180°。
  6. colors:颜色设置,可以是string类型或者tuple类型,为string类型时所有的区域将被绘制成一个颜色。
    当使用元组时,元组里设置的颜色个数要与参数4中bins数相同,不然报错,颜色可采用字符串,十六进制,rgb方式表示。
  7. cmap:颜色设置,继承自matplotlib.cm
  8. 支持matplotlib.pyplot.plot中的所有参数设置
    注:1,2是必须参数,3-7为可选参数,具体使用可以参考官方实例。

bar图绘制的源码如下:

def bar(self, direction, var, **kwargs):
    bins, nbins, nsector, colors, angles, kwargs = self._init_plot(
        direction, var, **kwargs
    )
    kwargs.pop("facecolor", None)
    edgecolor = kwargs.pop("edgecolor", None)
    if edgecolor is not None:
        if not isinstance(edgecolor, str):
            raise ValueError("edgecolor must be a string color")
    opening = kwargs.pop("opening", None)
    if opening is None:
        opening = 0.8
    dtheta = 2 * np.pi / nsector
    opening = dtheta * opening

    offs = self._calm_circle()

    for j in range(nsector):
        offset = offs
        for i in range(nbins):
            if i > 0:
                offset += self._info["table"][i - 1, j]
            val = self._info["table"][i, j]
            zorder = ZBASE + nbins - i
            patch = mpl.patches.Rectangle(
                (angles[j] - opening / 2, offset),
                opening,
                val,
                facecolor=colors[i],
                edgecolor=edgecolor,
                zorder=zorder,
                **kwargs
            )
            self.add_patch(patch)
            if j == 0:
                self.patches_list.append(patch)
    self._update()

其他方法等用到时候再一一解释,希望对大家有所帮助。有错误欢迎指正。

  • 7
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值