Python中图中的列数据错误

2024-05-19 10:10:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用子图在一个图形中绘制多个图形。它看起来像我想要的那样,但是x轴上的数据是错误的。它不是采用“时间”值,而是采用使用pandas导入的csv文件的索引数。时间仅为0到7毫秒。csv被导入为200000行和5列,其中包括一列索引编号和4列,我想用它们来打印。我已附上我的代码。有人能帮我弄清楚吗

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pathlib import Path 


def function(x,y,z,v,Plot_ShareY=True):    #x, y, z, v is filename 
    data_1 = Path(x)    #stores the relative path location
    data_2 = Path(y)
    data_3 = Path(z)
    data_4 = Path(v)
    print(data_1)       #prints the file location for each input argument
    print(data_2)
    print(data_3)
    print(data_4)
    
    x1 = pd.read_csv(data_1, delimiter=";", skiprows=(1), decimal = ",", na_values = ['no info', '.'])  #loads the csv file from the path created above
    y1 = pd.read_csv(data_2, delimiter=";", skiprows=(1), decimal = ",", na_values = ['no info', '.'])
    z1 = pd.read_csv(data_3, delimiter=";", skiprows=(1), decimal = ",", na_values = ['no info', '.'])
    v1 = pd.read_csv(data_4, delimiter=";", skiprows=(1), decimal = ",", na_values = ['no info', '.'])
    
    time1 = x1.iloc[:,0]
    time2 = y1.iloc[:,0]
    time3 = z1.iloc[:,0]
    time4 = v1.iloc[:,0]
    
    resA_x1 = x1.iloc[:,1]
    resB_x1 = x1.iloc[:,2]
    resC_x1 = x1.iloc[:,3]
    resD_x1 = x1.iloc[:,4]
    
    resA_y1 = y1.iloc[:,1]
    resB_y1 = y1.iloc[:,2]
    resC_y1 = y1.iloc[:,3]
    resD_y1 = y1.iloc[:,4]
    
    resA_z1 = z1.iloc[:,1]
    resB_z1 = z1.iloc[:,2]
    resC_z1 = z1.iloc[:,3]
    resD_z1 = z1.iloc[:,4]
    
    resA_v1 = v1.iloc[:,1]
    resB_v1 = v1.iloc[:,2]
    resC_v1 = v1.iloc[:,3]
    resD_v1 = v1.iloc[:,4]
    
    
    fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 1, figsize=(12,8))
    fig.suptitle('Drops')   
    
    ax1.plot(time1, resA_x1, 'r', resB_x1, 'g', resC_x1, 'b', resD_x1, 'y')
    ax1.set_ylabel('A [V]')
       
    ax2.plot(time2, resA_y1, 'r', resB_y1, 'g', resC_y1, 'b', resD_y1, 'y')
    ax2.set_ylabel('B [V]') 
    
    ax3.plot(time3, resA_z1, 'r', resB_z1, 'g', resC_z1, 'b', resD_z1, 'y')
    ax3.set_ylabel('C [V]')
    
    ax4.plot(time4, resA_v1, 'r', resB_v1, 'g', resC_v1, 'b', resD_v1, 'y')
    ax4.set_xlabel('Time [s]')
    ax4.set_ylabel('D [V]') 
     
    
function('above-1-cm-A1-3.csv','above-1-cm-B2-1.csv', 'above-1-cm-C3-1.csv', 'above-1-cm-D4-1.csv')

Tags: csvpathdataabovepdv1x1set
1条回答
网友
1楼 · 发布于 2024-05-19 10:10:05

您需要为每个数据集指定相同的x轴:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pathlib import Path 

def function(x,y,z,v,Plot_ShareY=True):    #x, y, z, v is filename 
    data_1 = Path(x)    #stores the relative path location
    data_2 = Path(y)
    data_3 = Path(z)
    data_4 = Path(v)
    print(data_1)       #prints the file location for each input argument
    print(data_2)
    print(data_3)
    print(data_4)

    #loads the csv file from the path created above
    x1 = pd.read_csv(data_1, delimiter=";", skiprows=(1, 2), decimal=",", na_values=['no info', '.'])  
    y1 = pd.read_csv(data_2, delimiter=";", skiprows=(1, 2), decimal=",", na_values=['no info', '.'])
    z1 = pd.read_csv(data_3, delimiter=";", skiprows=(1, 2), decimal=",", na_values=['no info', '.'])
    v1 = pd.read_csv(data_4, delimiter=";", skiprows=(1, 2), decimal=",", na_values=['no info', '.'])

    time1 = x1['Time']
    time2 = y1['Time']
    time3 = z1['Time']
    time4 = v1['Time']
    
    resA_x1 = x1['Channel A']
    resB_x1 = x1['Channel B']
    resC_x1 = x1['Channel C']
    resD_x1 = x1['Channel D']
    
    resA_y1 = y1['Channel A']
    resB_y1 = y1['Channel B']
    resC_y1 = y1['Channel C']
    resD_y1 = y1['Channel D']
    
    resA_z1 = z1['Channel A']
    resB_z1 = z1['Channel B']
    resC_z1 = z1['Channel C']
    resD_z1 = z1['Channel D']
    
    resA_v1 = v1['Channel A']
    resB_v1 = v1['Channel B']
    resC_v1 = v1['Channel C']
    resD_v1 = v1['Channel D']

    fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 1, figsize=(12,8))
    fig.suptitle('Drops')   
    ax1.plot(time1, resA_x1, 'r', time1, resB_x1, 'g', time1, resC_x1, 'b', time1, resD_x1, 'y')
    ax1.set_ylabel('A [V]')

    ax2.plot(time2, resA_y1, 'r', time2, resB_y1, 'g', time2, resC_y1, 'b', time2, resD_y1, 'y')
    ax2.set_ylabel('B [V]') 

    ax3.plot(time3, resA_z1, 'r', time3, resB_z1, 'g', time3, resC_z1, 'b', time3, resD_z1, 'y')
    ax3.set_ylabel('C [V]')

    ax4.plot(time4, resA_v1, 'r', time4, resB_v1, 'g', time4, resC_v1, 'b', time4, resD_v1, 'y')
    ax4.set_ylabel('D [V]') 
      
    plt.show()
    
function('trial.csv', 'trial.csv', 'trial.csv', 'trial.csv')    

给你:

Matplot lib output

如果还使用skiprows=(1, 2),则也可以使用标题列


这可以完全概括为允许传递不同数量的CSV文件:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pathlib import Path 

def function(*xyzv, Plot_ShareY=True):    #x, y, z, v is filename 

    # data paths
    dps = [Path(f) for f in xyzv]
    
    for dp in dps:
        print(dp)

    #dataframes - loads the csv file from the paths created above
    dfs = [pd.read_csv(dp, delimiter=";", skiprows=(1, 2), decimal=",", na_values=['no info', '.']) for dp in dps]
    times = [df['Time'] for df in dfs]
    y_labels = [f'{dp} [V]' for dp in dps]    # e.g. base it on the filename
    ch = [('Channel A', 'r'), ('Channel B', 'g'), ('Channel C', 'b'), ('Channel D', 'y')]    

    fig, *axes = plt.subplots(len(dfs), 1, figsize=(12, 8))
    fig.suptitle('Drops')   

    for x, ax, df, y_label in zip(times, axes[0], dfs, y_labels):
        for channel, colour in ch:
            ax.plot(x, df[channel], colour)
        ax.set_ylabel(y_label)
        
    plt.show()
    
function('trial.csv', 'trial.csv', 'trial.csv', 'trial.csv')    
function('trial.csv', 'trial.csv')    

相关问题 更多 >

    热门问题