• python可视化分析(五)-绘制边缘箱线图


    实现功能:

    中间散点图,右边和下边分别绘制y轴及x轴数据的箱图,类似于边缘直方图,不过箱线图有助于精确定位变量的分位数。

    实现代码:

    import pandas as pd
    import matplotlib as mpl
    import matplotlib.pyplot as plt
    import seaborn as sns
    import warnings
    warnings.filterwarnings(action='once')
    plt.style.use('seaborn-whitegrid')
    sns.set_style("whitegrid")
    print(mpl.__version__)
    print(sns.__version__)
    
    def draw_Marginal_Boxplot(file):
        # Import Data
        df = pd.read_csv(file)
    
        # Create Fig and gridspec
        fig = plt.figure(figsize=(10, 8), dpi=100)
        grid = plt.GridSpec(
            4, 4, hspace=0.5, wspace=0.2
        )  # 这里使用了matplotlib.pyplot.GridSpec分片figure
    
        # 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", linewidth=1, palette='Set1')
        sns.boxplot(df.displ, ax=ax_bottom, orient="h", linewidth=1, palette='Set1')
    
        # 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 \n displ vs hwy',
                    xlabel='displ',
                    ylabel='hwy')
    
        # Set font size of different components
        ax_main.title.set_fontsize(12)
        for item in ([ax_main.xaxis.label, ax_main.yaxis.label] +
                     ax_main.get_xticklabels() + ax_main.get_yticklabels()):
            item.set_fontsize(11)
    
        plt.show()
    
    draw_Marginal_Boxplot("F:\数据杂坛\datasets\mpg_ggplot2.csv")

    实现效果:

     

    喜欢记得点赞,在看,收藏,

    关注V订阅号:数据杂坛,获取数据集,完整代码和效果,将持续更新!

     

  • 相关阅读:
    十大排序算法(面试必备)
    yarn 报错文件名、目录名或卷标语法不正确 nvm
    Java的<? super T>和<? extends R>理解与应用
    nginx(三十九) ngx_http_realip_module模块学习
    《500强高管谈VE》-以VE ORIENTED管理为目标
    vue事件总线
    【Java】map根据value获取所有key
    STS使用手册
    刷题记录:牛客NC50493石子合并
    使用Ceres进行slam必须要弄清楚的几个类和函数
  • 原文地址:https://blog.csdn.net/sinat_41858359/article/details/125404703