介紹
以下來自維基百科
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ù)分布在這附近概率最大喜每,其他的功能與箱線圖基本差不多,由于氣象領(lǐng)域用該圖形較少蕉堰,所以今天寫了一個記錄貼來研究下如何繪制宝冕。
方法有多種
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ù)導(dǎo)入dataset,其他不用設(shè)置就可以出來簡單的小提琴圖蔗蹋。其他的如顏色何荚,百分比都可以在參數(shù)中設(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)的參考文檔永遠是最好的學(xué)習(xí)工具
seaborn作為一個數(shù)據(jù)處理分析的工具使用起來非常方便,很適合初期出圖的時候使用稠鼻。但是我這邊因為個人需求比較多冈止,改起來很亂不如我自己手動寫函數(shù)了,所以這個用的不多候齿。我就用同樣的數(shù)據(jù)做個例子熙暴。
上段代碼最后一個改為
bplot1 =sns.violinplot(data=ALL)
總結(jié)
seaborn很適合對數(shù)據(jù)進行草圖的繪制,這兩個圖的結(jié)果是一致的慌盯。小提琴圖目前在很多一區(qū)論文中也會出現(xiàn)了周霉,以后可能會經(jīng)常出現(xiàn),代替箱線圖指日可待了亚皂,哈哈俱箱。