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

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

主站蜘蛛池模板: 上犹县| 光山县| 沅江市| 郯城县| 磐石市| 彰化县| 永昌县| 全椒县| 太湖县| 大埔区| 慈利县| 耿马| 金堂县| 石楼县| 泉州市| 特克斯县| 北川| 廉江市| 大邑县| 沙田区| 上犹县| 冷水江市| 定边县| 涿鹿县| 会昌县| 廉江市| 汉中市| 南涧| 潜山县| 曲阜市| 新龙县| 正镶白旗| 蓝山县| 扬中市| 信丰县| 阿城市| 兴安盟| 东至县| 宿松县| 大庆市| 阜新|