Skip to content
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

Date not showing properly in a Hovertemplate for a Plotly map. #7309

Open
1 task
AndreeVela opened this issue Sep 20, 2024 · 0 comments
Open
1 task

Date not showing properly in a Hovertemplate for a Plotly map. #7309

AndreeVela opened this issue Sep 20, 2024 · 0 comments

Comments

@AndreeVela
Copy link

AndreeVela commented Sep 20, 2024

ALL software version info

Software Version Info
Apple M2 Sonoma 14.6.1 (23G93)
Google Chrome
Python 3.12
Pip 24.2

IPython          : 8.26.0
ipykernel        : 6.29.5
ipywidgets       : 8.1.2
jupyter_client   : 8.6.2
jupyter_core     : 5.7.2
jupyter_server   : 2.14.1
jupyterlab       : 4.2.3
nbclient         : 0.10.0
nbconvert        : 7.16.4
nbformat         : 5.9.2
notebook         : not installed
qtconsole        : not installed
traitlets        : 5.14.3

folium                    0.17.0
hvplot                    0.10.0
kaleido                   0.2.1
matplotlib                3.9.2
matplotlib-inline         0.1.6
numpy                     2.0.1
pandas                    2.2.2
panel                     1.4.4
plotly                    5.22.0
scipy                     1.14.0
seaborn                   0.13.2

Description of expected behavior and the observed behavior

When plotting a Map using panels and Plotly extension, and the Scattermapbox function, dates data passed in the customdata attribute, and later used in the hovertemplate are not displayed correctly. Instead they are displayed as the seconds from 1970 representation. Plotting the Fig directly without wrapping it in a Plotly pane shows the expected behavior.

Complete, minimal, self-contained example code that reproduces the issue

import pandas as pd
import numpy as np
import plotly.graph_objects as go
import panel as pn

# Enable Panel extensions
pn.extension('plotly')

# Create sample time series data with latitude and longitude
np.random.seed(42)
n_points = 100
data = pd.DataFrame({
    'timestamp': pd.date_range(start='2023-01-01', periods=n_points, freq='T'),
    'latitude': np.cumsum(np.random.randn(n_points) * 0.01) + 52.3702,  # Centered around Amsterdam
    'longitude': np.cumsum(np.random.randn(n_points) * 0.01) + 4.8952,  # Centered around Amsterdam
})

# Function to create a map using plotly.graph_objects and Scattermapbox
def create_map(data):
    fig = go.Figure()

    # Add the route using Scattermapbox
    fig.add_trace(go.Scattermapbox(
        lat=data['latitude'],
        lon=data['longitude'],
        mode='markers+lines',
        marker={'size': 10, 'color': 'blue'},
        line={'width': 2, 'color': 'blue'},
        hovertemplate=(
            '<b>Timestamp</b>: %{customdata[0]}<br>' +
            '<b>Latitude</b>: %{lat:.5f}<br>' +
            '<b>Longitude</b>: %{lon:.5f}<extra></extra>'
        ),
        customdata=data[['timestamp']],  # Pass timestamp as custom data for hovertemplate
    ))

    # Set the mapbox layout
    fig.update_layout(
        mapbox_style="open-street-map",
        mapbox=dict(
            center=go.layout.mapbox.Center(lat=52.3702, lon=4.8952),
            zoom=12,
        ),
        height=500,
        margin={"r":0,"t":0,"l":0,"b":0}
    )

    return fig

# Create a Panel widget for interactive display
map_plot = pn.pane.Plotly(create_map(data))

# Create a Panel layout for the dashboard
dashboard = pn.Column(
    pn.pane.Markdown("# Route Map Dashboard"),
    map_plot
)

# Display the dashboard
dashboard.show()

Stack traceback and/or browser JavaScript console output

No Stack traceback or errors messages are output.

Screenshots or screencasts of the bug in action

Wrong behavior
image

Expected behavior
image

  • I may be interested in making a pull request to address this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant