0

I have this graph Time series with horizontal gap

The data series is expected to run several hours a day, more or less. I'd like to be able to visualize the data by date, but without the horizontal gaps as appear in the snapshot.

# data is a pandas dataframe
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(data['Date'], date['Temperature'])
plt.show()

I added NaN between the gaps but this only helps to remove the connecting line between them. What I'd like is either to eliminate the gap completely, or make it small so that the graph can we conveniently viewed as a continuous time series.

How do I tell matplotlib to reduce the horizontal gaps here? Thanks!


EDIT: After trying several suggestions (notably, sehan2's pointed to the right direction), the solutions were overly complicated and either didn't work, or had specific problems working with dates (as opposed to just ranges of numbers).

Finally I found this post that suggests plotting using Pandas' .plot() will automatically detect and apply broken-axes for non-date x-axis. One man's bug is another man's feature apparently.

target_col = 'Temperature'
df[target_col].plot()
plt.show()

I still can't get a solution to work well with dates. Without dates: breaking works With dates: breaking doesn't work

Ruslan
  • 911
  • 2
  • 11
  • 28
  • 1
    Are you using `pandas`, is `data` variable type `pandas.Series`? – imxitiz Jul 10 '21 at 12:02
  • yes, data is a pandas dataframe, and data['Date'] is a series. Thanks for noticing. Edited the original post. – Ruslan Jul 10 '21 at 12:06
  • Doesn't `data=data.dropna()` solves your problem? – imxitiz Jul 10 '21 at 13:47
  • No. There aren't any records in the gap. It's not like there are nan-records. It's just doing a large gap because the dates are far apart – Ruslan Jul 10 '21 at 13:49
  • Changing dates into number could solve or, dates is compulsory in graph? – imxitiz Jul 10 '21 at 13:58
  • Can we fill missing data? Is filling missing data okay! – imxitiz Jul 10 '21 at 14:03
  • 1
    Does this answer your question? https://stackoverflow.com/questions/32185411/break-in-x-axis-of-matplotlib – sehan2 Jul 10 '21 at 14:26
  • I tried several of the solutions there, but they either didn't work, or were overly complicated and had troubles working with dates. Eventually I found that pandas will do this automatically, out of the box, no questions asked (see answer below). – Ruslan Jul 10 '21 at 15:24
  • Kshitz: filling it is not the problem. What I'm trying to do is to get the plot to be "broken" at the x-axis. To cut out the white stuff – Ruslan Jul 10 '21 at 15:40
  • 1
    Unfortunately, there is no simple solution to creating the tick labels for the dates. You can find an example of how to create custom tick labels for a discontinuous time series in [this answer](https://stackoverflow.com/a/66102371/14148248) which uses list comprehensions (where `df.index` is equal `df['Date']` in your case). Let me know if you need any help to make it work for your case. – Patrick FitzGerald Jul 11 '21 at 22:35

0 Answers0