Wlgls 冲鸭!

在同一坐标系中画多条曲线且偏移

2020-11-26
Q&A

目标:画出下图

图片源自论文Automatic Removal of Ocular Artifacts in the EEG without an EOG Reference Channel

image-20201126144049603

我想要在一个坐标系中绘制出多个波形进行比较,然后解决方法如下:

声明数据:

为了演示结果,此处创建一个三组波形,其数据组织格式及波形如下:

In[1]: data.shape
Out[1]: (3, 258)	# 即每一行一个波形

image-20201126144732036

方法一: 简单粗暴

简单的方法就是通过设置一个偏差然后在同一个坐标系中绘制,就是有点low,如下:

In[2]:  for i in range(3):
    	plt.plot(data[i]+i*3, c='black')
    	plt.yticks(range(0, 7, 3), list("abc"))

image-20201126145154230

方法二: 采用seaborn.facetGrid()方法

附上网页https://seaborn.pydata.org/examples/kde_ridgeplot.html

但是不想更改数据集,所以尝试使用上述方法, 但是这一方法需要对数据格式进行一定的修改。而且还复杂了。。。。

这种方法本质上还是多个坐标系,然后将左边的坐标轴和下面的坐标轴去掉。无论是使用matplotlib手动创建还是seaborn的FacetGrid方法,其都具有一定的局限性(当然也可能我不晓得怎么处理)

In[3]:	cat = np.array(list('abc')).reshape(-1, 1)
	  	cat = np.tile(cat, 258).flatten()
In[4]:	index = np.tile(np.arange(258).reshape(1, -1), 3).flatten()
In[5]:	df = pd.DataFrame({'x':index, 'y':data, 'z':cat})
In[6]:	pal = sns.cubehelix_palette(10, rot=-.25, light=.7)
        g = sns.FacetGrid(df, row="z", hue="z", aspect=15, height=1, palette=pal)

        def haddleaxes(x, color, label):
        ax.text(0.03, 0.5, label, fontweight='bold', color='black', ha='left',
        va='center', transform=ax.transAxes)
        g.set(yticks=[])
        g.despine(bottom=True, left=True)
        g.map(sns.lineplot, 'x', 'y')
        g.map(haddleaxes, 'x')

image-20201127115552594


Toc
Related