python气象数据处理与绘图_Python气象数据处理与绘图:小提琴图 Violin Plot

介绍

以下来自维基百科

A violin plot is a method of plotting numeric data. It is similar to a box plot, with the addition of a rotated kernel density plot on each side.Violin plots are similar to box plots, except that they also show the probability density of the data at different values, usually smoothed by a kernel density estimator. Typically a violin plot will include all the data that is in a box plot: a marker for the median of the data; a box or marker indicating the interquartile range; and possibly all sample points, if the number of samples is not too high.

A violin plot is more informative than a plain box plot. While a box plot only shows summary statistics such as mean/median and interquartile ranges, the violin plot shows the full distribution of the data. The difference is particularly useful when the data distribution is multimodal (more than one peak). In this case a violin plot shows the presence of different peaks, their position and relative amplitude.

简单来说

小提琴图 (Violin Plot)顾名思义就是长得像小提琴的图,相比较箱线图(Box Plot)多了一个概率密度展示的功能。这个功能通过小提琴的宽窄来表示,比如下图下面最宽的地方代表数据分布在这附近概率最大,其他的功能与箱线图基本差不多,由于气象领域用该图形较少,所以今天写了一个记录贴来研究下如何绘制。

image.png

方法有多种

1.matplotlib

https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.violinplot.html

matplotlib官网提供了最简单的小提琴图画图

用法如下

Axes.matplotlib.violinplot(self, dataset, positions=None, vert=True, widths=0.5, showmeans=False, showextrema=True, showmedians=False, quantiles=None, points=100, bw_method=None, ***, data=None)[source]¶

主要就是数据读取之后,把数据导入dataset,其他不用设置就可以出来简单的小提琴图。其他的如颜色,百分比都可以在参数中设置。这里需要注意的是,数据读取可以用pandas,也可以自己手动输入保存为numpy格式都行。

这里给出读取数据+画图的主要核心模块,比如我把各个区域的数据提取其中不同时期的数据进行分析。

region=0

ALL=[[],t1[region][6:25 ].values,t2[region][6:25 ].values,t3[region][6:25 ].values,t4[region][6:25 ].values,

[],t1[region][46:65].values,t2[region][46:65].values,t3[region][46:65].values,t4[region][46:65].values,

[],t1[region][66:85].values,t2[region][66:85].values,t3[region][66:85].values,t4[region][66:85].values,

[],

]

fig =plt.figure(figsize=(6, 5))

ax =fig.add_axes([0.3, 0.2, 1.5, 1])

medianprops = dict(color="black",linewidth=2)

bplot1 =ax.boxplot(wind_ALL, vert=True, # vertical box alignment

whis=False,

patch_artist=True, # fill with color

showfliers =False, #

showbox = True , #显示超出上限的异常值。

showmeans =False , #平均值. 绿色交尖

medianprops=medianprops,

widths=0.5,

autorange=True,

# labels=labels

) # will be used to label x-ticks

自己设置一下图的属性就可以得到这样的小提琴图了

2.Seaborn

https://seaborn.pydata.org/generated/seaborn.violinplot.html

官网的参考文档永远是最好的学习工具

seaborn作为一个数据处理分析的工具使用起来非常方便,很适合初期出图的时候使用。但是我这边因为个人需求比较多,改起来很乱不如我自己手动写函数了,所以这个用的不多。我就用同样的数据做个例子。

上段代码最后一个改为

bplot1 =sns.violinplot(data=ALL)

seaborn

总结

seaborn很适合对数据进行草图的绘制,这两个图的结果是一致的。小提琴图目前在很多一区论文中也会出现了,以后可能会经常出现,代替箱线图指日可待了,哈哈。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值