Unnecessarily large date range created when plotting a single point using a Datetime object · Issue #5963 · matplotlib/matplotlib · GitHub
Skip to content

Unnecessarily large date range created when plotting a single point using a Datetime object #5963

Description

@bcoov

As explained in my original StackOverflow question, I've found some unexpected behavior when plotting single points in matplotlib.pyplot with Datetime objects. Plotting multiple points (>1) gives the range of dates with some buffer on either side. Plotting a single point causes the range to span multiple years (2014 - 2018 using Feb 03, 2016).

When using the AutoDateLocator(), the (assumed) issue isn't as noticeable since the generated ticks are fairly conservative, but using a WeekdayLocator() instead results in a dense mess of ticks. The application I'm using this for deals with time spans of at most a few months, so suddenly getting a range of multiple years is quite jarring.

Below is some code that generates 6 matplotlib.pyplot plot images to demonstrate a few example cases and the output from running the script with the --verbose-debug flag

import matplotlib.pyplot as plt
import matplotlib.dates as md
from datetime import datetime

# Create figures
fig0 = plt.figure(0)
fig1 = plt.figure(1)
fig2 = plt.figure(2)
fig3 = plt.figure(3)
fig4 = plt.figure(4)
fig5 = plt.figure(5)
fig6 = plt.figure(6)
# Add some subplots
sub3 = fig3.add_subplot(111)
sub4 = fig4.add_subplot(111)
sub5 = fig5.add_subplot(111)
sub6 = fig6.add_subplot(111)

# Creating a regular plot directly with a single point
plt.figure(0)
plt.plot((2,), (3,), marker='o')
plt.title('Regular plot, single point')

# Doing the same, but with a subplot
sub3.plot((2,), (3,), marker='o')
sub3.set_title('Subplot, single point')

# Creating a regular plot using a Datetime object with multiple points
# Using the AutoDateLocator()
test_date_one = datetime.strptime('2016-02-03', '%Y-%m-%d')
test_date_two = datetime.strptime('2016-03-03', '%Y-%m-%d')

plt.figure(1)
plt.plot((test_date_one, test_date_two), (2, 3), marker='o')
fig1.autofmt_xdate()
plt.title('Regular plot, multiple Datetime points')

# Now with a single point
plt.figure(2)
plt.plot(test_date_one, 2, marker='o')
fig2.autofmt_xdate()
plt.title('Regular plot, single Datetime point')

# Doing the same, but with a subplot
# Multiple points
sub4.plot((test_date_one, test_date_two), (2, 3), marker='o')
fig4.autofmt_xdate()
sub4.set_title('Subplot, multiple Datetime points')

# Single point
sub5.plot(test_date_one, 2, marker='o')
fig5.autofmt_xdate()
sub5.set_title('Subplot, single Datetime point')

# Finally, single Datetime point with WeekdayLocator()
sub6.plot(test_date_two, 3, marker='o')
sub6.xaxis.set_major_locator(md.WeekdayLocator())
fig6.autofmt_xdate()
sub6.set_title('Subplot, single Datetime point, WeekdayLocator()')

plt.show()

Debug Output
$HOME=C:\Users******
matplotlib data path C:\Python27\lib\site-packages\matplotlib\mpl-data


You have the following UNSUPPORTED LaTeX preamble customizations:

Please do not ask for support with these customizations active.


loaded rc file C:\Python27\lib\site-packages\matplotlib\mpl-data\matplotlibrc
matplotlib version 1.5.1
verbose.level debug
interactive is False
platform is win32
loaded modules: <dictionary-keyiterator object at 0x04E08810>
CACHEDIR=C:\Users*.matplotlib
Using fontManager instance from C:\Users*
.matplotlib\fontList.cache
backend TkAgg version 8.5
Could not load matplotlib icon: can't use "pyimage10" as iconphoto: not a photo image
Could not load matplotlib icon: can't use "pyimage19" as iconphoto: not a photo image
Could not load matplotlib icon: can't use "pyimage28" as iconphoto: not a photo image
Could not load matplotlib icon: can't use "pyimage37" as iconphoto: not a photo image
Could not load matplotlib icon: can't use "pyimage46" as iconphoto: not a photo image
Could not load matplotlib icon: can't use "pyimage55" as iconphoto: not a photo image
findfont: Matching :family=sans-serif:style=normal:variant=normal:weight=400:stretch=normal:size=medium to Bitstream Vera Sans (u'c:\python27\lib\site-packages\matplotlib\mpl-data\fonts\ttf\Vera.ttf') with score of 0.000000
findfont: Matching :family=sans-serif:style=normal:variant=normal:weight=400:stretch=normal:size=large to Bitstream Vera Sans (u'c:\python27\lib\site-packages\matplotlib\mpl-data\fonts\ttf\Vera.ttf') with score of 0.000000

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions