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

Skip to content

[Bug]: eventplot cannot handle multiple datetime-based series #21552

Closed
@peguerosdc

Description

@peguerosdc

Bug summary

I am having the exact same issue as in #5248 (which apparently was resolved at some point, but it's not working for me) with the latest version of matplotlib as passing a list of datetime.dates to eventplot is not working.

Code for reproduction

`
import datetime
from matplotlib import pyplot as plt

ts_1 = [datetime.date(2021, 1, 15), datetime.date(2021, 1, 16), datetime.date(2021, 1, 18), datetime.date(2021, 1, 22), datetime.date(2021, 1, 26), datetime.date(2021, 1, 28), datetime.date(2021, 1, 30), datetime.date(2021, 2, 1), datetime.date(2021, 2, 3), datetime.date(2021, 2, 7), datetime.date(2021, 2, 8), datetime.date(2021, 2, 11), datetime.date(2021, 2, 15)]
ts_2 = [datetime.date(2021, 1, 1), datetime.date(2021, 1, 2), datetime.date(2021, 1, 3), datetime.date(2021, 1, 4), datetime.date(2021, 1, 5), datetime.date(2021, 1, 6), datetime.date(2021, 1, 7), datetime.date(2021, 1, 8), datetime.date(2021, 1, 9), datetime.date(2021, 1, 10), datetime.date(2021, 1, 11), datetime.date(2021, 1, 12), datetime.date(2021, 1, 13), datetime.date(2021, 1, 14), datetime.date(2021, 1, 15), datetime.date(2021, 1, 16), datetime.date(2021, 1, 17), datetime.date(2021, 1, 18), datetime.date(2021, 1, 19), datetime.date(2021, 1, 20), datetime.date(2021, 1, 25), datetime.date(2021, 1, 26), datetime.date(2021, 1, 27), datetime.date(2021, 1, 28), datetime.date(2021, 1, 29), datetime.date(2021, 1, 30), datetime.date(2021, 1, 31), datetime.date(2021, 2, 12)]
plt.eventplot([ts_1, ts_2])
`

Actual outcome


TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_6840/2408848598.py in
4 ts_1 = [datetime.date(2021, 1, 15), datetime.date(2021, 1, 16), datetime.date(2021, 1, 18), datetime.date(2021, 1, 22), datetime.date(2021, 1, 26), datetime.date(2021, 1, 28), datetime.date(2021, 1, 30), datetime.date(2021, 2, 1), datetime.date(2021, 2, 3), datetime.date(2021, 2, 7), datetime.date(2021, 2, 8), datetime.date(2021, 2, 11), datetime.date(2021, 2, 15)]
5 ts_2 = [datetime.date(2021, 1, 1), datetime.date(2021, 1, 2), datetime.date(2021, 1, 3), datetime.date(2021, 1, 4), datetime.date(2021, 1, 5), datetime.date(2021, 1, 6), datetime.date(2021, 1, 7), datetime.date(2021, 1, 8), datetime.date(2021, 1, 9), datetime.date(2021, 1, 10), datetime.date(2021, 1, 11), datetime.date(2021, 1, 12), datetime.date(2021, 1, 13), datetime.date(2021, 1, 14), datetime.date(2021, 1, 15), datetime.date(2021, 1, 16), datetime.date(2021, 1, 17), datetime.date(2021, 1, 18), datetime.date(2021, 1, 19), datetime.date(2021, 1, 20), datetime.date(2021, 1, 25), datetime.date(2021, 1, 26), datetime.date(2021, 1, 27), datetime.date(2021, 1, 28), datetime.date(2021, 1, 29), datetime.date(2021, 1, 30), datetime.date(2021, 1, 31), datetime.date(2021, 2, 12)]
----> 6 plt.eventplot([ts_1, ts_2], colors="black")

c:\Users\User\Documents\venv\lib\site-packages\matplotlib\pyplot.py in eventplot(positions, orientation, lineoffsets, linelengths, linewidths, colors, linestyles, data, **kwargs)
2784 linelengths=1, linewidths=None, colors=None,
2785 linestyles='solid', *, data=None, **kwargs):
-> 2786 return gca().eventplot(
2787 positions, orientation=orientation, lineoffsets=lineoffsets,
2788 linelengths=linelengths, linewidths=linewidths, colors=colors,

c:\Users\User\Documents\venv\lib\site-packages\matplotlib_init_.py in inner(ax, data, *args, **kwargs)
1359 def inner(ax, *args, data=None, **kwargs):
1360 if data is None:
-> 1361 return func(ax, *map(sanitize_sequence, args), **kwargs)
1362
1363 bound = new_sig.bind(ax, *args, **kwargs)

c:\Users\User\Documents\venv\lib\site-packages\matplotlib\axes_axes.py in eventplot(self, positions, orientation, lineoffsets, linelengths, linewidths, colors, linestyles, **kwargs)
1321 zip(positions, lineoffsets, linelengths, linewidths,
1322 colors, linestyles):
-> 1323 coll = mcoll.EventCollection(position,
1324 orientation=orientation,
1325 lineoffset=lineoffset,

c:\Users\User\Documents\venv\lib\site-packages\matplotlib\collections.py in init(self, positions, orientation, lineoffset, linelength, linewidth, color, linestyle, antialiased, **kwargs)
1619 self._lineoffset = lineoffset
1620 self.set_orientation(orientation)
-> 1621 self.set_positions(positions)
1622
1623 def get_positions(self):

c:\Users\User\Documents\venv\lib\site-packages\matplotlib\collections.py in set_positions(self, positions)
1638 pos_idx = 0 if self.is_horizontal() else 1
1639 segments = np.empty((len(positions), 2, 2))
-> 1640 segments[:, :, pos_idx] = np.sort(positions)[:, None]
1641 segments[:, 0, 1 - pos_idx] = lineoffset + linelength / 2
1642 segments[:, 1, 1 - pos_idx] = lineoffset - linelength / 2

TypeError: float() argument must be a string or a number, not 'datetime.date'

Expected outcome

An eventplot with one time series per "row" in the y-axis.

Operating system

Windows

Matplotlib Version

3.4.3

Matplotlib Backend

No response

Python version

3.9.7

Jupyter version

No response

Other libraries

No response

Installation

pip

Conda channel

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions