Line plotsΒΆ

A line plot shows how data changes over time or across different values, with each point on the plot representing a specific value and connecting to the next one to create a continuous line.

By looking at the shape and movement of the line, you can easily visualize trends, patterns, and correlations in the data, making it easier to understand and analyze complex information.

Hide code cell source
import plotly.io as pio

pio.renderers.default = "sphinx_gallery"
import plotly.express as px
import statsplotly

statsplotly makes it easy to plot an entire dataset on a graph while keeping the output readable thanks to the slicer dimension :

df = px.data.gapminder()

fig = statsplotly.plot(
    data=df.sort_values("lifeExp"),
    y="gdpPercap",
    x="lifeExp",
    text="pop",
    color_palette="tab20",
    slicer="country",
)
fig.show()

plotly’s native update_layout methods can be used to modify the axes of the returned object, for example to set a logscale on the yaxis :

fig.update_layout(yaxis_type="log")

Plotting regression linesΒΆ

By default, the slices appear in their order in the dataframe. This can be changed with the slice_order argument.

Here we also use the fit parameter to fit a regression line to the data (see regression type):

fig = statsplotly.plot(
    data=df.sort_values("lifeExp"),
    y="gdpPercap",
    x="lifeExp",
    slice_order=df.country.sort_values(),
    color_palette="tab20",
    slicer="country",
    fit="linear",
)
fig.show()

Shaded error barΒΆ

Statsplotly also supports β€œshaded error bar” to display a continuous region of error behind the lines.

Here we calculate the span of the GDP for each continent / year on the fly and superimpose each country data over it :

fig = statsplotly.plot(
    data=df.merge(
        df.groupby(["continent", "year"])["gdpPercap"]
        .apply(lambda x: (x.min(), x.max()))
        .rename("continent_span"),
        on=["continent", "year"],
    ).sort_values("lifeExp"),
    y="gdpPercap",
    x="lifeExp",
    shaded_error="continent_span",
    color_palette="tab20",
    slicer="country",
)
fig.update_layout(yaxis_type="log").show()

Full details of the API : plot().