BioLizard style with plotly
The plotly graphing library for python is an open source library with which you can build interactive graphs.
Here, we show an example of how to create these graphs in the biolizard style.
Applying the biolizard style template
Several pre-defined plotly templates exist, which can be added to plotly express of plotly graph objects manually. The BiolizardStylePython package includes a plotly template named “lizard_style” that can be applied to the plots.
In plotly express the template can be added as an argument:
from BioLizardStylePython import *
import plotly.express as px
long_df = px.data.medals_long()
fig = px.bar(long_df, x="nation", y="count", color="medal", title="default template",
barmode = "stack")
fig.show()
fig = px.bar(long_df, x="nation", y="count", color="medal", title="Biolizard style",
barmode = "stack", template="lizard_style")
fig.show()
For graph objects, the template can be added with using update_layout()
:
import plotly.graph_objects as go
wide_df = px.data.medals_wide()
fig = go.Figure(data=[
go.Bar(name='Gold', x=wide_df['nation'], y=wide_df['gold']),
go.Bar(name='Silver', x=wide_df['nation'], y=wide_df['silver']),
go.Bar(name='Bronze', x=wide_df['nation'], y=wide_df['bronze'])],
layout={
'barmode': 'stack',
'title': {'text': 'default template'}
}
)
fig.show()
fig.update_layout(title={'text': 'Biolizard style'},
template="lizard_style")
It is even easier to call the lizard_style()
function with plotly=True
to set this style as the default for all plotly graphs:
# call lizard_style with plotly=True to set lizard style plotly template as default
lizard_style(plotly=True)
fig = px.bar(long_df, x="nation", y="count", color="medal", title="BioLizard Style is now default!",
barmode = "stack")
fig.show()
Plotly express
This section includes some examples of interactive graphs that can be created with plotly express, as well as some examples where other colorscales of the BioLizardStylePython package are applied.
import plotly.express as px
Scatterplots
df = px.data.gapminder()
df_2007 = df.query("year==2007")
fig = px.scatter(df_2007,
x="gdpPercap", y="lifeExp", size="pop", color="continent",
log_x=True, size_max=60,
title="Gapminder 2007")
fig.show()
# manually set colorscale
# 1. take colors at regular intervals across the colormap
import numpy as np
ncolors = len(df_2007["country"].unique())
rgbacolors = biolizard_hues_pal(np.linspace(0, 1, ncolors))
# 2. convert to hex values
hexcolors = [matplotlib.colors.rgb2hex(col) for col in rgbacolors]
fig = px.scatter(df_2007,
x="gdpPercap", y="lifeExp", size="pop", color="country",
color_discrete_sequence=hexcolors,
log_x=True, size_max=60,
title="Gapminder 2007")
fig.show()
Barplots
df = px.data.tips()
fig = px.histogram(df, x="sex", y="total_bill",
color='smoker', barmode = "group",
height=400)
fig.show()
# faceting
fig = px.histogram(df, x="sex", y="total_bill", color="smoker", barmode="group",
facet_row="time", facet_col="day",
category_orders={"day": ["Thur", "Fri", "Sat", "Sun"],
"time": ["Lunch", "Dinner"]})
fig.show()
Heatmaps
Note that the default colorscale for continuous data is l_viridis.
z = [[.1, .3, .5, .7, .9],
[1, .8, .6, .4, .2],
[.2, 0, .5, .7, .9],
[.9, .8, .4, .2, 0],
[.3, .4, .5, .7, 1]]
fig = px.imshow(z, text_auto=True, aspect="auto")
fig.show()
Choropleth (maps)
Note that colorscale needed to be set manually here.
from urllib.request import urlopen
import json
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
counties = json.load(response)
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv",
dtype={"fips": str})
fig = px.choropleth(df, geojson=counties, locations='fips', color='unemp',
color_continuous_scale=[matplotlib.colors.rgb2hex(l_viridis_pal(i)) for i in range(255)],
range_color=(0, 12),
scope="usa",
labels={'unemp':'unemployment rate'}
)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
Graph objects
This section includes some examples of interactive graphs that can be created with graph objects, as well as some examples where other colorscales of the BioLizardStylePython package are applied.
import plotly.graph_objects as go
Barplots
animals=['giraffes', 'orangutans', 'monkeys']
fig = go.Figure(data=[
go.Bar(name='SF Zoo', x=animals, y=[20, 14, 23]),
go.Bar(name='LA Zoo', x=animals, y=[12, 18, 29])
])
fig.show()
Heatmaps
fig = go.Figure(data=go.Heatmap(
z=[[1, None, 30, 50, 1], [20, 1, 60, 80, 30], [30, 60, 1, -10, 20]],
x=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
y=['Morning', 'Afternoon', 'Evening'],
hoverongaps = False))
fig.show()
Surface plots
import pandas as pd
z_data = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/api_docs/mt_bruno_elevation.csv")
fig = go.Figure(data=[go.Surface(z=z_data.values)])
# fig.update_traces(contours_z=dict(show=True, usecolormap=True,
# highlightcolor=blz_yellow, project_z=True))
#use update_layout
# fig.update_layout(template="simple_white")
fig.update_layout(title=dict(text='Mt Bruno Elevation'), autosize=False,
scene_camera_eye=dict(x=1.87, y=0.88, z=-0.64),
width=500, height=500,
margin=dict(l=65, r=50, b=65, t=90))
# diverging colorscale
# pass a list of hex colors
divcolors = [matplotlib.colors.rgb2hex(col) for col in biolizard_divergent_pal(range(255))]
fig = go.Figure(data=[go.Surface(z=z_data.values-200, colorscale=divcolors, cmid=0)])
fig.update_layout(title=dict(text='Mt Bruno Elevation'), autosize=False,
scene_camera_eye=dict(x=1.87, y=0.88, z=-0.64),
width=500, height=500,
margin=dict(l=65, r=50, b=65, t=90))
Contour plots
fig = go.Figure(data =
go.Contour(
z=[[10, 10.625, 12.5, 15.625, 20],
[5.625, 6.25, 8.125, 11.25, 15.625],
[2.5, 3.125, 5., 8.125, 12.5],
[0.625, 1.25, 3.125, 6.25, 10.625],
[0, 0.625, 2.5, 5.625, 10]]
))
fig.show()
# diverging colorscale
# pass a list of hex colors
divcolors = [matplotlib.colors.rgb2hex(col) for col in biolizard_divergent_pal(range(255))]
fig = go.Figure(data =
go.Contour(
z=[[0, 0.625, 2.5, 5.625, 10],
[-5.625, -4.25, -2.125, 1.25, 5.625],
[-8.5, -7.125, -5., -2.125, 2.5],
[-9.625, -9.25, -7.125, -4.25, 0.625],
[-10, -9.625, -8.5, -5.625, 0]],
colorscale=divcolors,
))
fig.show()