如何在具有x,y,label_color,label_marker列的数据框中分散绘图数据,以使x,y为坐标,相同的label_color值提供相同的颜色(或label_color为颜色),相同的label_marker提供相同的标记(或label_marker是标记符号)。理想情况下,图例将具有有关此跨产品标签的信息。
我已经尝试过df.hvplot.scatter(x="x", y="y", c="label_color")
,但是效果很好。我尝试使用s =“ label_size”代替标记,但是我希望有一个清晰的标记[pan]。这也给人一个奇怪的传说。我进一步尝试了df.hvplot(kind="points", x="1",y="2", c="label_color", s="label_size", by="other_label")
。
例如
df = pd.DataFrame([[1, 1, "a", "A"],
[2, 2, "a", "B"],
[3,3, "b", "C"],
[3,4, "b", "C"]], columns=["x", "y", "color_label", "marker_label"])
scatter_by_color_and_marker(df).redim.range(x=(0,5), y=(0,5))
更新: 我实现了以下内容。欢迎更简单的实现/增强功能
class PianoIter(object):
def __init__(self, max_iter=100):
self.max_iter = max_iter
def __iter__(self):
self.a = 1
return self
def next(self):
x = self.a
self.a += 1
if x>self.max_iter:
raise Exception("too large iteration")
return x
from itertools import cycle as Cycle
class DictCycle(dict):
def __init__(self, d=None, cycle=None):
d = d if d is not None else {}
super(DictCycle, self).__init__(d)
self.fixed_keys = d.keys()
self.cycle = Cycle(cycle) if cycle is not None else iter(PianoIter())
def __getitem__(self, label):
if label not in self:
value = next(self.cycle)
self[label] = value
return value
else:
return self.get(label)
def scatter_by_color_and_marker(df, x_label="x", y_label="y", color_label="color_label", marker_label="marker_label", color_dict=None, marker_dict=None):
color_dict = DictCycle(d=color_dict, cycle=hv.Cycle().values)
marker_dict = DictCycle(d=marker_dict, cycle=["o", "+", "d", "*", "s", "v", "^", "<", ">", "x"])
l = []
for (c_l, m_l), df_i in df.groupby([color_label, marker_label]):
c = color_dict[c_l]
m = marker_dict[m_l]
l.append(df_i.hvplot.scatter(x=x_label, y=y_label).options(color=c, marker=m))
for c_l, c in color_dict.items():
l.append(hv.Curve([], label="C "+str(c_l)).opts(line_color=c))
for m_l, m in marker_dict.items():
l.append(hv.Points([None,None], label="M "+str(m_l)).opts(marker=m, color="black"))
return hv.Overlay(l)