简体   繁体   中英

matplotlib multiple lines on one chart don't get how to do it?

I can see how to do it using the example from Matplotlib site BUT that's only for 3 lines, what happens if you have 100? I can't figure out the way to do it with a python loop. So the layout would be

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

Here's the code from Matplotlib site but I don't really see how to loop over my data set.

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

plt.plot() works by entering an x and ay value. Assuming the 'aa', 'bb', 'cc' values are your y values in a list, you can do something like:

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

plt.show()

If your data looks like what you've included in the question, you will first need to separate it into list or arrays to be plotted. Once you have done that, a sample code like this should work:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM