python - 循环内的标签和着色图 - matplotlib

标签 python matplotlib plot

我正在使用 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)

关于python - 循环内的标签和着色图 - matplotlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20639852/

相关文章:

python - 如何使用Google Oauth2 access_token?

python - matplotlib: "TypeError: Image data can not convert to float"看起来像一个精细矩阵

matplotlib - 描述 Julia 中二维空间中数据点密度的图

r - 在 rCharts + polychart 中手动设置点颜色

python - 用于 Python 的 XML 编写工具

Python类变量把一个函数变成了一个方法,为什么?

python - 使用 Beautiful Soup 和 Python 解析元标记

python - 使用matplotlib.pyplot制作一维波动方程的动画

Python matplotlib - Linux 上的大窗口

r - 如何像箱线图一样在 R 中创建分类散点图?