I am trying to plot multiple graphs in one figure using subplots. It looks the way I want but the data on x-axis is wrong. Instead of taking the values of ‘Time’ it is taking the number of indices of the csv file imported using pandas. The time is from 0 to 7 milliseconds only. csv is imported as 200000 rows and 5 columns which includes a column for index numbers and 4 columns which I want to use to plot. I have attached my code. could someone help me figure it out?
JavaScript
x
66
66
1
import pandas as pd
2
import numpy as np
3
import matplotlib.pyplot as plt
4
from pathlib import Path
5
6
7
def function(x,y,z,v,Plot_ShareY=True): #x, y, z, v is filename
8
data_1 = Path(x) #stores the relative path location
9
data_2 = Path(y)
10
data_3 = Path(z)
11
data_4 = Path(v)
12
print(data_1) #prints the file location for each input argument
13
print(data_2)
14
print(data_3)
15
print(data_4)
16
17
x1 = pd.read_csv(data_1, delimiter=";", skiprows=(1), decimal = ",", na_values = ['no info', '.']) #loads the csv file from the path created above
18
y1 = pd.read_csv(data_2, delimiter=";", skiprows=(1), decimal = ",", na_values = ['no info', '.'])
19
z1 = pd.read_csv(data_3, delimiter=";", skiprows=(1), decimal = ",", na_values = ['no info', '.'])
20
v1 = pd.read_csv(data_4, delimiter=";", skiprows=(1), decimal = ",", na_values = ['no info', '.'])
21
22
time1 = x1.iloc[:,0]
23
time2 = y1.iloc[:,0]
24
time3 = z1.iloc[:,0]
25
time4 = v1.iloc[:,0]
26
27
resA_x1 = x1.iloc[:,1]
28
resB_x1 = x1.iloc[:,2]
29
resC_x1 = x1.iloc[:,3]
30
resD_x1 = x1.iloc[:,4]
31
32
resA_y1 = y1.iloc[:,1]
33
resB_y1 = y1.iloc[:,2]
34
resC_y1 = y1.iloc[:,3]
35
resD_y1 = y1.iloc[:,4]
36
37
resA_z1 = z1.iloc[:,1]
38
resB_z1 = z1.iloc[:,2]
39
resC_z1 = z1.iloc[:,3]
40
resD_z1 = z1.iloc[:,4]
41
42
resA_v1 = v1.iloc[:,1]
43
resB_v1 = v1.iloc[:,2]
44
resC_v1 = v1.iloc[:,3]
45
resD_v1 = v1.iloc[:,4]
46
47
48
fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 1, figsize=(12,8))
49
fig.suptitle('Drops')
50
51
ax1.plot(time1, resA_x1, 'r', resB_x1, 'g', resC_x1, 'b', resD_x1, 'y')
52
ax1.set_ylabel('A [V]')
53
54
ax2.plot(time2, resA_y1, 'r', resB_y1, 'g', resC_y1, 'b', resD_y1, 'y')
55
ax2.set_ylabel('B [V]')
56
57
ax3.plot(time3, resA_z1, 'r', resB_z1, 'g', resC_z1, 'b', resD_z1, 'y')
58
ax3.set_ylabel('C [V]')
59
60
ax4.plot(time4, resA_v1, 'r', resB_v1, 'g', resC_v1, 'b', resD_v1, 'y')
61
ax4.set_xlabel('Time [s]')
62
ax4.set_ylabel('D [V]')
63
64
65
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')
66
Advertisement
Answer
You need to specify the same x-axis for each of the datasets:
JavaScript
1
64
64
1
import pandas as pd
2
import numpy as np
3
import matplotlib.pyplot as plt
4
from pathlib import Path
5
6
def function(x,y,z,v,Plot_ShareY=True): #x, y, z, v is filename
7
data_1 = Path(x) #stores the relative path location
8
data_2 = Path(y)
9
data_3 = Path(z)
10
data_4 = Path(v)
11
print(data_1) #prints the file location for each input argument
12
print(data_2)
13
print(data_3)
14
print(data_4)
15
16
#loads the csv file from the path created above
17
x1 = pd.read_csv(data_1, delimiter=";", skiprows=(1, 2), decimal=",", na_values=['no info', '.'])
18
y1 = pd.read_csv(data_2, delimiter=";", skiprows=(1, 2), decimal=",", na_values=['no info', '.'])
19
z1 = pd.read_csv(data_3, delimiter=";", skiprows=(1, 2), decimal=",", na_values=['no info', '.'])
20
v1 = pd.read_csv(data_4, delimiter=";", skiprows=(1, 2), decimal=",", na_values=['no info', '.'])
21
22
time1 = x1['Time']
23
time2 = y1['Time']
24
time3 = z1['Time']
25
time4 = v1['Time']
26
27
resA_x1 = x1['Channel A']
28
resB_x1 = x1['Channel B']
29
resC_x1 = x1['Channel C']
30
resD_x1 = x1['Channel D']
31
32
resA_y1 = y1['Channel A']
33
resB_y1 = y1['Channel B']
34
resC_y1 = y1['Channel C']
35
resD_y1 = y1['Channel D']
36
37
resA_z1 = z1['Channel A']
38
resB_z1 = z1['Channel B']
39
resC_z1 = z1['Channel C']
40
resD_z1 = z1['Channel D']
41
42
resA_v1 = v1['Channel A']
43
resB_v1 = v1['Channel B']
44
resC_v1 = v1['Channel C']
45
resD_v1 = v1['Channel D']
46
47
fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 1, figsize=(12,8))
48
fig.suptitle('Drops')
49
ax1.plot(time1, resA_x1, 'r', time1, resB_x1, 'g', time1, resC_x1, 'b', time1, resD_x1, 'y')
50
ax1.set_ylabel('A [V]')
51
52
ax2.plot(time2, resA_y1, 'r', time2, resB_y1, 'g', time2, resC_y1, 'b', time2, resD_y1, 'y')
53
ax2.set_ylabel('B [V]')
54
55
ax3.plot(time3, resA_z1, 'r', time3, resB_z1, 'g', time3, resC_z1, 'b', time3, resD_z1, 'y')
56
ax3.set_ylabel('C [V]')
57
58
ax4.plot(time4, resA_v1, 'r', time4, resB_v1, 'g', time4, resC_v1, 'b', time4, resD_v1, 'y')
59
ax4.set_ylabel('D [V]')
60
61
plt.show()
62
63
function('trial.csv', 'trial.csv', 'trial.csv', 'trial.csv')
64
Giving you:
If you also use skiprows=(1, 2)
, the header columns can also be used.
This could be fully generalised to allow different numbers of CSV files to be passed:
JavaScript
1
32
32
1
import pandas as pd
2
import numpy as np
3
import matplotlib.pyplot as plt
4
from pathlib import Path
5
6
def function(*xyzv, Plot_ShareY=True): #x, y, z, v is filename
7
8
# data paths
9
dps = [Path(f) for f in xyzv]
10
11
for dp in dps:
12
print(dp)
13
14
#dataframes - loads the csv file from the paths created above
15
dfs = [pd.read_csv(dp, delimiter=";", skiprows=(1, 2), decimal=",", na_values=['no info', '.']) for dp in dps]
16
times = [df['Time'] for df in dfs]
17
y_labels = [f'{dp} [V]' for dp in dps] # e.g. base it on the filename
18
ch = [('Channel A', 'r'), ('Channel B', 'g'), ('Channel C', 'b'), ('Channel D', 'y')]
19
20
fig, *axes = plt.subplots(len(dfs), 1, figsize=(12, 8))
21
fig.suptitle('Drops')
22
23
for x, ax, df, y_label in zip(times, axes[0], dfs, y_labels):
24
for channel, colour in ch:
25
ax.plot(x, df[channel], colour)
26
ax.set_ylabel(y_label)
27
28
plt.show()
29
30
function('trial.csv', 'trial.csv', 'trial.csv', 'trial.csv')
31
function('trial.csv', 'trial.csv')
32