matplotlib一张图表上的多行不知道怎么做?

2024-05-08 13:51:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我可以使用Matplotlib站点的示例来了解如何做到这一点,但这仅适用于3行,如果有100行,会发生什么?我不知道用python循环怎么做。所以布局应该是

3/3/15 aa 10
3/4/15 aa 20
3/5/15 aa 30
3/3/15 bb 11
3/4/15 bb 21
3/5/15 bb 31
... 100 more of same 
3/3/15 cc 101
3/4/15 cc 102
3/5/15 cc 103

这是Matplotlib站点的代码,但我不知道如何循环使用我的数据集。你知道吗

plt.plot(x, np.sin(x) + x + np.random.randn(50))
plt.plot(x, np.sin(x) + 0.5 * x + np.random.randn(50))
plt.plot(x, np.sin(x) + 2 * x + np.random.randn(50))

plt.show()

Tags: of示例plot站点matplotlibmorenpplt
0条回答
网友
1楼 · 发布于 2024-05-08 13:51:30

plt.plot()通过输入x和y值来工作。假设'aa'、'bb'、'cc'值是列表中的y值,您可以执行以下操作:

for yval in list1:
    plt.plot(x, yval)

plt.show()
网友
2楼 · 发布于 2024-05-08 13:51:30

如果您的数据看起来像您在问题中包含的内容,则首先需要将其划分为要绘制的列表或数组。完成后,这样的示例代码应该可以工作:

import matplotlib.pyplot as plt
import numpy as np

time = np.arange(0,500)
aa = np.random.randn(500)
bb = np.random.randn(500)
cc = np.random.randn(500)
data = [aa,bb,cc]

plt.figure()
for y in data:
    plt.plot(time,y)

plt.show()  

Sample plot results

相关问题 更多 >

    热门问题