如何为部分直方图补丁指定颜色? [英] How can I specify the color for a partial histogram patch?

查看:71
本文介绍了如何为部分直方图补丁指定颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 我想产生下面的图
    • 请注意,一个条形部分为蓝色,部分为蓝绿色.这就是我要复制的东西

    import pandas as pd
    import seaborn as sns
    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib.patches import Rectangle
    
    # plt parameters
    plt.rcParams['figure.figsize'] = (10.0, 10.0)
    plt.style.use('seaborn-dark-palette')
    plt.rcParams['axes.grid'] = True
    plt.rcParams["patch.force_edgecolor"] = True
    
    # data
    np.random.seed(365)
    replicates = np.random.normal(0.0011542834124829882, 1.6243483937004772, 10000)
    
    mean_diff = 1.1582360922659518
    
    # plot replicates
    p = sns.distplot(replicates, bins=30, )
    
    # add the vertical line
    plt.vlines(mean_diff, 0, 0.25, color='r', label='mean', colors=clrs)
    
    # add the annotation
    plt.annotate('p-value', xy=(3.5, 0.05), weight='bold', color='teal',
                 xytext=(4, 0.15), fontsize=15, arrowprops=dict(arrowstyle="->", color='teal'))
    
    # color bars greater than mean_diff except the partial bar
    for rectangle in p.patches:
        if rectangle.get_x() >= mean_diff:
            rectangle.set_facecolor('teal')
    
    # I tried adding a Rectangle of the following dimensions, but it didn't color the rectangle 
    Rectangle(xy=(mean_diff, 0), width=1.28523-mean_diff, height=0.206371, angle=0).set_facecolor('teal')
    
    # add cosmetics
    plt.legend()
    plt.ylabel('PDF')
    plt.xlabel('PA - OH mean percent replicate vote difference')
    plt.show()
    

    • Rectangle(xy =(0.876747,0),width = 0.408487,height = 0.206371,angle = 0)是需要部分着色的矩形.
      • Rectangle(xy =(1.28523,0),width = 0.408487,height = 0.150066,angle = 0)是紧随其后的色块,其颜色为 Teal
        • Rectangle(xy=(0.876747, 0), width=0.408487, height=0.206371, angle=0) is the Rectangle that needs to be partially colored.
          • Rectangle(xy=(1.28523, 0), width=0.408487, height=0.150066, angle=0) is the patch immediately after, which is colored Teal
            • 请注意,从均值开始的部分条形不是 teal

            推荐答案

            import pandas as pd
            import seaborn as sns
            import matplotlib.pyplot as plt
            import numpy as np
            from matplotlib.patches import Rectangle
            
            # plt parameters
            plt.rcParams['figure.figsize'] = (10.0, 10.0)
            plt.style.use('seaborn-dark-palette')
            plt.rcParams['axes.grid'] = True
            plt.rcParams["patch.force_edgecolor"] = True
            
            # data
            np.random.seed(365)
            replicates = np.random.normal(0.0011542834124829882, 1.6243483937004772, 10000)
            
            mean_diff = 1.1582360922659518
            
            # plot replicates
            p = sns.distplot(replicates, bins=30)
            
            # add the vertical line
            plt.vlines(mean_diff, 0, 0.25, color='r', label='mean', colors="r")
            
            # add the annotation
            plt.annotate('p-value', xy=(3.5, 0.05), weight='bold', color='teal',
                         xytext=(4, 0.15), fontsize=15, arrowprops=dict(arrowstyle="->", color='teal'))
            
            # color bars greater than mean_diff except the partial bar
            for rectangle in p.patches:
                if rectangle.get_x() >= mean_diff:
                    rectangle.set_facecolor('teal')
            
            # I tried adding a Rectangle of the following dimensions, but it didn't color the rectangle
            width = 1.28523-mean_diff
            plt.bar(x=mean_diff+0.5*width,height=0.206371,width=width,color="#99cccc",edgecolor="#7a7d89")
            plt.show()
            

            这会以匹配的方式添加栏.就像我们之前所说的那样.

            This adds the bar in a matching manner. A bit hacky as we said before.

            这篇关于如何为部分直方图补丁指定颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆