【问题标题】:how to fill pcolor with hatches by mapping it alongwith color fill如何通过将其与颜色填充一起映射来用阴影填充 pcolor
【发布时间】:2021-03-15 01:33:52
【问题描述】:

您好,我想继续我已经在这里提出的问题 Create vertical stacked bar chart referenced to y values (lithology/stratigraphic column)

问题是如何在我的 pcolor 上制作多个影线,这是图片

我想用“x”图案制作白色的 pcolor,用“+”制作红色的 pcolor。

有什么办法吗?提前谢谢你

lithcode = {'ABT': 1, 'AT': 2, 'GD': 3, 'BAT': 4, 'BTT': 5, 'TT': 6, 'NC': 7, 'MV':8, 'MS':9, 'GW':10}
colors = ['darkred', 'crimson', 'red', 'peru', 'lightsalmon', 'lightpink', 'white', 'oldlace', 
          'thistle', 'lightgray']
hatch=['','+','','','','','x','','','']
cmap = ListedColormap(colors)


Zb=litho['litho'].map(lithcode).to_numpy().reshape(1, -1)
nonan=np.count_nonzero(Zb[np.logical_not(np.isnan(Zb))])
Za=litho['litho'][:nonan].map(lithcode).to_numpy().reshape(1, -1)
X=litho['elev litho'][:nonan]
cl.pcolor(X, [0, 1], Za, cmap=cmap, vmin=1, vmax=len(colors))

# ax.set_yticks(data['md litho']) optionally set the y-ticks to indicate the row borders
hands = [Patch(color=col, label=k) for k, col in zip(lithcode.keys(), colors)]
cl.legend(handles=hands, loc=(1.01, 0), ncol=2, fontsize=8)
        

【问题讨论】:

    标签: python numpy matplotlib plot colormap


    【解决方案1】:

    基于链接的示例数据,并基于How to hatch PolyCollection instance? 获取由pcolor 创建的PolyCollection 的孵化方法:

    import matplotlib.pyplot as plt
    from matplotlib.colors import ListedColormap
    from matplotlib.patches import Patch, PathPatch
    import pandas as pd
    
    df = pd.DataFrame({'md litho': [0, 31, 49, 67, 406, 427, 442],
                       'litho': ['NC', 'AT', 'BTT', 'NC', 'ABT', 'BAT', 'NC']})
    lithcode = {'ABT': 1, 'AT': 2, 'GD': 3, 'BAT': 4, 'BTT': 5, 'TT': 6, 'NC': 7, 'MV': 8, 'MS': 9, 'GW': 10}
    colors = ['darkred', 'crimson', 'red', 'peru', 'lightsalmon', 'lightpink', 'white', 'oldlace', 'thistle', 'lightgray']
    cmap = ListedColormap(colors)
    hatches = ['', '++', '', '', '', '', 'xx', '', '', '']
    
    fig, ax = plt.subplots(figsize=(12, 1.4))
    lithcode_values = df['litho'][:-1].map(lithcode).to_numpy()
    polycollection = ax.pcolor(df['md litho'], [0, 1], lithcode_values.reshape(1, -1), cmap=cmap, vmin=1, vmax=len(colors))
    ax.set_xticks(df['md litho'])  # optionally set the x-ticks to indicate the column borders
    ax.set_yticks([])  # hide ticks on the y-axis
    
    for path, d2_val in zip(polycollection.get_paths(), lithcode_values):
        hatch = hatches[d2_val - 1]
        if hatch != '':
            ax.add_patch(PathPatch(path, hatch=hatch, facecolor='none', edgecolor='black'))
    
    hands = [Patch(facecolor=col, label=k, edgecolor='black', hatch=hatch) for k, col, hatch in
             zip(lithcode.keys(), colors, hatches)]
    ax.legend(handles=hands, bbox_to_anchor=(1.03, 1.02), loc='upper left', ncol=2, fontsize=8, facecolor='PaleTurquoise')
    plt.tight_layout()  # this fits the legend and the labels nicely into the figure
    plt.show()
    

    PS:将matplotlib.pyplot 导入为cl 非常令人困惑。

    【讨论】:

    • 嗨,对不起@JohanC,我离开这个 python 工作有一段时间了。我在孵化 for 循环中仍然有问题。当我使用自己的脚本而不是您的示例时,有一条错误消息指出在 ---> 孵化 = 孵化 [d2_val - 1] '只有整数标量数组可以转换为标量索引'。其实我还在摸索中
    • 你能打印出lithcode_values 并检查它是一个整数数组吗?这样,上面代码中的d2_val 应该是一个整数。您可以通过代码更改来编辑您的问题吗?
    • 嘿@JohanC,它正在工作!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2022-12-09
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    • 1970-01-01
    • 2022-07-23
    • 1970-01-01
    相关资源
    最近更新 更多