Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

numpy - How to plot 10 traces in same figure with different color in python?

I need to plot 10 traces with different color in python, every trace is in a different file with the same extension .numpy., I mean by that that I have 10 files:

trace1
trace2
trace3
trace4
trace5
trace6
trace7
trace8
trace9
trace10

This is my code to plot just one trace:

import matplotlib.pyplot as plt 
import numpy as np
dataArray= np.load(r'/home/user/trace1.npy')
print(dataArray)
plt.plot(dataArray.T)
plt.show()

According to you must I put all of them in the same file? In order to plot them?

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

No, you don't have to put everything in the same file. You can simply loop over a list of files and plot into the same axes. For the color, it is the easiest if you simply grab a color for a colormap. Here is a little example:

import matplotlib.pyplot as plt 
import numpy as np
import matplotlib

# Read in list of files. You might want to look into os.listdir()
traces=[list of filepaths to your .npy files]

# Create figure 
fig=plt.figure()
fig.show()
ax=fig.add_subplot(111)

# Grab colormap
cmap = matplotlib.cm.get_cmap('jet')

# Loop through traces and plot them
for j,trace in enumerate(traces):

    # Load file
    dataArray= np.load(trace)

    # Grab color
    c=cmap(float(j)/len(traces))

    # Plot
    ax.plot(dataArray.T,color=c)

plt.show()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...