GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing plotly.com — Styling plotly express figures in Python Styling Figures made with Plotly Express¶Plotly Express is the easy-to-use, high-level interface to Plotly, which operates on a variety of types of data. Every Plotly Express function returns a
More specifically, here are the 4 ways you can style and customize figures made with Plotly Express:
Built-in Plotly Express Styling Arguments¶Many common styling options can be set directly in the
To illustrate each of these, here is a simple, default figure made with Plotly Express. Note the default orderings for the x-axis categories and the usage of lowercase & snake_case data frame columns for axis labelling. In [1]:
import plotly.express as px df = px.data.tips() fig = px.histogram(df, x="day", y="total_bill", color="sex") fig.show() Here is the same figure, restyled by adding some extra parameters to the initial Plotly Express call: In [2]:
import plotly.express as px df = px.data.tips() fig = px.histogram(df, x="day", y="total_bill", color="sex", title="Receipts by Payer Gender and Day of Week", width=600, height=400, labels={ # replaces default labels by column name "sex": "Payer Gender", "day": "Day of Week", "total_bill": "Receipts" }, category_orders={ # replaces default order by column name "day": ["Thur", "Fri", "Sat", "Sun"], "sex": ["Male", "Female"] }, color_discrete_map={ # replaces default color mapping by value "Male": "RebeccaPurple", "Female": "MediumPurple" }, template="simple_white" ) fig.show() Updating or Modifying Figures made with Plotly Express¶If none of the built-in Plotly Express arguments allow you to customize the figure the way you need to, you can use the Here is the same figure as above, with some additional customizations to the axes and legend via In [3]:
import plotly.express as px df = px.data.tips() fig = px.histogram(df, x="day", y="total_bill", color="sex", title="Receipts by Payer Gender and Day of Week vs Target", width=600, height=400, labels={"sex": "Payer Gender", "day": "Day of Week", "total_bill": "Receipts"}, category_orders={"day": ["Thur", "Fri", "Sat", "Sun"], "sex": ["Male", "Female"]}, color_discrete_map={"Male": "RebeccaPurple", "Female": "MediumPurple"}, template="simple_white" ) fig.update_yaxes( # the y-axis is in dollars tickprefix="$", showgrid=True ) fig.update_layout( # customize font and legend orientation & position font_family="Rockwell", legend=dict( title=None, orientation="h", y=1, yanchor="bottom", x=0.5, xanchor="center" ) ) fig.add_shape( # add a horizontal "target" line type="line", line_color="salmon", line_width=3, opacity=1, line_dash="dot", x0=0, x1=1, xref="paper", y0=950, y1=950, yref="y" ) fig.add_annotation( # add a text callout with arrow text="below target!", x="Fri", y=400, arrowhead=1, showarrow=True ) fig.show() How Plotly Express Works with Templates¶Plotly has a theming system based on templates and figures created with Plotly Express interact smoothly with this system:
By way of example, in the following figure, simply setting the In [4]:
import plotly.express as px df = px.data.iris() fig = px.density_heatmap(df, x="sepal_width", y="sepal_length", template="seaborn") fig.show() Setting Plotly Express Styling Defaults¶Plotly Express supports a simple default-configuration system via the
To illustrate this "defaults hierarchy", in the following example:
As a result, any figure produced with Plotly Express thereafter uses the In [5]:
import plotly.express as px import plotly.io as pio pio.templates.default = "simple_white" px.defaults.template = "ggplot2" px.defaults.color_continuous_scale = px.colors.sequential.Blackbody px.defaults.width = 600 px.defaults.height = 400 df = px.data.iris() fig = px.scatter(df, x="sepal_width", y="sepal_length", color="sepal_length", width=400) fig.show() What About Dash?¶Dash is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library. Learn about how to install Dash at https://dash.plot.ly/installation. Everywhere in this page that you see import plotly.graph_objects as go # or plotly.express as px fig = go.Figure() # or any Plotly Express function e.g. px.bar(...) # fig.add_trace( ... ) # fig.update_layout( ... ) from dash import Dash, dcc, html app = Dash() app.layout = html.Div([ dcc.Graph(figure=fig) ]) app.run(debug=True, use_reloader=False) # Turn off reloader if inside Jupyter
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |