如何通过将 pcolor 与颜色填充一起映射来用影线填充 pcolor

how to fill pcolor with hatches by mapping it alongwith color fill

嗨,我想继续我已经在这里问过的问题

问题是如何在我的 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)
        

基于链接的示例数据,以及 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 非常令人困惑。