Timeseries plot from CSV data (Timestamp and events): x-label constant
dataframe, matplotlib, pandas, python, time-series
Solution
Making the example reproducible, we can create the following text file (`data/timestamp01.csv`):
TIMESTAMP;eventid
2017-03-20 02:38:24;1
2017-03-21 05:59:41;1
2017-03-23 12:59:58;1
2017-03-24 01:00:07;1
2017-03-27 03:00:13;1
(same for `data/timestamp00.csv`). We can then read them in
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
df1 = pd.read_csv('data/timestamp01.csv', parse_dates=True, index_col='TIMESTAMP', sep=";")
df0 = pd.read_csv('data/timestamp00.csv', parse_dates=True, index_col='TIMESTAMP', sep=";")
Plotting them
f, (ax1, ax2) = plt.subplots(1, 2)
ax1.plot(df0.resample('D').size())
ax2.plot(df1.resample('D').size())
plt.setp(ax1.xaxis.get_majorticklabels(), rotation=30, ha="right")
plt.setp(ax2.xaxis.get_majorticklabels(), rotation=30, ha="right")
plt.show()
results in
which is the desired plot.
Problem
(This question can be read alone, but is a sequel to: Timeseries from CSV data (Timestamp and events)) I would like to visualize CSV data (from 2 files) as shown below, by a timeseries representation, using python's pandas module (see links below). Sample data of df1: ``` TIMESTAMP eventid 0 2017-03-20 02:38:24 1 1 2017-03-21 05:59:41 1 2 2017-03-23 12:59:58 1 3 2017-03-24 01:00:07 1 4 2017-03-27 03:00:13 1 ``` The 'eventid' column always contains the value of 1, and I am trying to show the sum of events for each day in the dataset. The 2nd dataset, df0, has similar structure but contains only zeros: Sample data of df0: ``` TIMESTAMP eventid 0 2017-03-21 01:38:24 0 1 2017-03-21 03:59:41 0 2 2017-03-22 11:59:58 0 3 2017-03-24 01:03:07 0 4 2017-03-26 03:50:13 0 ``` The x-axis label only shows the same date, and my question is: How can the different dates be shown? (What causes the same date to be shown multiple times on x labels?) script so far: ``` import pandas as pd import matplotlib.pyplot as plt import matplotlib.ticker as ticker df1 = pd.read_csv('timestamp01.csv', parse_dates=True, index_col='TIMESTAMP') df0 = pd.read_csv('timestamp00.csv', parse_dates=True, index_col='TIMESTAMP') f, (ax1, ax2) = plt.subplots(1, 2) ax1.plot(df0.resample('D').size()) ax1.set_xlim([pd.to_datetime('2017-01-27'), pd.to_datetime('2017-04-30')]) ax1.xaxis.set_major_formatter(ticker.FixedFormatter (df0.index.strftime('%Y-%m-%d'))) plt.setp(ax1.xaxis.get_majorticklabels(), rotation=15) ax2.plot(df1.resample('D').size()) ax2.set_xlim([pd.to_datetime('2017-03-22'), pd.to_datetime('2017-04-29')]) ax2.xaxis.set_major_formatter(ticker.FixedFormatter(df1.index.strftime ('%Y-%m-%d'))) plt.setp(ax2.xaxis.get_majorticklabels(), rotation=15) plt.show() ``` Output: (https://www.dropbox.com/s/z21koflkzglm6c3/figure_1.png?dl=0) Links I have tried to follow: http://pandas.pydata.org/pandas-docs/stable/visualization.html Multiple timeseries plots from Pandas Dataframe Pandas timeseries plot setting x-axis major and minor ticks and labels Any help is much appreciated.