【问题标题】:Custom multi-colored horizontal bar chart matplotlib自定义多色水平条形图 matplotlib
【发布时间】:2021-11-08 22:15:02
【问题描述】:

我想创建一个水平条形图,其中每个条形都有自定义的条纹配色方案(类似于下图)。我熟悉创建普通条形图,但我不知道两种颜色方案如何工作。

试用过的代码:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.DataFrame({"P1_P2": [0.20],
                   "P1_P3": [0.16],
                   "P2_P5": [0.12],
                   "P3_P5": [0.06],
                   "P3_P6": [0.06],
                   "P5_P6": [0.06]})
df=df.T

fig, ax = plt.subplots(dpi=600, figsize=(4, 4), nrows=1, ncols=1, facecolor=None, edgecolor='black')

df.plot.barh(ax=ax, position=0.50, width=0.3, color=(245/255, 153/255, 145/255, 1.0))
ax.get_legend().remove()

plt.show()

【问题讨论】:

    标签: python matplotlib bar-chart colorbar


    【解决方案1】:

    您可以用不同的颜色孵化每个条形。线条的宽度需要根据特定的情节进行调整。

    由于阴影与轮廓使用相同的颜色,因此可以创建仅显示轮廓的条的副本。

    import matplotlib.pyplot as plt
    from matplotlib import rcParams
    import pandas as pd
    from copy import copy
    
    df = pd.DataFrame([0.2 , 0.16, 0.12, 0.06, 0.06, 0.06], index=['P1_P2', 'P1_P3', 'P2_P5', 'P3_P5', 'P3_P6', 'P5_P6'])
    first_colors = ['dodgerblue', 'orange', 'green','silver', 'silver', 'dodgerblue']
    second_colors = ['crimson', 'crimson', 'dodgerblue', 'dodgerblue', 'green', 'gold']
    
    fig, ax = plt.subplots(dpi=600, figsize=(4, 4), nrows=1, ncols=1, facecolor=None, edgecolor='black')
    
    df.plot.barh(ax=ax, position=0.50, width=0.3, legend=False, linewidth=0)
    
    rcParams['hatch.linewidth'] = 4
    for bar, main_color, hatch_color in zip(ax.containers[0], first_colors, second_colors):
        rect = copy(bar)
        rect.set_facecolor('none')
        rect.set_edgecolor('black')
        rect.set_linewidth(2)
        ax.add_patch(rect)
        bar.set_facecolor(main_color)
        bar.set_edgecolor(hatch_color)
        bar.set_hatch('//')
    ax.invert_yaxis()
    plt.tight_layout()
    plt.show()
    

    PS:获得轮廓的更简单的版本可能是两次绘制条的轮廓版本,然后更新首先绘制的条的颜色和阴影(第一个条存储在 ax.containers[0] 中) .

    for _ in range(2):
        df.plot.barh(ax=ax, position=0.50, width=0.3, legend=False, fc='none', ec='black', lw=2)
    rcParams['hatch.linewidth'] = 4
    for bar, main_color, hatch_color in zip(ax.containers[0], first_colors, second_colors):
        bar.set_linewidth(0)
        bar.set_facecolor(main_color)
        bar.set_edgecolor(hatch_color)
        bar.set_hatch('//')
    

    【讨论】:

    • 正是我想要的!我到处寻找这个。线粗的技巧做到了。
    【解决方案2】:

    这可以通过在barh 中使用facecolor 和edgecolor 参数来实现。我看到您在subplots 中使用了以下参数。使用以下解决方案重新创建和生成该图:

    %matplotlib
    
    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    
    df = pd.DataFrame({"P1_P2": [0.20],
                       "P1_P3": [0.16],
                       "P2_P5": [0.12],
                       "P3_P5": [0.06],
                       "P3_P6": [0.06],
                       "P5_P6": [0.06]})
    df=df.T
    
    fig, ax = plt.subplots(dpi=600, figsize=(4, 4), nrows=1, ncols=1)
    
    df.plot.barh(ax=ax, position=0.50, width=0.3, facecolor="red", 
                         edgecolor="blue")
    ax.get_legend().remove()
    
    plt.show()
    

    要在颜色之间添加条纹,可以使用参数hatch,如以下代码中所述:

    %matplotlib
    
    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    
    df = pd.DataFrame({"P1_P2": [0.20],
                       "P1_P3": [0.16],
                       "P2_P5": [0.12],
                       "P3_P5": [0.06],
                       "P3_P6": [0.06],
                       "P5_P6": [0.06]})
    df=df.T
    
    fig, ax = plt.subplots(dpi=600, figsize=(4, 4), nrows=1, ncols=1)
    
    df.plot.barh(ax=ax, position=0.50, width=0.3, facecolor="red", 
                         edgecolor="blue", hatch=r'//')
    ax.get_legend().remove()
    
    plt.show()
    

    情节如下所示:

    如果您想增加或减少线宽,可以使用plt.rcParams["hatch.linewidth"]进行更改

    plt.rcParams["hatch.linewidth"]= 4
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-14
      • 2017-01-15
      • 2012-03-26
      • 2012-08-09
      • 2017-11-21
      • 2013-05-15
      相关资源
      最近更新 更多