0% found this document useful (0 votes)
570 views1 page

Plotly Express Cheat Sheet

Plotly Express is a Python library for creating interactive plots from data. It is built on top of Plotly Graph Objects and allows creating various plot types like scatter plots, line plots, bar plots and histograms with just a few lines of code. Plotly plots have interactive controls that allow zooming, panning, selecting regions, and more. Properties like color, size, shape, and opacity can be customized for markers, lines and bars.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
570 views1 page

Plotly Express Cheat Sheet

Plotly Express is a Python library for creating interactive plots from data. It is built on top of Plotly Graph Objects and allows creating various plot types like scatter plots, line plots, bar plots and histograms with just a few lines of code. Plotly plots have interactive controls that allow zooming, panning, selecting regions, and more. Properties like color, size, shape, and opacity can be customized for markers, lines and bars.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

> Common plot types Customizing markers in Plotly

Data Visualization
 Import plotly When working with visualizations like scatter plots, lineplots, and more, you can customize
markers according to certain properties. These include

with Plotly Express in Python


# import plotly express as px

import plotly.express as px

size: set the marker siz line: set the width and color of a borde
color: set the marker colo symbol: set the shape of the marker
opacity: set the marker transparency

Learn Plotly online at www.DataCamp.com Scatter plots # In this example, we’re updating a scatter plot named fig_sct

fig_sct.update_traces(marker={"size": 24,

# Create a scatterplot on a DataFrame named clinical_data
 "color": "magenta",

px.scatter(clinical_data, x="experiment_1", y="experiment_2")

"opacity": 0.5,

"line": {"width": 2, "color": "cyan"},

> What is plotly? Set the size argument to the name of a numeric column to control
the size of the points and create a bubble plot.

fig_sct.show()

"symbol": "square"})

Plotly Express is a high-level data visualization package that allows you to create
interactive plots with very little code. It is built on top of Plotly Graph Objects, which
Line plots Customizing lines in Plotly
provides a lower-level interface for developing custom visualizations.
When working with visualizations that contain lines, you can customize them according
# Create a lineplot on a DataFramed named stock_data

to certain properties. These include:


px.line(stock_data, x="date", y=["FB", "AMZN"])

> Interactive controls in Plotly color: set the line colo shape: set how values are connected
Set the line_dash argument to the name of a categorical dash: set the dash style ("solid", ("linear", "spline", "hv", "vh",
column to have dashes or dots for different lines.
"dot", "dash", "longdash", "hvh","vhv"
"dashdot", "longdashdot") width: set the line width

Plotly plots have interactive controls shown in the top-right of the plot. The controls allow Bar plots
# In this example, we’re updating a scatter plot named fig_ln

you to do the following:
# Create a barplot on a DataFramed named commodity_data
fig_ln.update_traces(patch={"line": {"dash": "dot",

Download plot as a png: Save your interactive plot as a static PNG.

px.bar(commodity_data, x="nation", y=["gold", "silver", "bronze"],


"shape": "spline",

color_discrete_map={"gold": "yellow", 
 "width": 6}})

"silver": "grey",
fig_ln.show()

Zoom: Zoom in on a region of interest in the plot.

"bronze": "brown"})

Pan: Move around in the plot.

Customizing bars in Plotly


Swap the x and y arguments to draw horizontal bars.

Box Select: Select a rectangular region of the plot to be highlighted.

When working with barplots and histograms, you can update the bars themselves
according to the following properties
Lasso Select: Draw a region of the plot to be highlighted.

Histograms
size: set the marker siz line: set the width and color of a borde
Autoscale: Zoom to a "best" scale.

# Create a histogram on a DataFramed named bill_data


color: set the marker colo symbol: set the shape of the marker
px.histogram(bill_data, x="total_bill")

opacity: set the marker transparency

Reset axes: Return the plot to its original state.

Set the nbins argument to control the number of bins shown in the # In this example, we’re updating a scatter plot named fig_bar

Toggle Spike Lines: Show or hide lines to the axes whenever you hover over data.

histogram.
fig_bar.update_traces(marker={"color": "magenta",

"opacity": 0.5,

Show closest data on hover: Show details for the nearest data point to the cursor.

"line": {"width": 2, "color": "cyan"}})

Heatmaps fig_bar.show()

Compare data on hover: Show the nearest data point to the x-coordinate of the
cursor.
# Create a heatmap on a DataFramed named iris_data

px.imshow(iris_data.corr(numeric_only=True),
# In this example, we’re updating a histogram named fig_hst

zmin=-1, zmax=1, color_continuous_scale='rdbu')

fig_hst.update_traces(marker={"color": "magenta",

> Plotly Express code pattern


"opacity": 0.5,

Set the text_auto argument to True to display text values for each cell.
"line": {"width": 2, "color": "cyan"}})

fig_hst.show()

The code pattern for creating plots is to call the plotting function, passing a data frame as
the first argument. The x argument is a string naming the column to be used on the x-axis.
The y argument can either be a string or a list of strings naming column(s) to be used on > Customizing plots in plotly
the y-axis.

The code pattern for customizing a plot is to save the figure object returned
px.plotting_fn(dataframe, # Dataframe being visualized
from the plotting function, call its .update_traces() method, then call its .show()
x=["column-for-x-axis"], # Accepts a string or a list of strings

method to display it.

y=["columns-for-y-axis"], # Accepts a string or a list of strings



title="Overall plot title", # Accepts a string
Learn Data Skills Online at
# Create a plot with plotly (can be of any type)

xaxis_title="X-axis title", # Accepts a string

fig = px.some_plotting_function()

www.DataCamp.com
yaxis_title="Y-axis title", # Accepts a string

width=width_in_pixels, # Accepts an integer


# Customize and show it with .update_traces() and .show()

height=height_in_pixels) # Accepts an integer fig.update_traces()

fig.show()

You might also like