2013-12-17 34 views
1

我使用for循環繪製了來自同一軸上多個文件的多個滯後循環,我想知道如何爲每個循環使用不同的標籤和顏色。每個循環實際上由兩條線組成。相關的代碼是:循環內標記和着色圖 - matplotlib

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

filelist = ['file1', 'file2', 'file3'] 

fig = plt.figure() 
axes = fig.add_subplot(111) 

for item in filelist: 
    filename = item 
    #import hyst files 
    up = pd.read_csv(filename) 
    up.columns = ['Field','Moment','Temperature'] 

    upcut = up2[:cut_loc -1] #cuts the file in 2 
    down = up2[cut_loc +1:] 

    upcutsrt = (upcut.sort(['Field'])) 

    upcutsrt.plot(x='Field', y='Moment', ax=axes) 
    down.plot(x='Field', y='Moment', ax=axes) 

ax1.set_xlabel('Magnetic Field', fontsize = 20, labelpad = 15) 
ax1.set_ylabel('Magnetic Moment', fontsize=20, labelpad = 15) 

plt.show() 

所以這個地塊上具有相同的軸標籤和規模,一切都在同一座標3條不同的磁滯回線。不幸的是,所有的線都是相同的顏色。我希望每條曲線(從文件上下)都具有不同的顏色和不同的標籤,但我不知道如何去做。

回答

1

那麼首先您需要的顏色列表,每個文件之一:

colors = ['red', 'green', 'blue'] 

然後修改for循環:

for item,col in zip(filelist,colors): 

,並最終繪製使用的顏色:

upcutsrt.plot(x='Field', y='Moment', ax=axes, color=col) 
    down.plot(x='Field', y='Moment', ax=axes, color=col) 
+0

你可以用正確的縮進格式來使它更具可讀性嗎? – tacaswell