Thanks to visit codestin.com
Credit goes to github.com

Skip to content

datetime64 handling for ax.hist2d differs from ax.plot #17319

Description

@deeplycloudy

Bug report

hist2d interprets dates differently than do plot, scatter, etc. In the example, manually applying date2num before calling hist2d makes the output work as expected, but is not necessary for plot.

Code for reproduction

import matplotlib
import numpy as np
import matplotlib.pyplot as plt
print(matplotlib.__version__)

t = np.array(['2020-04-02T23:00:00.749079563', '2020-04-02T23:00:00.749083039',
              '2020-04-02T23:00:01.792519894', '2020-04-02T23:00:01.793246307',
              '2020-04-02T23:00:01.793248355', '2020-04-02T23:00:01.795347666',
              '2020-04-02T23:00:01.796964828', '2020-04-02T23:00:01.798717183',
              '2020-04-02T23:00:01.800349212', '2020-04-02T23:00:01.801143192'],
      dtype='datetime64[ns]')
tlim = np.array(['2020-04-02T23:00:00', '2020-04-02T23:00:02'],
      dtype='datetime64[ns]')
z = np.arange(t.shape[0], dtype=float)

fig, ax = plt.subplots(3,2, figsize=(12,12))
# plot with no axis control
ax[0,0].plot(t, z, '.')
ax[0,1].hist2d(t, z, bins=[5, 5])

# set axes to expected range with default time handling
# plot works, hist2d doesnt.
ax[1,0].plot(t, z, '.')
ax[1,0].set_xlim(tlim[0], tlim[1])
ax[1,1].hist2d(t, z, bins=[5, 5])
ax[1,1].plot(t, z, '.')
ax[1,1].set_xlim(tlim[0], tlim[1])

# manually converting dates fixes hist2d
ax[2,0].plot(t, z, '.')
ax[2,0].set_xlim(tlim[0], tlim[1])

ax[2,1].hist2d(matplotlib.dates.date2num(t), z, bins=[5, 5])
ax[2,1].plot(t, z, '.')
ax[2,1].set_xlim(tlim[0], tlim[1])

Actual outcome

(737517.9583333334, 737517.9583564815) # output of set_xlim call

image

Note that the times are O(10^18) in the first hist2d plot (upper right), unlike O(10^5) in the x-limits.

Expected outcome

hist2d should interpret time as with plot.

Matplotlib version

  • Operating system: MacOS Catalina
  • Matplotlib version: 3.2.1 (conda-forge)
  • Matplotlib backend (print(matplotlib.get_backend())): module://ipykernel.pylab.backend_inline
  • Python version: 3.6.7
  • Jupyter version (if applicable): 6.1.3 (client)

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

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions