如何在 Python 中使用 matplotlib 绘制多条线图 [英] How to draw multiple line graph by using matplotlib in Python

查看:112
本文介绍了如何在 Python 中使用 matplotlib 绘制多条线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过在Python 2.7中使用matplotlib创建完整的12导联心电图,因此我已经写下了一些代码来表示每个导联(使用子图),但是在子图上绘制网格存在问题.然后,我尝试找到一种新的解决方案,以利用同一张图中的所有12条线索,如下图所示

I want to create a Full 12 Leads EKG graph by using matplotlib in Python 2.7, so I had already wrote down some code to represent each lead (using subplot), but it have an issue about drawing a grid on sub-graph. Then I try to find a new solution to ploy all 12 leads within the same graph like this picture below

我有这样的数据列表....

I have the list of data like this....

x = [1,2,3,4,5,....]
lead1 = [-39,-34,-36,-38,.... ]
lead2 = [-40,-44,-86,-28,.... ]
.
.
lead12 = [-30,-27,-80,-69,.... ]

你能给我一些示例代码或如何制作的方向.

Could you give me some example code or the direction of how to make that.

非常感谢您.

推荐答案

这是一个简单的示例,其中所有线都绘制在相同的轴上,但在 y 方向上偏移了 3 个单位.

Here is a simple example, where all lines are plotted in the same axes, but offset by 3 units in y direction.

import matplotlib.pyplot as plt
import numpy as np; np.random.seed(1)

s=3
x = np.linspace(0,10,2000)
a = np.sin(np.cumsum((np.random.normal(scale=0.1, size=(len(x), 12))), axis=0))

fig, ax = plt.subplots()
for i in range(a.shape[1]):
    ax.plot(x, a[:,i]+s*i)

labels = ["PG{}".format(i) for i in range(a.shape[1])]
ax.set_yticks(np.arange(0,a.shape[1])*s)
ax.set_yticklabels(labels)
for t, l in zip(ax.get_yticklabels(), ax.lines):
    t.set_color(l.get_color())

plt.show()

这篇关于如何在 Python 中使用 matplotlib 绘制多条线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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