Q: Subplot of difference between data points imported with Pandas and conversion of time values I'm relatively new to Python (in the process of self-teaching) and so this is proving to be quite a learning curve but I'm very happy to get to grips with it. I have a set of data points from an experiment in excel, one column is time (with the format 00:00:00:000) and a second column is the measured parameter.
I'm using pandas to read the excel document in order to produce a graph from it with time along the x-axis and the measured variable along the y-axis. However, when I plot the data, the time column becomes the data point number (i.e. 00:00:00:000 - 00:05:40:454 becomes 0 - 2000) and I'm not sure why. Could anyone please advise how to rectify this?
Secondly, I'd like to produce a subplot that shows the difference between the y-values as a function of time, basically a gradient to show the variation. Is there a way to easily calculate this and display it using pandas?
Here is my code, please do forgive how basic it is!
import pandas as pd
import matplotlib.pyplot as plt
import pylab

df = pd.read_excel('rest.xlsx', 'Sheet1')

df.plot(legend=False, grid=False)

plt.show()
plt.savefig('myfig') 


A: If you just read the excel file, pandas will create a RangeIndex, starting at 0. To use your time information from you excel file as index, you have to specify the name (as string) of the time column with the key-word argument index_col in the read_excel call:
df = pd.read_excel('rest.xlsx', 'Sheet1', index_col='name_of_time_column') 

Just replace 'name_of_time_column' with the actual name of the column that contains the time information.
(Hopefully pandas will automatically parse the time information to a Datetimeindex, but your format should be fine.) The plot will use the Datetimeindex on x-axis.
To get the time difference between each datapoint, use the diff method with argument 1 on your DataFrame:
difference = df.diff(1)
difference.plot(legend=False, grid=False)


A: Try This:
import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_excel('rest.xlsx', 'Sheet1')


X = df['Time'].tolist()#If the time column  called 'Time'

Y = df['Parameter'].tolist()#If the Parameter column called 'Parameter'

plt.plot(X,Y)

plt.gcf().autofmt_xdate()
plt.show()


A: With matplotlib you can create a figure with two axis and name the axis for example ax_df and ax_diff:
import matplotlib.pyplot as plt

fig, [ax_df, ax_diff] = plt.subplots(nrows=2, ncols=1, sharex=True)

sharex=True specifies to use the same x-axis for both subplots.
When calling plot on the DataFrame, you can redirect the output to the axis by specifying the axes with the keyword argument ax:
df.plot(ax=ax_df)
df.diff(1).plot(ax=ax_diff)
plt.show()

