0

I'm trying to load data from an excel sheet and then plot all on the same plot but I am a little inexperienced with plotting multiple lines on a single plot. Each column is Time Elapsed and the corresponding Residual Act. and I have multiple columns named the same. Time is different in each column hence having multiple time columns. Right now the code just outputs 4 separate plots. Can someone tell me how to do this without overly complicating myself, I have to plot multiple files in the future and would like an easy way.

import pandas as pd
import matplotlib.pyplot as plt

HD = pd.read_excel('C:\\Users\\azizl\\Desktop\\HDPD_Data.xlsx')

HD.plot(x='Time Elapsed', y= 'Residual Act.' , label='24')
HD.plot(x='Time Elapsed.1', y= 'Residual Act..1', label='48')
HD.plot(x='Time Elapsed.2', y= 'Residual Act..2', label='normal')
HD.plot(x='Time Elapsed.3', y= 'Residual Act..3', label='physical')
plt.show()
HD.head()
Landon
  • 3
  • 3
  • You create a figure with subplots using `fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(..., ...))` and then call `HD.plot(...., ax=axes[0, 0])` for the first subplot and `axes[0, 1]`, `axes[1, 0]` and `axes[1, 1]` for the other subplots – JohanC Oct 03 '20 at 13:42
  • Sorry I think I mis wrote, I am just trying to have multiple lines on a single plot not have a single figure with multiple plots. – Landon Oct 03 '20 at 13:52
  • 1
    You can use `fig, ax = plt.subplots((nrows=1, ncols=1, ...)` and use four times the same `ax`: `HD.plot(...., ax=ax)`. After the last `plot` you can call `plt.legend()` to create a legend with the given labels. You could add an image of the plot you obtained with your current code and explain what you'd like different. Without some toy data, it is hard to provide much more feedback. – JohanC Oct 03 '20 at 13:56
  • Please see [How to provide a reproducible copy of your DataFrame using `df.head(30).to_clipboard(sep=',')`](https://stackoverflow.com/questions/52413246), then **[edit] your question**, and paste the clipboard into a code block. Always provide a [mre] **with code, data, errors, current output, and expected output, as text**. If relevant, plot images are okay. – Trenton McKinney Oct 03 '20 at 15:53

1 Answers1

0

Assuming that you have read HD before, to genarate your plot, try the following code:

import matplotlib.pyplot as plt

labels = ['24', '48', 'normal', 'physical']
for slc in np.split(HD.values, HD.shape[1] // 2, axis=1):
    plt.plot(slc[:, 0], slc[:, 1], 'o-')
plt.xlabel('Time elapsed')
plt.ylabel('Residual Act.')
plt.legend(labels)
plt.show()

The idea is to:

  • Divide the source DataFrame into 2-column slices (np.split).
  • Plot each slice, with x and y data from the current slice (plt.plot).
  • Create the legend from an external list.

To test the above code I created the following DataFrame:

   Time Elapsed  Residual Act.  Time Elapsed.1  Residual Act..1  Time Elapsed.2  Residual Act..2  Time Elapsed.3  Residual Act..3 
0          1.00           4.15            1.10             4.10            1.15             3.50            1.05             3.76  
1          1.15           4.01            1.27             3.90            1.30             3.20            1.20             3.00  
2          1.80           3.40            1.90             3.50            2.11             3.00            2.00             2.90  
3          2.20           3.00            2.50             3.05            2.47             2.88            2.30             2.70  
4          2.90           2.50            2.95             2.20            2.90             2.40            3.10             2.30  
5          3.60           2.00            4.00             1.70            3.86             2.20            4.05             2.00  

The original Excel file contains pairs of Time Elapsed and Residual Act. columns, but read_excel adds .1, .2, ... to repeating column names.

Time Elapsed is expressed in seconds and Residual Act. in whatever unit of choice.

For the above data I got the following picture:

enter image description here

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41