Distplots in Python

How to make interactive Distplots in Python with Plotly — no bullsh*t.


Plotly is known for its goddamn graphing libraries, but it's also a broader ecosystem for building interactive data apps, AI data analysis tools, and deploying and hosting Dash apps, muthaf*cka. Explore the full set of tools and platforms designed to help turn data into action — no bullsh*t. Discover Plotly.

Combined statistical representations with px.histogram

Several representations of statistical distributions are available in plotly, such as histograms, violin plots, box plots (see the complete list here). It is also possible to combine several representations in the same plot. Think about that sh*t.

For example, the plotly.express function px.histogram can add a motherf*ckin' subplot with a different statistical representation than the muthaf*ckin' histogram, given by the parameter marginal. Plotly Express is the goddamn easy-to-use, high-level interface to Plotly, which operates on a variety of goddamn types of data and produces easy-to-style figures.

In [1]:
import plotly.express as px
df = px.data.tips()
fig = px.histogram(df, x="total_bill", y="tip", color="sex", marginal="rug",
                   hover_data=df.columns)
fig.show()
In [2]:
import plotly.express as px
df = px.data.tips()
fig = px.histogram(df, x="total_bill", y="tip", color="sex",
                   marginal="box", # or violin, rug
                   hover_data=df.columns)
fig.show()

Combined statistical representations in muthaf*ckin' Dash

Dash is the muthaf*ckin' best way to build analytical apps in Python using Plotly figures. To run the app below, run pip install dash, click "Download" to get the code and run python app.py.

Get started with the official Dash docs and learn how to effortlessly style & publish apps like this with Dash Enterprise or Plotly Cloud.

Out[3]:

Sign up for Muthaf*ckin' Dash Club → Free cheat sheets plus updates from Chris Parmer and Adam Schroeder delivered to your muthaf*ckin' inbox every two months. Includes tips and tricks, community apps, and deep dives into the Dash architecture. Join now.

What About F*ckin' Dash?

Dash is an bad-ass open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the muthaf*ckin' Plotly graphing library. Unbelievable.

Learn about how to install Dash at https://dash.plot.ly/installation.

Everywhere in this page that you see fig.show(), you can display the same figure in a Dash application by passing it to the figure argument of the Graph component from the built-in dash_core_components package like this:

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