Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib only business days without weekends on x-axis with plot_date

I have the following persistent problem:

The following code should draw a straight line:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
d = pd.date_range(start="1/1/2012", end="2/1/2012", freq="B")
v = np.linspace(1,10,len(d))
plt.plot_date(d,v,"-")

But all I get is a jagged line because "plot_date" somehow fills up the dates in "d" with the weekends.

Is there a way to force matplotlib to take my dates (only business days) as is without filing them up with weekend dates?

Jagged line instead of straight one

>>>d
DatetimeIndex(['2012-01-02', '2012-01-03', '2012-01-04', '2012-01-05',
               '2012-01-06', '2012-01-09', '2012-01-10', '2012-01-11',
               '2012-01-12', '2012-01-13', '2012-01-16', '2012-01-17',
               '2012-01-18', '2012-01-19', '2012-01-20', '2012-01-23',
               '2012-01-24', '2012-01-25', '2012-01-26', '2012-01-27',
               '2012-01-30', '2012-01-31', '2012-02-01'],
               dtype='datetime64[ns]', freq='B')
like image 783
leofields Avatar asked Feb 14 '16 20:02

leofields


1 Answers

plot_date does a trick, it converts dates to number of days since 1-1-1 and uses these numbers to plot, then converts the ticks to dates again in order to draw nice tick labels. So using plot_date each day count as 1, business or not.

You can plot your data against a uniform range of numbers but if you want dates as tick labels you need to do it yourself.

d = pd.date_range(start="1/1/2012", end="2/1/2012", freq="B")
v = np.linspace(1,10,len(d))

plt.plot(range(d.size), v)
xticks = plt.xticks()[0]
xticklabels = [(d[0] + x).strftime('%Y-%m-%d') for x in xticks.astype(int)]
plt.xticks(xticks, xticklabels)
plt.autoscale(True, axis='x', tight=True)

enter image description here

But be aware that the labels can be misleading. The segment between 2012-01-02 and 2012-01-09 represents five days, not seven.

like image 102
Stop harming Monica Avatar answered Oct 11 '22 13:10

Stop harming Monica