from colorspace import qualitative_hcl, dataset
import pandas as pd
import plotly.express as px
# Loading HarzTraffic data set
= dataset("HarzTraffic")
df
# Palette (modified "Dark 3" sequential palette)
= qualitative_hcl("Dark 3", h1 = -150, h2 = 120)
pal
# Creating plot
= px.scatter(df, x = "temp", y = "bikes", color = "season",
fig = {"temp": "daily mean temperature [deg C]", "bikes": "number of bikes"},
labels = "Number of bikes per day given season<br>Sonnenberg, Harz, Germany",
title = pal.colors(4))
color_discrete_sequence = dict(size = 7))
fig.update_traces(marker = "plotly_white")
fig.update_layout(template fig.show()
HCL-Based Color Scales for plotly
This article shows a series of different plots and figures using the plotly
graphing library using color maps based on the colorspace package.
Scatterplot
The following example shows the number of motorbikes recorded by an automatic traffic counting station in the Harz Mountains (Germany), an region very popular with bikers. The number of bikes (y-axis) is plotted against the daily
mean temperature (x-axis) and colored by season using custom colors from a modified version of the HCL-based qualitative color palette “Dark 3”.
Multi-group histogram
The next example shows a multi-group distribution plot based on the ‘HarzTraffic’ data set where the distribution of the daily maximum temperature is shown given the prevailing season. The colors are based on an adjusted qualitative HCL-based palette using “Dark 3” as template (adjusting hues).
from colorspace import qualitative_hcl, dataset
import plotly.figure_factory as ff
import numpy as np
# Palette to be used
= qualitative_hcl("Dark 3", h1 = -180, h2 = 100)
pal
# Loading data
= dataset("HarzTraffic")
df
# Creating objects for plotting
= ["winter", "spring", "summer", "autumn"]
group_labels = []
hist_data for n in group_labels:
== n, "tempmax"])
hist_data.append(df.loc[df.season
# Create distplot with custom bin_size
= ff.create_distplot(hist_data, group_labels, bin_size=.2,
fig = pal.colors(4))
colors = "plotly_white",
fig.update_layout(template = "Distribution of daily maximum temperature<br>given season. " +
title_text "Sonnenberg, Harz, Germany")
fig.show()
Heatmap
Plotting the joint distribution of the daily minimum and maximum temperature (2021-2023) at station Sonnenberg using the sequential HCL-based color palette “YlOrRd” (yellow-orange-red).
from colorspace import sequential_hcl, datasets
import plotly.express as px
# Loading data
= dataset("HarzTraffic")
df
= px.density_heatmap(df, x = "tempmin", y = "tempmax",
fig = "Joint distribution of daily minimum and maximum tmeperature",
title = sequential_hcl("YlOrRd", rev = True)(51))
color_continuous_scale fig.show()
Alternatively a fully customised sequential palette is used for the following figure, showing the same information as the plot above.
from colorspace import sequential_hcl, dataset
import plotly.express as px
# Loading data
= dataset("HarzTraffic")
df
= sequential_hcl(h = [330, 170], c = [45, 70, 10],
pal = [25, 95], power = [0.5, 1.5])
l
# Creating plot
= px.density_heatmap(df, x = "tempmin", y = "tempmax",
fig = "Joint distribution of daily minimum and maximum tmeperature",
title = pal.colors(21, rev = True))
color_continuous_scale fig.show()
Three-dimensional surface
Finally, the example below shows the topographic data of Maunga Whau (Mt Eden) located in the Auckland volcanic field on a 10m by 10m grid using the HCL-based terrain color palette, a multi-hue sequential palette.
from colorspace import terrain_hcl, dataset
import plotly.graph_objects as go
import numpy as np
= dataset("volcano") / 100.
volcano
# Custom set of colors to be used
= terrain_hcl().colors(51)
cols
# Plotting data
= go.Figure(data = [go.Surface(z = volcano, colorscale = cols)],
fig = go.Layout(title = "Maunga Whau (Mt Eden)"))
layout = "plotly_white")
fig.update_layout(template fig.show()