代码之家  ›  专栏  ›  技术社区  ›  user7345804

为什么散点图中点的颜色与相应图例中点的颜色不匹配?

  •  2
  • user7345804  · 技术社区  · 7 年前

    通过下面的代码,我通过matplotlib获得了一个散点图示例。

    import numpy as np
    import matplotlib.pyplot as plt
    
    x = np.linspace(0, 100, 501)
    y = np.sin(x)
    
    label = 'xy data sample'
    
    plt.scatter(x, y, cmap='plasma', c=x, label=label)
    legend_dict = dict(ncol=1, loc='best', scatterpoints=4, fancybox=True, shadow=True)
    plt.legend(**legend_dict)
    plt.show()
    

    运行上面的代码将生成下面的绘图。

    enter image description here

    已成功绘制颜色贴图,但图例显示的点全部为蓝色,而不是与所选颜色贴图对应的颜色中的点。为什么会发生这种情况?

    我试着把 cmap='plasma' 在里面 legend_dict ,但它会导致以下错误。

    File "/Users/.../
    site-packages/matplotlib/axes/_axes.py", line 550, in legend
        self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
    TypeError: __init__() got an unexpected keyword argument 'cmap'
    

    编辑:

    我想要的输出是通过所选的颜色映射将图例中表示的四个点变成不同的颜色。理想情况下, cmap='plasma' 在本例中,可以使用类似于蓝点、紫点、橙红点、黄点的内容生成图例。虽然色条可能是一种可能的替代品,但我还没有浏览任何有关色条的文档。

    1 回复  |  直到 7 年前
        1
  •  2
  •   ImportanceOfBeingErnest    7 年前

    色条可以通过以下方式实现 plt.colorbar() 。这将允许直接看到与颜色对应的值。

    让图例中的点显示不同的颜色当然也很好,尽管它不允许提供任何定量信息。

    不幸的是,matplotlib没有提供任何内置的方法来实现这一点。因此,一种方法是对用于创建图例句柄的图例处理程序进行子类化,并实现此功能。

    在这里,我们创建 ScatterHandler 带有自定义 create_collection 方法,在其中创建所需的 PathCollection 并通过在 legend_map 图例词典。

    handler_map={ type(sc) : ScatterHandler()}
    

    下面的代码乍一看似乎有点复杂,但是您可以简单地复制类而不完全理解它,并在代码中使用它。

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.legend_handler import HandlerRegularPolyCollection
    
    class ScatterHandler(HandlerRegularPolyCollection):
        def update_prop(self, legend_handle, orig_handle, legend):
            legend._set_artist_props(legend_handle)
            legend_handle.set_clip_box(None)
            legend_handle.set_clip_path(None)
    
        def create_collection(self, orig_handle, sizes, offsets, transOffset):
            p = type(orig_handle)([orig_handle.get_paths()[0]],
                                  sizes=sizes, offsets=offsets,
                                  transOffset=transOffset,
                                  cmap=orig_handle.get_cmap(),
                                  norm=orig_handle.norm )
    
            a = orig_handle.get_array()
            if type(a) != type(None):
                p.set_array(np.linspace(a.min(),a.max(),len(offsets)))
            else:
                self._update_prop(p, orig_handle)
            return p
    
    
    x = np.linspace(0, 100, 501)
    y = np.sin(x)*np.cos(x/50.)
    
    sc = plt.scatter(x, y, cmap='plasma', c=x, label='xy data sample')
    
    legend_dict = dict(ncol=1, loc='best', scatterpoints=4, fancybox=True, shadow=True)
    plt.legend(handler_map={type(sc) : ScatterHandler()}, **legend_dict)
    
    plt.show()
    

    enter image description here