我正在使用 for 循环在同一轴上绘制来自多个文件的多个滞后循环,我想知道如何为每个循环设置不同的标签和颜色。每个循环实际上由两行组成。相关代码为:
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 条不同的滞后曲线,具有相同的轴标签和比例以及所有内容。不幸的是,所有的线条都是相同的颜色。我想让每条曲线(从文件上下)有不同的颜色和不同的标签,但我不知道该怎么做。
请您参考如下方法:
首先你需要一个颜色列表,每个文件一个:
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)
