在matplotlib中向散点图添加连接线

时间:2020-02-28 22:53:51

标签: python matplotlib scatter-plot

我有一个功能可以为每个数据点有条件地自定义标记和颜色。

def mscatter(x,y,ax=None, m=None, **kw):
   import matplotlib.markers as mmarkers
   if not ax: ax=plt.gca()
   sc = ax.scatter(x,y,**kw)
   if (m is not None) and (len(m)==len(x)):
     paths = []
     for marker in m:
        if isinstance(marker, mmarkers.MarkerStyle):
            marker_obj = marker
        else:
            marker_obj = mmarkers.MarkerStyle(marker)
        path = marker_obj.get_path().transformed(
                    marker_obj.get_transform())
        paths.append(path)
    sc.set_paths(paths)
  return sc

然后我有一些关于x,y的数据,以及关于x,y的颜色和标记。

col = np.where(this_data['ifAgentCorrect'],"g","r")
mrkr = np.where(this_data['ifCorrectDecisions'],"o","x")
x = range(len(this_data['ifCorrectDecisions']))
y = this_data['cumSum']

plt.figure(figsize=(20, 8))
scatter = mscatter(x, y, c=col, m=mrkr)
plt.show()

我要做的就是将散点连接到线上。通常使用“ o-”,“ x-”等标记来完成此操作。但是matplotlib.markers的标记列表中没有'o-'或'x-'。所以我得到了错误ValueError: Unrecognized marker style o-。关于如何实现此目标的任何建议?谢谢。

0 个答案:

没有答案