【发布时间】:2020-06-18 21:18:03
【问题描述】:
我正在尝试修改包含两个重叠散点图的图例。更具体地说,我想要两个图例句柄和标签:第一个句柄将包含多个点(每个点的颜色不同),而另一个句柄由一个点组成。
As per this related question,我可以修改图例句柄以显示多个点,每个点都是不同的颜色。
As per this similar question,我知道我可以更改指定句柄显示的点数。但是,这会将更改应用于图例中的所有句柄。可以只用在一个手柄上吗?
我的目标是结合这两种方法。有没有办法做到这一点?
如果不清楚,我想修改嵌入图(见下文),使Z vs X 句柄仅在相应的图例标签旁边显示一个点,而留下Y vs X句柄不变。
我制作这样一个数字的失败尝试如下:
要复制此图,可以运行以下代码:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.legend_handler import HandlerTuple, 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.arange(10)
y = np.sin(x)
z = np.cos(x)
fig, ax = plt.subplots()
hy = ax.scatter(x, y, cmap='plasma', c=y, label='Y vs X')
hz = ax.scatter(x, z, color='k', label='Z vs X')
ax.grid(color='k', linestyle=':', alpha=0.3)
fig.subplots_adjust(bottom=0.2)
handler_map = {type(hz) : ScatterHandler()}
fig.legend(mode='expand', ncol=2, loc='lower center', handler_map=handler_map, scatterpoints=5)
plt.show()
plt.close(fig)
我不喜欢的一个解决方案是创建两个图例 - 一个用于Z vs X,一个用于Y vs X。但是,我的实际用例涉及可选数量的句柄(可以超过两个),我不想计算每个图例框的最佳宽度/高度。还有什么办法可以解决这个问题?
【问题讨论】:
标签: python-3.x matplotlib plot legend scatter-plot