介紹
以下來自維基百科
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)多了一個概率密度展示的功能。這個功能通過小提琴的寬窄來表示,比如下圖下面最寬的地方代表數(shù)據(jù)分布在這附近概率最大,其他的功能與箱線圖基本差不多,由于氣象領域用該圖形較少,所以今天寫了一個記錄貼來研究下如何繪制。

方法有多種
1.matplotlib
https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.violinplot.html
matplotlib官網(wǎng)提供了最簡單的小提琴圖畫圖
用法如下
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]?
主要就是數(shù)據(jù)讀取之后,把數(shù)據(jù)導入dataset,其他不用設置就可以出來簡單的小提琴圖。其他的如顏色,百分比都可以在參數(shù)中設置。這里需要注意的是,數(shù)據(jù)讀取可以用pandas,也可以自己手動輸入保存為numpy格式都行。
這里給出讀取數(shù)據(jù)+畫圖的主要核心模塊,比如我把各個區(qū)域的數(shù)據(jù)提取其中不同時期的數(shù)據(jù)進行分析。
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
官網(wǎng)的參考文檔永遠是最好的學習工具
seaborn作為一個數(shù)據(jù)處理分析的工具使用起來非常方便,很適合初期出圖的時候使用。但是我這邊因為個人需求比較多,改起來很亂不如我自己手動寫函數(shù)了,所以這個用的不多。我就用同樣的數(shù)據(jù)做個例子。
上段代碼最后一個改為
bplot1 =sns.violinplot(data=ALL)

總結
seaborn很適合對數(shù)據(jù)進行草圖的繪制,這兩個圖的結果是一致的。小提琴圖目前在很多一區(qū)論文中也會出現(xiàn)了,以后可能會經(jīng)常出現(xiàn),代替箱線圖指日可待了,哈哈。