代码之家  ›  专栏  ›  技术社区  ›  Nikko

Matplotlib正在打印两次/多次线条图

  •  1
  • Nikko  · 技术社区  · 5 年前

    如果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 .

    1 回复  |  直到 5 年前
        1
  •  1
  •   CDJB tohasanali    5 年前

    这是因为阴谋 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:] 只给出没有第一行的数据帧。更改的每个实例 数据框[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