Plot data from CSV file with datetime on x-axis

I have CSV data in a file that looks like:

timestamp,operation,trace_id,elapsed
2019-10-16T04:00:02Z,tsm1_cache_snapshot,0IWgPXJG000,0.609221
2019-10-16T04:00:03Z,tsm1_compact_group,0IWgPTPG000,2.641524
2019-10-16T04:00:04Z,tsm1_cache_snapshot,0IWgPe7G000,0.451853

Read it with:

df = pandas.read_csv('ops.csv', parse_dates=['timestamp'])

Types are:

df.dtypes

timestamp    datetime64[ns, UTC]
operation                 object
trace_id                  object
elapsed                  float64
dtype: object

I want to scatter plot with ‘timestamp’ on the x-axis and ‘elapsed’ on the y-axis. I’ve tried:

df.plot.scatter('timestamp', 'elapsed')

…which gives:

TypeError: data type not understood

I’ve tried a number of other things. The best I’ve gotten so far is a scatter plot with all points stacked on top of each other.

How do I get it to plot correctly?

Thanks

Got it working with:

plt.plot([],[])
for name, grp in df.groupby('operation'):
    plt.scatter(grp.timestamp, grp.elapsed, label=name)
plt.gcf().autofmt_xdate()

Note the:

plt.plot([],[])

Without that, all the points are stacked on top of each other. Unfortunately, I do not understand this “fix”. Just stumbled across it elsewhere.

2 Likes