- Python Data Analysis Cookbook
- Ivan Idris
- 193字
- 2021-07-14 11:05:41
Combining box plots and kernel density plots with violin plots
Violin plots combine box plots and kernel density plots or histograms in one type of plot. Seaborn and matplotlib both offer violin plots. We will use Seaborn in this recipe on z-scores of weather data. The z-scoring is not essential, but without it, the violins will be more spread out.
How to do it...
- Import the required libraries as follows:
import seaborn as sns from dautil import data import matplotlib.pyplot as plt
- Load the weather data and calculate z-scores:
df = data.Weather.load() zscores = (df - df.mean())/df.std()
- Plot a violin plot of the z-scores:
%matplotlib inline plt.figure() plt.title('Weather Violin Plot') sns.violinplot(zscores.resample('M')) plt.ylabel('Z-scores')
Refer to the following plot for the first violin plot:
- Plot a violin plot of rainy and dry (the opposite of rainy) days against wind speed:
plt.figure() plt.title('Rainy Weather vs Wind Speed') categorical = df categorical['RAIN'] = categorical['RAIN'] > 0 ax = sns.violinplot(x="RAIN", y="WIND_SPEED", data=categorical)
Refer to the following plot for the second violin plot:

The source code is available in the violins.ipynb
file in this book's code bundle.
See also
- The Seaborn documentation about violin plots at https://web.stanford.edu/~mwaskom/software/seaborn/generated/seaborn.violinplot.html (retrieved July 2015)
推薦閱讀
- Designing Machine Learning Systems with Python
- Java程序設(shè)計(jì)實(shí)戰(zhàn)教程
- 零基礎(chǔ)學(xué)Scratch少兒編程:小學(xué)課本中的Scratch創(chuàng)意編程
- 軟件測(cè)試工程師面試秘籍
- Oracle數(shù)據(jù)庫從入門到運(yùn)維實(shí)戰(zhàn)
- Visual C++數(shù)字圖像處理技術(shù)詳解
- Gradle for Android
- Arduino家居安全系統(tǒng)構(gòu)建實(shí)戰(zhàn)
- 用案例學(xué)Java Web整合開發(fā)
- 智能搜索和推薦系統(tǒng):原理、算法與應(yīng)用
- JavaScript高級(jí)程序設(shè)計(jì)(第4版)
- Python深度學(xué)習(xí)入門:從零構(gòu)建CNN和RNN
- Mastering Responsive Web Design
- The Python Apprentice
- VBA Automation for Excel 2019 Cookbook