如何在带有列x、y、label\u color、label\u marker的数据框中散点打印数据,以便x、y是坐标,相同的label\u color值给出相同的颜色(或label\u color是一种颜色),相同的label\u marker给出相同的标记(或label\u marker是一个标记符号)。理想的情况下,图例会有一些关于这个交叉产品标签的信息。你知道吗
我试过df.hvplot.scatter(x="x", y="y", c="label_color")
,效果很好。我试过用s=“label\u 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)
目前没有回答
相关问题 更多 >
编程相关推荐