使用LineCollection时添加行标记

pae*_*uin 6 python matplotlib

我正在使用LineCollectionmatplotlib快速绘制大量的线条并使用不同的颜色.但是,即使在查看LineCollection文档之后,我也找不到为行设置行标记的方法.使用LineCollection时有没有办法让行标记?

注意:使用pyplot.plot()不是一个选项,因为它对于我的用例来说太慢了,它正在绘制大约200k行.

插图示例: 在此输入图像描述

用于生成示例的代码(原始源):

import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection

lines = [[(0, 1), (1, 1)], [(2, 3), (3, 3)], [(1, 2), (1, 3)]]

lc = LineCollection(lines, colors=['r', 'g', 'b'])
fig = plt.figure()

ax1 = fig.add_subplot(1, 2, 1)
ax1.add_collection(lc)
ax1.autoscale()
ax1.set_title('Current')

# Doesn't seem to do anything
for l in ax1.lines:
    l.set_marker('o')

ax2 = fig.add_subplot(1, 2, 2)
ax2.plot([0, 1], [1, 1], 'ro-')
ax2.plot([2, 3], [3, 3], 'go-')
ax2.plot([1, 1], [2, 3], 'bo-')
ax2.set_title('Goal')

plt.show()
Run Code Online (Sandbox Code Playgroud)

tmd*_*son 6

我认为您不能在中添加标记LineCollection。但是,使用ax.scatter标记在顶部绘制LineCollection可能比使用标记更快ax.plot

例如,类似:

import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection

lines = [[(0, 1), (1, 1)], [(2, 3), (3, 3)], [(1, 2), (1, 3)]]
colors = ['r', 'g', 'b']

lc = LineCollection(lines, colors=['r', 'g', 'b'])
fig = plt.figure()

ax1 = fig.add_subplot(1, 1, 1)
ax1.add_collection(lc)
ax1.autoscale()

x = [i[0] for j in lines for i in j]
y = [i[1] for j in lines for i in j]
c = [col for col in colors for _ in (0, 1)]

ax1.scatter(x, y, c=c)

plt.show()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明