plotly
plotly
What is Plotly?
Plotly allows you to create over 40 beautiful interactive web-based visualizations that can be
displayed in Jupyter notebooks or saved to HTML files. It is widely used to plot scientific,
statistical and financial data. Plotly is free to use unless you want them to host your data for you.
Installation
You can install using Anaconda under the environment tab by searching for Plotly. You'll also
need Cufflinks and a few other packages that you can install by running : conda install -c conda-
forge cufflinks-py in your command line or terminal. Also you can use the commands pip install
plotly and pip install cufflinks. Cufflinks connects Plotly to Pandas.
Import
import pandas as pd
import numpy as np
import chart_studio.plotly as py
import cufflinks as cf
import seaborn as sns
import plotly.express as px
%matplotlib inline
Basics
# Create a dataframe using a NumPy array that is 50 by 4
arr_1 = np.random.randn(50, 4)
df_1 = pd.DataFrame(arr_1, columns=['A','B','C','D'])
df_1.head()
Line Plots
# Allows us to create graph objects for making more customized plots
import plotly.graph_objects as go
# Use included Google price data to make one plot
df_stocks = px.data.stocks()
px.line(df_stocks, x='date', y='GOOG', labels={'x':'Date',
'y':'Price'})
Bar Charts
# Get population change in US by querying for US data
df_us = px.data.gapminder().query("country == 'United States'")
px.bar(df_us, x='year', y='pop')
Scatter Plot
# Use included Iris data set
df_iris = px.data.iris()
# Create a scatter plot by defining x, y, different color for count of
provided
# column, size based on supplied column and additional data to display
on hover
px.scatter(df_iris, x="sepal_width", y="sepal_length",
color="species",
size='petal_length', hover_data=['petal_width'])
# Create a customized scatter with black marker edges with line width
2, opaque
# and colored based on width. Also show a scale on the right
fig = go.Figure()
fig.add_trace(go.Scatter(
x=df_iris.sepal_width, y=df_iris.sepal_length,
mode='markers',
marker_color=df_iris.sepal_width,
text=df_iris.species,
marker=dict(showscale=True)
))
fig.update_traces(marker_line_width=2, marker_size=10)
Pie Charts
# Create Pie chart of the largest nations in Asia
# Color maps here plotly.com/python/builtin-colorscales/
df_samer = px.data.gapminder().query("year == 2007").query("continent
== 'Asia'")
px.pie(df_samer, values='pop', names='country',
title='Population of Asian continent',
color_discrete_sequence=px.colors.sequential.RdBu)
Histograms
# Plot histogram based on rolling 2 dice
dice_1 = np.random.randint(1,7,5000)
dice_2 = np.random.randint(1,7,5000)
dice_sum = dice_1 + dice_2
# bins represent the number of bars to make
# Can define x label, color, title
# marginal creates another plot (violin, box, rug)
fig = px.histogram(dice_sum, nbins=11, labels={'value':'Dice Roll'},
title='5000 Dice Roll Histogram', marginal='violin',
color_discrete_sequence=['green'])
fig.update_layout(
xaxis_title_text='Dice Roll',
yaxis_title_text='Dice Sum',
bargap=0.2, showlegend=False
)
Box Plots
# A box plot allows you to compare different variables
# The box shows the quartiles of the data. The bar in the middle is
the median
# The whiskers extend to all the other data aside from the points that
are considered
# to be outliers
df_tips = px.data.tips()
# We can see which sex tips the most, points displays all the data
points
px.box(df_tips, x='sex', y='tip', points='all')
# Complex Styling
df_stocks = px.data.stocks()
fig = go.Figure()
# Show all points, spread them so they don't overlap and change
whisker width
fig.add_trace(go.Box(y=df_stocks.GOOG, boxpoints='all', name='Google',
fillcolor='blue', jitter=0.5, whiskerwidth=0.2))
fig.add_trace(go.Box(y=df_stocks.AAPL, boxpoints='all', name='Apple',
fillcolor='red', jitter=0.5, whiskerwidth=0.2))
# Change background / grid colors
fig.update_layout(title='Google vs. Apple',
yaxis=dict(gridcolor='rgb(255, 255, 255)',
gridwidth=3),
paper_bgcolor='rgb(243, 243, 243)',
plot_bgcolor='rgb(243, 243, 243)')
Violin Plot
# Violin Plot is a combination of the boxplot and KDE
# While a box plot corresponds to data points, the violin plot uses
the KDE estimation
# of the data points
df_tips = px.data.tips()
px.violin(df_tips, y="total_bill", box=True, points='all')
# Multiple plots
px.violin(df_tips, y="tip", x="smoker", color="sex", box=True,
points="all",
hover_data=df_tips.columns)
color_continuous_scale="Viridis")
fig
marginal_x="histogram",
marginal_y="histogram")
fig
3D Scatter Plots
# Create a 3D scatter plot using flight data
fig = px.scatter_3d(flights, x='year', y='month', z='passengers',
color='year',
opacity=0.7, width=800, height=400)
fig
3D Line Plots
fig = px.line_3d(flights, x='year', y='month', z='passengers',
color='year')
fig
Scatter Matrix
# With a scatter matrix we can compare changes when comparing column
data
fig = px.scatter_matrix(flights, color='month')
fig
Choropleth Maps
# You can color complex maps like we do here representing unemployment
data
# Draw map using the county JSON data, color using unemployment values
on a range of 12
fig = px.choropleth(df, geojson=counties, locations='fips',
color='unemp',
color_continuous_scale="Viridis",
range_color=(0, 12),
scope="usa",
labels={'unemp':'unemployment rate'}
)
fig
Polar Chart
# Polar charts display data radially
# Let's plot wind data based on direction and frequency
# You can change size and auto-generate different symbols as well
df_wind = px.data.wind()
px.scatter_polar(df_wind, r="frequency", theta="direction",
color="strength",
size="frequency", symbol="strength")
Ternary Plot
# Used to represent ratios of 3 variables
df_exp = px.data.experiment()
px.scatter_ternary(df_exp, a="experiment_1", b="experiment_2",
c='experiment_3', hover_name="group",
color="gender")
Facets
# You can create numerous subplots
df_tips = px.data.tips()
px.scatter(df_tips, x="total_bill", y="tip", color="smoker",
facet_col="sex")
Animated Plots
# Create an animated plot that you can use to cycle through continent
# GDP & life expectancy changes
df_cnt = px.data.gapminder()
px.scatter(df_cnt, x="gdpPercap", y="lifeExp", animation_frame="year",
animation_group="country",
size="pop", color="continent", hover_name="country",
log_x=True, size_max=55, range_x=[100,100000],
range_y=[25,90])