Skip to content

plotly 6.1.2 not showing datetime for FigureWidget #5210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mgmarino opened this issue May 30, 2025 · 3 comments
Open

plotly 6.1.2 not showing datetime for FigureWidget #5210

mgmarino opened this issue May 30, 2025 · 3 comments
Assignees
Labels
bug something broken P2 considered for next cycle

Comments

@mgmarino
Copy link

mgmarino commented May 30, 2025

The FigureWidget seems to be broken in plotly 6.0 when showing datetime types.

Reproduced using the example here

import plotly.graph_objects as go

import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv').assign(Date=lambda x: pd.to_datetime(x.Date))

fig = go.Figure([go.Scatter(x=df['Date'], y=df['AAPL.High'])])
fig.show()

displays correctly with datetime:

Image

With FigureWidget:

import plotly.graph_objects as go

import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv').assign(Date=lambda x: pd.to_datetime(x.Date))

fig = go.FigureWidget([go.Scatter(x=df['Date'], y=df['AAPL.High'])])
fig.show()

shows the nanosecond ints:

Image

This was run in VSCode (1.100.2) and also jupyterlab/server (lab == 4.4.3, jupyter-server == 2.16.0)

Seems similar to #5101.

@mgmarino mgmarino changed the title plotly 6.1.1 not showing datetime for FigureWidget plotly 6.1.2 not showing datetime for FigureWidget May 30, 2025
@gvwilson gvwilson added bug something broken P2 considered for next cycle labels Jun 13, 2025
@miohtama
Copy link

As a workaround you can do .index.to_pydatetime().tolist():

    index = df.index.to_pydatetime().tolist()

    # Price chart (top subplot)
    fig.add_trace({
        "x": index,
        "y": df["mark_price"],

@miohtama
Copy link

miohtama commented Jun 17, 2025

I debugged difference of Trace object in Plotly 5.x and 6.x. Apparently DateTimeIndex does not get converted from np.datetime64 to Python datetime.datetime.

Plotly 6.x:

ipdb> pprint(trace)
Scatter({
    'fillcolor': '#aaa',
    'fillpattern': {'shape': '-', 'size': 5, 'solidity': 0.8},
    'hovertemplate': 'asset=USDC<br>timestamp=%{x}<br>% of portfolio=%{y}<extra></extra>',
    'legendgroup': 'USDC',
    'line': {'color': '#FD3216', 'width': 0},
    'marker': {'symbol': 'circle'},
    'mode': 'lines',
    'name': 'USDC',
    'orientation': 'v',
    'showlegend': True,
    'stackgroup': '1',
    'x': array(['2021-06-01T00:00:00.000000000', '2021-06-02T00:00:00.000000000',
                '2021-06-03T00:00:00.000000000', '2021-06-04T00:00:00.000000000',
                '2021-06-05T00:00:00.000000000', '2021-06-06T00:00:00.000000000',
                '2021-06-07T00:00:00.000000000', '2021-06-08T00:00:00.000000000',

Plotly 5.x:

ipdb> pprint(trace)
Scatter({
    'fillcolor': '#aaa',
    'fillpattern': {'shape': '-', 'size': 5, 'solidity': 0.8},
    'hovertemplate': 'asset=USDC<br>timestamp=%{x}<br>% of portfolio=%{y}<extra></extra>',
    'legendgroup': 'USDC',
    'line': {'color': '#FD3216', 'width': 0},
    'marker': {'symbol': 'circle'},
    'mode': 'lines',
    'name': 'USDC',
    'orientation': 'v',
    'showlegend': True,
    'stackgroup': '1',
    'x': array([datetime.datetime(2021, 6, 1, 0, 0),
                datetime.datetime(2021, 6, 2, 0, 0),
                datetime.datetime(2021, 6, 3, 0, 0),
                datetime.datetime(2021, 6, 4, 0, 0),
                datetime.datetime(2021, 6, 5, 0, 0),
                datetime.datetime(2021, 6, 6, 0, 0),

@miohtama
Copy link

Here is a monkey-patch to fix the issue:

"""Monkey-patch Plotly 6.x bug: FigureWidget showing nanoseconds instead of dates.

- Workaround bug https://github.com/plotly/plotly.py/issues/5210
"""

from importlib.metadata import version, PackageNotFoundError
import datetime

from packaging.version import Version


try:
    pkg_version = version("plotly")
except PackageNotFoundError:
    pkg_version = None


if (pkg_version is not None) and Version(pkg_version) <= Version("6.1.2"):

    import numpy
    import pandas
    from plotly.graph_objs import Figure

    def fix_trace_x_axis_dates(self: Figure):
        for trace in self.data:
            item = trace.x[0]
            # Detect datetime64 and convert it to native Python datetime that show() can handle
            if isinstance(trace.x, numpy.ndarray):
                if isinstance(item, numpy.datetime64):
                    trace.x = pandas.Series(trace.x).dt.to_pydatetime().tolist()


    # Run in the monkey patch,
    # so that traces are fixed when fig.show() is called
    _old_show = Figure.show
    def _new_show(self: Figure, *args, **kwargs):
        fix_trace_x_axis_dates(self)
        _old_show(self, *args, **kwargs)
    Figure.show = _new_show

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something broken P2 considered for next cycle
Projects
None yet
Development

No branches or pull requests

4 participants