1

I'm experiencing some issues with Matplotlib and pandas dataframe.

I want to plot data according to the time elapsed from start of the test, with on X axis "minutes:seconds" displayed.

To do so I retract the current timestamp to the timestamp of the first data, this gives me a timedelta64 object. With the correct representation of "hours:minutes:seconds" when I try to print it.

But when I try to plot the figure, the X axis is completely empty...

On the example below, the first plot is showing minutes by 20mn ticks. And the second one is empty.

Any idea why this axis is empty ?

Here is my example code :

# Imports
import pandas as pd
import datetime
import random
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.ticker as tk

# Data creation
l = [random.randrange(0, 10) for i in range(100)]
d = []
for i in range(100):
    d.append(datetime.datetime(2019, 12, 3, 11, 11, random.randrange(0, 60), 0) + datetime.timedelta(minutes=i))

df = pd.DataFrame()
df['time'] = d
df['value'] = l
df['reltime'] = df.time.apply(lambda x: x - df.time.iloc[0])
df.head(5)

df['reltime_s'] = df.reltime.apply(lambda x: x.total_seconds())
df['reltime_mn'] = df.reltime_s.apply(lambda x: x / 60)
df.head(5)

# Plotting
fig, ax = plt.subplots()
df.plot(x='reltime_mn', y='value', figsize=(18, 5), ax=ax)
plt.show()

fig, ax = plt.subplots()
df.plot(x='reltime', y='value', figsize=(18, 5), ax=ax)
plt.show()
Hexxotest
  • 19
  • 2
  • Does this answer your question? [Plotting timestamps (hour/minute/seconds) with Matplotlib](https://stackoverflow.com/questions/17709823/plotting-timestamps-hour-minute-seconds-with-matplotlib) – Yaakov Bressler Dec 05 '19 at 22:10
  • Does this answer your question? [Matplotlib timedelta64 index as x-axis](https://stackoverflow.com/questions/50717534/matplotlib-timedelta64-index-as-x-axis) – Diziet Asahi Dec 05 '19 at 22:17

0 Answers0