使用 LineCollection 时添加线标记 [英] Adding line markers when using LineCollection

查看:41
本文介绍了使用 LineCollection 时添加线标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

用于生成示例的代码(

I'm using LineCollection in matplotlib to plot a large number of lines quickly and with different colors. However, I can't find any way to set a line marker for the lines, even after looking at the LineCollection documentation. Is there any way to have line markers when using LineCollection?

Note: Using pyplot.plot() is not an option as it's too slow for my use case, which is plotting about 200k lines.

Illustrated example:

Code used to generate example (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()

解决方案

I don't think you can add markers to a LineCollection. However, using ax.scatter to plot your markers on top of your LineCollection would probably be quicker than using ax.plot

For example, something like:

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()

这篇关于使用 LineCollection 时添加线标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆