0

I am using a numpy array with datetime values for my x-axis. Although the xData has gaps, matplot finds a way to fill them in the plot. I do not want that behavior. I want the x axis to plot the exact entries in the array "dates". Any suggestions?

import numpy as np
import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

dates = np.array([datetime.datetime(2017, 3, 24, 14, 52),datetime.datetime(2017, 3, 24, 14, 56),datetime.datetime(2017, 3, 26, 16, 0),datetime.datetime(2017, 3, 26, 16, 4)])
yData = np.ones(len(dates))

fig = plt.figure(figsize=(6,6))
ax = plt.subplot(111)
li, = ax.plot(dates,yData)
plt.gcf().subplots_adjust(bottom=0.3)

ax.get_xaxis().tick_bottom()
xfmt = mdates.DateFormatter('%Y-%m-%d-%H:%M')
ax.xaxis.set_major_formatter(xfmt)
locs, labels = plt.xticks()
plt.setp(labels, rotation=90)

fig.canvas.draw()
plt.show()

2 Answers2

2

In the end I was able to remove the gap using the post provided by @ImportanceOfBeingErnest and the hacks in those threads. I had to treat dates as indexes and only preserve them for labeling and ease of numpy manipulation. Here is the final code:

import numpy as np
import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

dates = np.array([datetime.datetime(2017, 3, 24, 14, 52),
                  datetime.datetime(2017, 3, 24, 23, 56),
                  datetime.datetime(2017, 3, 26, 8, 0),
                  datetime.datetime(2017, 3, 26, 19, 4)])
dates_indx = range(len(dates))
dates_strings = [dt.strftime('%Y-%m-%d-%H:%M') for dt in dates]

yData = np.ones(len(dates))

fig = plt.figure(figsize=(6,6))
ax = plt.subplot(111)
li, = ax.plot(dates_indx,yData, marker = 'o', linestyle="-")
plt.gcf().subplots_adjust(bottom=0.3)

ax.get_xaxis().tick_bottom()
plt.xticks(dates_indx,dates_strings)
plt.xticks(dates_indx, rotation=90)

fig.canvas.draw()
plt.show(block=False)
1

The xticks can be set via plt.xticks. So

plt.xticks(dates, rotation=90)

would set the xticks to those values from the dates array.

Complete code where I changed the dates a bit, such that they don't overlap

import numpy as np
import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

dates = np.array([datetime.datetime(2017, 3, 24, 14, 52),
                  datetime.datetime(2017, 3, 24, 23, 56),
                  datetime.datetime(2017, 3, 26, 8, 0),
                  datetime.datetime(2017, 3, 26, 19, 4)])
yData = np.ones(len(dates))

fig = plt.figure(figsize=(6,6))
ax = plt.subplot(111)
li, = ax.plot(dates,yData, marker="o", linestyle="-")
plt.gcf().subplots_adjust(bottom=0.3)

ax.get_xaxis().tick_bottom()
xfmt = mdates.DateFormatter('%Y-%m-%d-%H:%M')
ax.xaxis.set_major_formatter(xfmt)
plt.xticks(dates, rotation=90)


fig.canvas.draw()
plt.show()

produces

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Connecting the points with a straight line is needed. The problem here is the extra "dates" that were added to the x-axis that are outside the "dates" array. – user7769718 Mar 26 '17 at 14:32
  • From that you can learn how important it is to be precise about the problem in the question. I edited the answer. – ImportanceOfBeingErnest Mar 26 '17 at 14:38
  • Thanks. plt.xticks(dates, rotation=90) instead of "labels" did the trick indeed. Yet, is there a way to remove the gap? In practice this gap is a full weekend, 48 hours with 5 seconds increments. – user7769718 Mar 26 '17 at 15:11
  • Matplotlib uses a linear numeric axis to plot dates. Two dates which are separated by a certain amount in time will show this separation on the axis. There have been some hacky ways around this, just look at those [search results](http://www.google.de/search?q=matplotlib+remove+weekends). – ImportanceOfBeingErnest Mar 26 '17 at 15:43