1

I have pandas DataFrame like this:

2013-09-13 20:51:00
2013-09-13 20:52:00
2013-09-13 20:53:00
2013-09-13 20:54:00
2013-09-13 20:55:00
2013-09-13 20:56:00
2013-09-13 20:57:00
2013-09-13 20:58:00
2013-09-13 20:59:00
2013-09-16 00:00:00
2013-09-16 00:01:00
2013-09-16 00:02:00
2013-09-16 00:03:00
2013-09-16 00:04:00
2013-09-16 00:05:00
2013-09-16 00:06:00
2013-09-16 00:07:00
2013-09-16 00:08:00

there is no Sun and Sat date. Matplotlib pyplot draws so:

enter image description here

Is it possible to crop the image except emty date? Like this: enter image description here

uralbash
  • 3,219
  • 5
  • 25
  • 45
  • 3
    [This post](http://stackoverflow.com/questions/5656798/python-matplotlib-is-there-a-way-to-make-a-discontinuous-axis) is full or suggestions for a discontinuous axis. – Greg Sep 19 '13 at 11:52

1 Answers1

0

If you plot a series with null values (or a masked array) in matplotlib, the line will be broken at the location of the nulls.

For example:

import matplotlib.pyplot as plt
import numpy as np

plt.plot([1, 2, 3, np.nan, np.nan, 6, 7, 8, 9])
plt.show()

enter image description here

Therefore, what you want to do is add NaN's for the weekend dates. pandas offers a few different ways to do this. There's an overview here, but basically you want to look at the asfreq and resample methods.

As an example with your dates:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

dates = """2013-09-13 20:51:00
2013-09-13 20:52:00
2013-09-13 20:53:00
2013-09-13 20:54:00
2013-09-13 20:55:00
2013-09-13 20:56:00
2013-09-13 20:57:00
2013-09-13 20:58:00
2013-09-13 20:59:00
2013-09-16 00:00:00
2013-09-16 00:01:00
2013-09-16 00:02:00
2013-09-16 00:03:00
2013-09-16 00:04:00
2013-09-16 00:05:00
2013-09-16 00:06:00
2013-09-16 00:07:00
2013-09-16 00:08:00"""
dates = pd.to_datetime(dates.split('\n'))

# Generate some random y-data...
y = (np.random.random(dates.size) - 0.5).cumsum()

# Resample to a regular 1-minute interval series
# By default, this will put NaNs in where there isn't any data...
data = pd.Series(y, index=dates).asfreq('1Min')

data.plot().set(title='Resampled data')
plt.show()
Joe Kington
  • 275,208
  • 71
  • 604
  • 463