赞
踩
边缘直方图具有沿X和Y轴变量的直方图。这用于可视化X和Y之间的关系以及单独的X和Y的单变量分布。该图如果经常用于探索性数据分析(EDA)。
- # Import Data
- df = pd.read_csv("https://raw.githubusercontent.com/selva86/datasets/master/mpg_ggplot2.csv")
-
- # Create Fig and gridspec
- fig = plt.figure(figsize=(16, 10), dpi= 80)
- grid = plt.GridSpec(4, 4, hspace=0.5, wspace=0.2)
-
- # Define the axes
- ax_main = fig.add_subplot(grid[:-1, :-1])
- ax_right = fig.add_subplot(grid[:-1, -1], xticklabels=[], yticklabels=[])
- ax_bottom = fig.add_subplot(grid[-1, 0:-1], xticklabels=[], yticklabels=[])
-
- # Scatterplot on main ax
- ax_main.scatter('displ', 'hwy', s=df.cty*4, c=df.manufacturer.astype('category').cat.codes, alpha=.9, data=df, cmap="tab10", edgecolors='gray', linewidths=.5)
-
- # histogram on the right
- ax_bottom.hist(df.displ, 40, histtype='stepfilled', orientation='vertical', color='deeppink')
- ax_bottom.invert_yaxis()
-
- # histogram in the bottom
- ax_right.hist(df.hwy, 40, histtype='stepfilled', orientation='horizontal', color='deeppink')
-
- # Decorations
- ax_main.set(title='Scatterplot with Histograms
- displ vs hwy', xlabel='displ', ylabel='hwy')
- ax_main.title.set_fontsize(20)
- for item in ([ax_main.xaxis.label, ax_main.yaxis.label] + ax_main.get_xticklabels() + ax_main.get_yticklabels()):
- item.set_fontsize(14)
-
- xlabels = ax_main.get_xticks().tolist()
- ax_main.set_xticklabels(xlabels)
- plt.show()
边缘箱图与边缘直方图具有相似的用途。然而,箱线图有助于精确定位X和Y的中位数,第25和第75百分位数。
- # Import Data
- df = pd.read_csv("https://raw.githubusercontent.com/selva86/datasets/master/mpg_ggplot2.csv")
-
- # Create Fig and gridspec
- fig = plt.figure(figsize=(16, 10), dpi= 80)
- grid = plt.GridSpec(4, 4, hspace=0.5, wspace=0.2)
-
- # Define the axes
- ax_main = fig.add_subplot(grid[:-1, :-1])
- ax_right = fig.add_subplot(grid[:-1, -1], xticklabels=[], yticklabels=[])
- ax_bottom = fig.add_subplot(grid[-1, 0:-1], xticklabels=[], yticklabels=[])
-
- # Scatterplot on main ax
- ax_main.scatter('displ', 'hwy', s=df.cty*5, c=df.manufacturer.astype('category').cat.codes, alpha=.9, data=df, cmap="Set1", edgecolors='black', linewidths=.5)
-
- # Add a graph in each part
- sns.boxplot(df.hwy, ax=ax_right, orient="v")
- sns.boxplot(df.displ, ax=ax_bottom, orient="h")
-
- # Decorations ------------------
- # Remove x axis name for the boxplot
- ax_bottom.set(xlabel='')
- ax_right.set(ylabel='')
-
- # Main Title, Xlabel and YLabel
- ax_main.set(title='Scatterplot with Histograms
- displ vs hwy', xlabel='displ', ylabel='hwy')
-
- # Set font size of different components
- ax_main.title.set_fontsize(20)
- for item in ([ax_main.xaxis.label, ax_main.yaxis.label] + ax_main.get_xticklabels() + ax_main.get_yticklabels()):
- item.set_fontsize(14)
-
- plt.show()
Correlogram用于直观地查看给定数据帧(或2D数组)中所有可能的数值变量对之间的相关度量。
- # Import Dataset
- df = pd.read_csv("https://github.com/selva86/datasets/raw/master/mtcars.csv")
-
- # Plot
- plt.figure(figsize=(12,10), dpi= 80)
- sns.heatmap(df.corr(), xticklabels=df.corr().columns, yticklabels=df.corr().columns, cmap='RdYlGn', center=0, annot=True)
-
- # Decorations
- plt.title('Correlogram of mtcars', fontsize=22)
- plt.xticks(fontsize=12)
- plt.yticks(fontsize=12)
- plt.show()
成对图是探索性分析中的最爱,以理解所有可能的数字变量对之间的关系。它是双变量分析的必备工具。
- # Load Dataset
- df = sns.load_dataset('iris')
-
- # Plot
- plt.figure(figsize=(10,8), dpi= 80)
- sns.pairplot(df, kind="scatter", hue="species", plot_kws=dict(s=80, edgecolor="white", linewidth=2.5))
- plt.show()
- # Load Dataset
- df = sns.load_dataset('iris')
-
- # Plot
- plt.figure(figsize=(10,8), dpi= 80)
- sns.pairplot(df, kind="reg", hue="species")
- plt.show()
如果您想根据单个指标查看项目的变化情况,并可视化此差异的顺序和数量,那么发散条是一个很好的工具。它有助于快速区分数据中组的性能,并且非常直观,并且可以立即传达这一点。
- # Prepare Data
- df = pd.read_csv("https://github.com/selva86/datasets/raw/master/mtcars.csv")
- x = df.loc[:, ['mpg']]
- df['mpg_z'] = (x - x.mean())/x.std()
- df['colors'] = ['red' if x < 0 else 'green' for x in df['mpg_z']]
- df.sort_values('mpg_z', inplace=True)
- df.reset_index(inplace=True)
-
- # Draw plot
- plt.figure(figsize=(14,10), dpi= 80)
- plt.hlines(y=df.index, xmin=0, xmax=df.mpg_z, color=df.colors, alpha=0.4, linewidth=5)
-
- # Decorations
- plt.gca().set(ylabel='$Model$', xlabel='$Mileage$')
- plt.yticks(df.index, df.cars, fontsize=12)
- plt.title('Diverging Bars of Car Mileage', fontdict={'size':20})
- plt.grid(linestyle='--', alpha=0.5)
- plt.show()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。