我在matplotlib中使用LineCollection
来快速绘制大量的线条并使用不同的颜色。但是,即使在查看LineCollection文档之后,我也找不到为行设置行标记的方法。使用LineCollection时有没有办法让行标记?
注意:使用pyplot.plot()不是一个选项,因为它对我的用例来说太慢了,因为它正在绘制大约200k行。
用于生成示例的代码(original source):
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()
答案 0 :(得分:4)
我认为您无法向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()