Skip to content
/ plotly Public

Plotly Plasma is a small opinionated enhancement for the Plotly for Python library

Notifications You must be signed in to change notification settings

a0th/plotly

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Plotly Plasma

Plotly Plasma is a small opinionated enhancement for the Plotly for Python library. It patches the Plotly Figure object, adding new methods to handle somewhat frequent use cases that would normally take some boilerplate code to solve.

Simply pip install plotly-plasma, import plasma, and you are good to go!

Multiple y-axes

When the values of the metrics are in very different scales, Plotly doesn't have a quick way to produce dual axes.

The default function will use the largest axis, and small variances will be barely noticeable.

px.line(data[["sinoid_0", "sinoid_9"]])

Plasma will let you easily do that with .dual, which will make the first column the left axis and the second column the right axis.

px.line(data[["sinoid_0", "sinoid_9"]]).dual()

Year-over-year comparison

When comparing timeseries, it is useful to overlay the same period of different years.

fig = px.line(data, y="sinoid_0", color=data.index.year)

You can use .yoy to do that, as long as the x-axis is a datetime.

fig.yoy()

Multiple colors in a single line

By default, when you pass a column to the color parameter, it will use one line for every color value. Sometimes, we want a single line to have multiple colors, for example, to highlight a specific period.

def add_month_condition(df):
    s = np.select(
        [
            data.index.month == 1,
            data.index.month == 6,
        ],
        ["january", "june"],
        default="rest of year",
    )
    return df.assign(month_condition=s)


fig = px.line(
    data.pipe(add_month_condition),
    y="sinoid_0",
    color="month_condition",
)
fig

You can use .single_line to do that.

fig.single_line()

Multiple lines with continuous colormap

Still on the color topic, Plotly will use a discrete colormap by default, which is not always the best option.

fig = px.line(
    tall_format,
    x="day",
    y="value",
    color="senoid_average",
)
fig

You can use .continuous_color to fix that. You can also pass a colorscale parameter to define the colormap, which can be any of the Plotly colormaps.

fig.continuous_color('Tropic')

Facets

Default facets have a column_name=column_value labels for facets, which I find almost always undesirable.

fig = px.line(
    tall_format,
    x="day",
    y="value",
    facet_row="variable",
    facet_col="is_even",
    facet_col_spacing=0.05,
)
fig

You can fix those with fix_facets_labels.

fig.fix_facet_labels()

You can also pass any keyworks that would be accepted by the annotation.update

fig.fix_facet_labels(
    font_size=12,
    font_family="Courier New",
    bgcolor="lightgrey",
)

About

Plotly Plasma is a small opinionated enhancement for the Plotly for Python library

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages