【问题标题】:How to draw multiple line graph by using matplotlib in Python如何在 Python 中使用 matplotlib 绘制多条折线图
【发布时间】:2017-03-28 17:11:57
【问题描述】:

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

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

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

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

非常感谢。

【问题讨论】:

标签: python matplotlib ekg


【解决方案1】:

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

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

【讨论】:

  • 谢谢先生,我试试这个很好,但是当我使用 np.sin 作为你的代码 a = np.sin(np.cumsum((np.random.normal(scale= 0.1, size=(len(x), 12))), axis=0)) 我改成a = np.dstack((nlead1,nlead2,nlead3,nlead4,nlead5,nlead6,nlead7,nlead8,nlead9,nlead10 ,nlead11,nlead12)) 错误发生如下 ValueError: x and y must have same first dimension 你能给我一些建议吗?再次感谢您_/_
  • 通常可以认真对待错误消息。该错误告诉您要绘制的 x 和 y 长度不同。例如plt.plot([1,2,3],[1.2,1.1]) 不起作用,因为列表的长度不同。您需要确保是这种情况。
  • 所以我改变了 x = np.linspace(0,200,5500) "5500" 是每个 nlead1-12 中相同数量的值大小不确定方括号是否结束 data = [[ [ -39 47 86 ..., -100 -77 -3] [ -34 50 85 ..., -101 -75 -1] [ -36 49 86 ..., -103 -77 -4] .. ., [ 0 0 0 ..., 0 0 0] [ 0 0 0 ..., 0 0 0] [ 0 0 0 ..., 0 0 0]]]
猜你喜欢
  • 2017-01-02
  • 2019-10-17
  • 2021-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-27
  • 2021-11-01
  • 1970-01-01
相关资源
最近更新 更多