box-and-whisker plot —— Python Data Science Cookook

来源:互联网 发布:多功能拍照软件 编辑:程序博客网 时间:2024/05/14 07:51

Sources from : Python Data Science Cookbook case

A box-and-whisker plot is a good companion with the summary statistics to view the statistical summary of the data in hand. Box-and-whiskers can effectively represent quantiles in data and also outliers, if any, emphasizing the overall structure of the data. A box plot consists of the following features:

  1. A horizontal line indicating the median that indicates the location of the data 
  2. A box spanning the interquartile range, measuring the dispersion   
  3. A set of whiskers that extends from the central box horizontally and vertically, which indicates the tail of the distribution
箱形图:最大优点展示数据的结构和异常点
  1. 标出中位线
  2. 箱形扩展到4分位(箱子下沿是数据分布在25%的标识,箱子中间那条线50%,箱子上沿75%)
  3. 箱子以外的垂直于箱子的线是头尾两端的数据分布
iris是sklearn库自带的数据集, 详询https://archive.ics.uci.edu/ml/datasets/Iris
#!/usr/bin/env python2# -*- coding: utf-8 -*-"""Created 2017@author: snaildove"""# Load Librariesimport numpy as npfrom sklearn.datasets import load_irisimport matplotlib.pyplot as plt# Load Iris datasetdata = load_iris()x = data['data']plt.close('all')#Let’s demonstrate how to create a box-and-whisker plot:# Plot the box and whiskerfig = plt.figure(1)ax = fig.add_subplot(111)ax.boxplot(x)ax.set_xticklabels(data['feature_names'])plt.show()y=data['target']class_labels = data['target_names']fig = plt.figure(2,figsize=(18,10))sub_plt_count = 221for t in range(0,3):    ax = fig.add_subplot(sub_plt_count)    y_index = np.where(y==t)[0]    x_ = x[y_index,:]    ax.boxplot(x_)    ax.set_title(class_labels[t])    ax.set_xticklabels(data['feature_names'])    sub_plt_count+=1plt.show()



0 0
原创粉丝点击