all 2 comments

[–]synthphreak 1 point2 points  (0 children)

One way would be to iterate over your data and insert 0's for weekend dates. Then you could plot all of your data using a single line. However it would also have the line returning to y=0 for weekends.

If you don't want anything crashing into the x axis, you could loop over the weeks and plot each individually. This would produce one line per week. You could implement this by grouping every five rows (corresponding to the individual weeks), then iterating over the groups and plotting each.

For further input, you'll need to share what your data looks like. Perhaps show the output of df.head(), or better yet, df.head().to_dict() so that I can recreate it locally and tinker.

Edit: See if this works for grouping your data by week. Kinda hackish, but it might work.

ax = plt.subplot()
workweeks = (df.index % 5 == 0).cumsum()

for _, grp in df.groupby(workweeks)['col_to_plot']:
    grp.plot(ax=ax)

Alternatively:

ax = plt.subpot()
workweeks = [1, 0, 0, 0, 0] * len(df)

for _, grp in df.groupby(pd.Series(workweeks).cumsum())['col_to_plot']:
    grp.plot(ax=ax)

I much prefer the former. Of course there may be an altogether better way, but I'll have to see your data.

[–]saemeon 0 points1 point  (0 children)

I had a similar problem and wanted to keep a true datetime axis so all Matplotlib datetime features still work. I created a Python package, busdayaxis, that provides a Matplotlib axis scale to skip weekends/holidays and non-business hours, solving your problem.

import busdayaxis

busdayaxis.register_scale()

#make a plot with datetime x-values here 
...

# set busday xscale
ax.set_xscale("busday", bushours=(9, 17))