Skip to content

[ENH]: Support using datetimes as positions argument to violin(...) #30417

Description

@dstansby

Problem

It should be possible to set the position of a violin plot to be a datetime, but currently this fails:

import datetime

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
datetimes = [
    datetime.datetime(2023, 2, 10),
    datetime.datetime(2023, 5, 18),
    datetime.datetime(2023, 6, 6)
]
ax.violin(
    [
        {
            'coords': datetimes,
            'vals': [0.1, 0.5, 0.2],
            'mean': datetimes[1],
            'median': datetimes[1],
            'min': datetimes[0],
            'max': datetimes[-1],
            'quantiles': datetimes
        }
    ],
    positions=[datetime.datetime(2020, 1, 1)]
)
Traceback (most recent call last):
  File "/Users/dstansby/software/mpl/matplotlib/test.py", line 11, in <module>
    ax.violin(
  File "/Users/dstansby/miniforge3/envs/mpl-dev/lib/python3.12/site-packages/matplotlib/_api/deprecation.py", line 453, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dstansby/miniforge3/envs/mpl-dev/lib/python3.12/site-packages/matplotlib/axes/_axes.py", line 9064, in violin
    line_ends = [[-0.25 if side in ['both', 'low'] else 0],
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: unsupported operand type(s) for +: 'float' and 'datetime.datetime'

Proposed solution

No response

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions