官术网_书友最值得收藏!

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...

  1. Import the required libraries as follows:
    import seaborn as sns
    from dautil import data
    import matplotlib.pyplot as plt
  2. Load the weather data and calculate z-scores:
    df = data.Weather.load()
    zscores = (df - df.mean())/df.std()
  3. 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:

  4. 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

主站蜘蛛池模板: 浪卡子县| 闸北区| 陆川县| 长子县| 岗巴县| 微山县| 呼伦贝尔市| 岑巩县| 文水县| 梁河县| 乌兰浩特市| 西林县| 大埔区| 咸宁市| 平凉市| 通化市| 远安县| 扶绥县| 门源| 洛隆县| 鸡泽县| 汾阳市| 玉屏| 本溪市| 延边| 宁武县| 共和县| 东宁县| 孙吴县| 池州市| 南涧| 科技| 互助| 东兰县| 繁昌县| 安新县| 邯郸县| 曲麻莱县| 辰溪县| 于田县| 丹江口市|