Matplotlib中带影线的多色图例条目

pvcm50d1  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(73)

我正在查看另一个StackOverflow post的代码,它向您展示了如何创建具有多种颜色的图例条目。我试着调整代码以在图例中显示阴影(//xx等),但无法使其工作。下面是我添加一个舱口的尝试。
注意:这只是一个最小的例子,我们的想法是让第一个矩形有阴影,第二个颜色矩形没有阴影,反之亦然。这个例子是为了帮助我弄清楚如何将它添加到补丁(当需要时)。

import matplotlib.pyplot as plt
from matplotlib.collections import PatchCollection

# define an object that will be used by the legend
class MulticolorPatch(object):
    def __init__(self, colors):
        self.colors = colors
        
# define a handler for the MulticolorPatch object
class MulticolorPatchHandler(object):
    def legend_artist(self, legend, orig_handle, fontsize, handlebox):
        width, height = handlebox.width, handlebox.height
        patches = []
        for i, c in enumerate(orig_handle.colors):
          patches.append(
              plt.Rectangle(
                  [
                      width/len(orig_handle.colors) * i - handlebox.xdescent, 
                      -handlebox.ydescent
                  ],
                  width / len(orig_handle.colors),
                  height, 
                  facecolor=c, 
                  edgecolor="black",
                  linewidth=0.5,
                  hatch='//'
              )
          )

        patch = PatchCollection(patches, match_original=True)
        handlebox.add_artist(patch)

        return patch

# ------ choose some colors
colors1 = ['r', 'g']
colors2 = ['b', 'y']

# ------ create a dummy-plot (just to show that it works)
f, ax = plt.subplots()
ax.plot([1,2,3,4,5], [1,4.5,2,5.5,3], c='g', lw=0.5, ls='--',
        label='... just a line')
ax.scatter(range(len(colors1)), range(len(colors1)), c=colors1)
ax.scatter([range(len(colors2))], [.5]*len(colors2), c=colors2, s=50)

# ------ get the legend-entries that are already attached to the axis
h, l = ax.get_legend_handles_labels()

# ------ append the multicolor legend patches
h.append(MulticolorPatch(colors1))
l.append("a nice multicolor legend patch")

h.append(MulticolorPatch(colors2))
l.append("and another one")

# ------ create the legend
f.legend(h, l, loc='upper left', 
         handler_map={MulticolorPatch: MulticolorPatchHandler()}, 
         bbox_to_anchor=(.125,.875))

字符串
这会产生以下结果,即使我的意图是有舱口:
x1c 0d1x的数据
我如何修改它以添加图案填充?
提前感谢!

e5nszbig

e5nszbig1#

目前无法为集合设置单独的图案填充,请参阅文档:
例外的是zorder、hatch、pickradius、capstyle和joinstyle属性,它们只能为整个集合全局设置。
解决方案是不使用集合:

class MulticolorPatchHandler(object):
    def legend_artist(self, legend, orig_handle, fontsize, handlebox):
        width, height = handlebox.width, handlebox.height
        for i, c in enumerate(orig_handle.colors):
          handlebox.add_artist(
              plt.Rectangle(
                  [
                      width/len(orig_handle.colors) * i - handlebox.xdescent, 
                      -handlebox.ydescent
                  ],
                  width / len(orig_handle.colors),
                  height, 
                  facecolor=c, 
                  edgecolor="black",
                  linewidth=0.5,
                  hatch='//' if i==0 else None
              )
          )
        return handlebox

字符串
下面是使用问题中的示例数据的输出:


的数据

相关问题