Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Customise Pandas Date Time Stamp @ x-axis

When I plots the complete data works fine and displays the date on the x-axis:

1.

When I zoom into particular portion to view:
2

the plot shows only the time rather than date, I do understand with less points can't display different set of date but how to show date or set date format even if the graph is zoomed?

dataToPlot = pd.read_csv(fileName, names=['time','1','2','3','4','plotValue','6','7','8','9','10','11','12','13','14','15','16'], 
                             sep=',', index_col=0, parse_dates=True, dayfirst=True)
dataToPlot.drop(dataToPlot.index[0])
startTime = dataToPlot.head(1).index[0]
endTime = dataToPlot.tail(1).index[0]
ax = pd.rolling_mean(dataToPlot_plot[startTime:endTime][['plotValue']],mar).plot(linestyle='-', linewidth=3,  markersize=9, color='#FECB00')

Thanks in advance!

like image 703
Java.beginner Avatar asked Jul 11 '14 08:07

Java.beginner


People also ask

How do you format the date on the X-axis?

In the chart, right-click the category axis, and then click Format Axis. In the Format Axis pane, select the Axis Options tab. Expand Axis Options, and then under Axis Type, make sure Date axis is selected.

How do I change the X-axis labels on a panda?

You can set the labels on that object. Or, more succinctly: ax. set(xlabel="x label", ylabel="y label") . Alternatively, the index x-axis label is automatically set to the Index name, if it has one.


1 Answers

I have a solution to make the labels look consistent, though bear in mind that it will also include the time on the "larger scale" time plot.

The code below uses the matplotlib.dates functionality to choose a date format for the x-axis. Note that as we're using the matplotlib formatting you can't simple use df.plot but must instead use plt.plot_date and convert your index to the correct format.

import pandas as pd

import matplotlib.pyplot as plt
from matplotlib import dates

# Generate some random data and plot it

time = pd.date_range('07/11/2014', periods=1000, freq='5min')
ts = pd.Series(pd.np.random.randn(len(time)), index=time)

fig, ax = plt.subplots()

ax.plot_date(ts.index.to_pydatetime(), ts.data)

# Create your formatter object and change the xaxis formatting.

date_fmt = '%d/%m/%y %H:%M:%S'

formatter = dates.DateFormatter(date_fmt)
ax.xaxis.set_major_formatter(formatter)

plt.gcf().autofmt_xdate()

plt.show()

An example showing the fully zoomed out plot

enter image description here

An example showing the plot zoomed in.

enter image description here

like image 136
Ffisegydd Avatar answered Sep 30 '22 13:09

Ffisegydd