如何在python中以相同的颜色绘制同一图形中的10条迹线? [英] How to plot 10 traces in same figure with different color in python?
问题描述
我需要在python中绘制10条具有不同颜色的迹线,每条迹线都位于具有相同扩展名.numpy.的不同文件中,这意味着我有10个文件:
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?
先谢谢了.
推荐答案
不,您不必将所有内容都放在同一个文件中.您可以简单地遍历文件列表,然后绘制到相同的axes
中.对于颜色,如果您只是为colormap
抓取颜色,这是最简单的.这是一个小例子:
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()
这篇关于如何在python中以相同的颜色绘制同一图形中的10条迹线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!