Skip to content Skip to sidebar Skip to footer

Why Is Go.scatter Printing Extra Lines Whereas Px.line Is Not?

Here is my code for graph_objects- go.Figure(go.Scatter(x=continent_df.date, y=continent_df.new_cases_smoothed)) Whereas my code for plotly express is this - px.line(continent_df

Solution 1:

I can't be 100% sure without a proper sample of your data. But it seems that your dataset is of a long format with multiple values in continent_df.new_cases_smoothed belonging to different contients. And you're assigning all these values to one single trace using go.Figure(go.Scatter(x=continent_df.date, y=continent_df.new_cases_smoothed)).

The straight lines are there because there's only one line that goes back and forth and covers all categories and all indexes. The straight parts of the line appear when it goes back to the beginning and starts showing a new category

However, using px.line here takes care of that by grouping the continents using color='continent'. Hence making the value categories appear as unique traces.

We can use the gapminder dataset, which has a structure similar to your real world data, to illustrate how to assign individual traces to a go.Figure using fig.add_traces(go.Scatter()). The key is to retrieve unique categories, subset your data, and add groups line by line. This gives you arguably greater flexibility compared to using px.line.

Plot

enter image description here

Code

import plotly.graph_objs as go
import plotly.express as px
import pandas as pd

# Data
gap = px.data.gapminder()

fig = go.Figure()
for c in gap['country'].unique()[:10]:
    df = gap[gap['country']==c]
    fig.add_traces(go.Scatter(x=df['year'], y = df['lifeExp'], name = c))
    
fig.show()

Post a Comment for "Why Is Go.scatter Printing Extra Lines Whereas Px.line Is Not?"