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

Viewing a matrix of scatterplots

If you don't have many variables in your dataset, it is a good idea to view all the possible scatterplots for your data. You can do this with one function call from either seaborn or pandas. These functions display a matrix of plots with kernel density estimation plots or histograms on the diagonal.

How to do it...

  1. Imports the following:
    import pandas as pd
    from dautil import data
    from dautil import ts
    import matplotlib.pyplot as plt
    import seaborn as sns
    import matplotlib as mpl
  2. Load the weather data with the following lines:
    df = data.Weather.load()
    df = ts.groupby_yday(df).mean()
    df.columns = [data.Weather.get_header(c) for c in df.columns]
  3. Plot with the Seaborn pairplot() function, which plots histograms on the diagonal by default:
    %matplotlib inline
    
    # Seaborn plotting, issues due to NaNs
    sns.pairplot(df.fillna(0))

    The following plots are the result:

  4. Plot similarly with the pandas scatter_matrix() function and request kernel density estimation plots on the diagonal:
    sns.set({'figure.figsize': '16, 12'})
    mpl.rcParams['axes.linewidth'] = 9
    mpl.rcParams['lines.linewidth'] = 2
    plots = pd.scatter_matrix(df, marker='o', diagonal='kde')
    plt.show()

Refer to the following plots for the end result:

The complete code is available in the scatter_matrix.ipynb file in this book's code bundle.

主站蜘蛛池模板: 本溪市| 额济纳旗| 洪洞县| 体育| 互助| 布尔津县| 于田县| 民和| 吉首市| 枣阳市| 克东县| 奈曼旗| 南丹县| 新民市| 日土县| 荥阳市| 沛县| 民勤县| 罗甸县| 吉木乃县| 杭州市| 文昌市| 白银市| 重庆市| 威远县| 克什克腾旗| 亚东县| 六安市| 福鼎市| 菏泽市| 夏邑县| 德格县| 金平| 盖州市| 南澳县| 乐山市| 上杭县| 东台市| 太仆寺旗| 山西省| 侯马市|