Matplotlib的LineCollection绘图函数介绍
Matplotlib是一个Python编程语言的2D绘图库,相对于其他绘图库,Matplotlib具有简单易用,性能强大,生成贴近出版品质量的图像,同时兼容numpy数组和Python编程语言中的其他数据结构等众多优点,被广泛应用于各种领域的数据可视化分析和图像处理中。LineCollection是Matplotlib中一种绘图方式,用于绘制高效显示一组线段的图像,由于Fast Line Styles的特殊情况下,该绘图方式绘出的线条较为稀疏,很难或者无法满足某些需求。本文将介绍Matplotlib中如何为LineCollection添加line markers。
阅读更多:Matplotlib 教程
Matplotlib之LineCollection的使用
首先,我们来介绍如何使用LineCollection绘制一个简单的折线图。在导入matplotlib库之后添加如下代码:
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
import numpy as np
fig, ax = plt.subplots()
x = np.linspace(0,3*np.pi,500)
y1 = np.sin(x)
y2 = np.sin(x+np.pi/4)
points = np.array([x,y1]).T.reshape(-1,1,2)
segments = np.concatenate([points[:-1],points[1:]],axis=1)
lc = LineCollection(segments,linewidths=2)
ax.add_collection(lc)
运行上述代码,即可绘制出一条sin函数的折线图,如下图所示:
1 +-+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+-+
+ ***** 圆点标记 +
| **** 钻石标记 |
| *** 六边形标记|
0.6 _ * 三角标记 |
|-+ ******** 矩形标记|
|- --------------------+
0.2 +-+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+-+
0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30
LineCollection的设置方法
如上述所示,通过segments参数可设置线条的起始点和终止点;通过linewidths参数可设置线条的粗细程度;通过colors参数可设置线条的颜色;通过antialiased参数可设置线条是否抗锯齿;通过zorder参数可设置线条在坐标轴上的绘制顺序。同时,Matplotlib还支持十几种种类的线条样式,如:
– “solid” : “–”
– “dotted” : “-.”
– “dashed” : “–”
– “dashdot” : “-.”
但是,LineCollection不能直接设置line markers,所以我们需要自定义”make_dashed_lines”函数,添加line markers。函数代码如下:
from matplotlib import marker_styles
def make_dashed_lines(x,y,length,on='',off='',converter=None,coords=None):
"""
x:1D array, y: 1D array, length: float or tuple, on: str, off: str, converter: callable object, coords: str
"""
if not np.iterable(length):
length = (length,)
if len(length)==1:
gap = length[0]
dashes = [0,gap]
else:
gap = sum(length)%2
dashes = np.sum([[0,l] for l in length],[])
nsegements = len(x)-1
segments = np.zeros((nsegements,2,2))
segments[:,0,0] = x[:-1]
segments[:,1,0] = x[1:]
segments[:,0,1] = y[:-1]
segments[:,1,1] = y[1:]
gc = LineCollection(segments, linestyle='--', dash_capstyle='round', dash_joinstyle='round')
if on or off:
gc.set_dashes(dashes,1)
if on:
on = marker_styles.get(on, on)
gc.set_dashes(dashes,0,dash_capstyle='round', dash_joinstyle='round')
gc.set_marker(on)
if off:
off = marker_styles.get(off, off)
gc.set_dashes(dashes,1,dash_capstyle='round', dash_joinstyle='round')
gc.set_marker(off)
if converter or coords:
gc = gc.convert(converter=converter, coords=coords)
return gc
该函数定义了各个参数以及细节设置,通过调用make_dashed_lines函数,可以向LineCollection中添加line markers。比如,我们可以用make_dashed_lines函数来为折线图添加不同的标记,代码如下:
fig, ax = plt.subplots()
x = np.linspace(0,3*np.pi,500)
y1 = np.sin(x)
y2 = np.sin(x+np.pi/4)
points = np.array([x,y1]).T.reshape(-1,1,2)
segments = np.concatenate([points[:-1],points[1:]],axis=1)
lc1 = make_dashed_lines(x,y1,length=20,on='o',off='')
lc2 = make_dashed_lines(x,y2,length=20,on='D',off='')
ax.add_collection(lc1)
ax.add_collection(lc2)
添加完标记后,折线图变为:
1 +-+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+-+
+ D +
| ***|
| * o |
0.6 _ * |
|-+ ***** |
|-+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+-+
0.2 +-+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+-+
0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30
总结
在本文中,我们介绍了Matplotlib中的LineCollection函数,它是一种较为高效的绘制线段图像的方式。但是,LineCollection函数并不支持line markers,为此我们自定义了make_dashed_lines函数,并介绍了对应的参数和细节设置。通过使用make_dashed_lines函数,我们成功为LineCollection添加了line markers。在数据可视化分析中,对于一些需要一眼就能看出特点的数据,行业自定义的line markers可以充当一种有力的视觉辅助手段。
极客教程