python - Matplotlib 正在打印线图两次/多次

标签 python pandas dataframe matplotlib scipy

如果 Matplotlib 打印线图两次或多次(如下所示),可能会出现什么问题:

enter image description here

这是我的代码:

import pandas as pd
import numpy as np
import scipy
import matplotlib.pyplot as plt
from scipy import integrate 


def compute_integrated_spectral_response_ikonos(file, sheet):
    df = pd.read_excel(file, sheet_name=sheet, header=2)

    blue = integrate.cumtrapz(df['Blue'], df['Wavelength'])
    green = integrate.cumtrapz(df['Green'], df['Wavelength'])
    red = integrate.cumtrapz(df['Red'], df['Wavelength'])
    nir = integrate.cumtrapz(df['NIR'], df['Wavelength'])
    pan = integrate.cumtrapz(df['Pan'], df['Wavelength'])


    plt.figure(num=None, figsize=(6, 4), dpi=80, facecolor='w', edgecolor='k')
    plt.plot(df[1:], blue, label='Blue', color='darkblue');
    plt.plot(df[1:], green, label='Green', color='b');
    plt.plot(df[1:], red, label='Red', color='g');
    plt.plot(df[1:], nir, label='NIR', color='r');
    plt.plot(df[1:], pan, label='Pan', color='darkred')
    plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0.)
    plt.xlabel('Wavelength (nm)')
    plt.ylabel('Spectral Response (%)')
    plt.title(f'Integrated Spectral Response of {sheet} Bands')
    plt.show()

compute_integrated_spectral_response_ikonos('Sorted Wavelengths.xlsx', 'IKONOS')

这是我的 dataset .

最佳答案

这是因为绘制 df[1:] 是将整个数据帧绘制为 x 轴。

>>> df[1:]
     Wavelength      Blue     Green       Red       NIR       Pan
1           355  0.001463  0.000800  0.000504  0.000532  0.000619
2           360  0.000866  0.000729  0.000391  0.000674  0.000361
3           365  0.000731  0.000806  0.000597  0.000847  0.000244
4           370  0.000717  0.000577  0.000328  0.000729  0.000435
5           375  0.001251  0.000842  0.000847  0.000906  0.000914
..          ...       ...       ...       ...       ...       ...
133        1015  0.002601  0.002100  0.001752  0.002007  0.149330
134        1020  0.001602  0.002040  0.002341  0.001793  0.136372
135        1025  0.001946  0.002218  0.001260  0.002754  0.118682
136        1030  0.002417  0.001376  0.000898  0.000000  0.103634
137        1035  0.001300  0.001602  0.000000  0.000000  0.089097

[137 rows x 6 columns]

切片[1:]仅给出没有第一行的数据帧。将 df[1:] 的每个实例更改为 df['Wavelength'][1:] 给出了我认为的预期输出:

>>> df['Wavelength'][1:]
1       355
2       360
3       365
4       370
5       375

133    1015
134    1020
135    1025
136    1030
137    1035
Name: Wavelength, Length: 137, dtype: int64

输出:

enter image description here

关于python - Matplotlib 正在打印线图两次/多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60079986/

相关文章:

Python:pandas数据框复杂的更新条件

python - 使用带有大 csv 的 pandas 结构(迭代和 block 大小)

python - 将字典转换为数据框,键作为列名,键的值作为数据框的列值

用 R 数据框中的匹配 ID 替换单元格

r - 比较 R 中的 3 个列列表

python - 从apache cgi python接收http post请求的xml内容

python - 通过 Tkinter GUI 使用加密技术

python - 打印 1 到 N - 位数

python - 在 Odoo 8 中,如何将模块 .py 文件导入自定义模块?

python - 从值列表中添加或更新 pandas 数据框