kevvcgn
kevvcgn

Reputation: 11

Plotly Express Scatter Plot not scattering correctly along date time axis

I am trying to plot data in a pandas data frame into a plotly express scatter plot. My issue is that while I can get the data to almost plot perfectly, it won't scatter properly across the x-axis. I am using a date time column in the data frame as my x-axis. My datapoints are in the seconds range. However, plotly seems to group them together into vertical lines that seem to occur ~every 1.5 - 2 mins. So my chart looks a bit weird.

The bubbles should be spread out along the x-axis and not form these vertical lines scatter plot grouping bubbles into vertical lines

I have the following data frame (as an example).

df:

| id | time                             | y_value | color_value | size_value |
| -- | -------------------------------- | ------- | ----------- | ---------- |
| 01 | 2023-03-20 21:12:56.561000+00:00 |    1    |      A      |     10     |
| 02 | 2023-03-20 21:12:57.613000+00:00 |    5    |      A      |      1     |
| 03 | 2023-03-20 21:12:58.623000+00:00 |    3    |      B      |     20     |
| 04 | 2023-03-20 21:12:60.317000+00:00 |    10   |      C      |     21     |

I am using the below code to plot the data in a scatter chart:

    import plotly.express as px
    fig = px.scatter(result, x='time', y='y_value', color='color_value', size='size_value', size_max=120, log_y=True)

I would expect the result to evenly plot out the datapoints on the xaxis rather than group them together in the verticals as it showed up in the image.

I also already tried adding the following to specify the tick size in the hopes that that works but it didn't change anything:

fig3.update_xaxes(
            dtick = 'L100',
            tickformat = '%H:%M:%S.%L\n%b %d,%Y',
            showgrid=True
        )

What's interesting is it does plot correctly with matplotlib but I would like to preserve the ability to hover over a bubble and get the extra information, which is why I am trying to stick with plotly.

Any help on how to scatter the values across the xaxis is greatly appreciated.

Upvotes: 0

Views: 1270

Answers (2)

montol
montol

Reputation: 173

This is probably related to this Github issue - your example plot has on the order of 1000 data points, which is the point at which plotly-express alters its plotting strategy.

The solution is to manually specify the renderer as "svg":

fig = px.scatter(
    result, 
    x='time', 
    y='y_value', 
    <other args>,
    render_mode='svg'
)

Upvotes: 0

kevvcgn
kevvcgn

Reputation: 11

It turns out that the issue seems to be with plotly express. When I use plotly.graph_objects instead it works.

Sample code that works:

    import plotly.graph_objects as go
    
    fig = go.Figure(data=[go.Scatter(
        x = df['time'], 
        y = df['y_value'],
        mode = 'markers',
        marker_size=df['size_value'],
        marker=dict(
            color=df['color_value']
        )
    )]
    )

    fig.show()

Upvotes: 1

Related Questions