Some random notes related to Plotly or Dash
Colour
See https://plotly.com/python/discrete-color/#discrete-colors-in-dash.
Plotly uses the colour sequence from the active template’s layout.colorway
attribute.
- The default active template is
plotly
.
- You can choose from one of plotly’s built in qualitative colour sequences from the
px.colors.qualitative
module or define your own.
import plotly.express as px
fig = px.colors.qualitative.swatches()
fig.show()
Mapping colour to a ‘Month’ column
In the weather dataset I was mapping the colour to see how values changed over a year across all the weather stations in the dataset.
In order to use a discrete colour sequence when mapping colour to a variable such as a ‘month’ column, the month need to be in string format and not integer. Otherwise the colour will be mapped using a qualitative colour sequence.
It is useful to have a month name rather than just the month number for the legends of some plots.
A month name can be added to a dataframe from a date column using dt.month_name()
as follows:
df['month'] = df['date'].dt.month_name()
- If there is a meaningful order to discrete/qualitative/categorical data then a continuous colour scale can be used as a discrete sequence.
For example:
color_discrete_sequence= px.colors.sequential.Plasma_r
Some random notes related to Plotly or Dash
Colour
See https://plotly.com/python/discrete-color/#discrete-colors-in-dash.
Plotly uses the colour sequence from the active template’s layout.colorway
attribute.
- The default active template is
plotly
. - You can choose from one of plotly’s built in qualitative colour sequences from the
px.colors.qualitative
module or define your own.
import plotly.express as px
fig = px.colors.qualitative.swatches()
fig.show()
Mapping colour to a ‘Month’ column
In the weather dataset I was mapping the colour to see how values changed over a year across all the weather stations in the dataset.
In order to use a discrete colour sequence when mapping colour to a variable such as a ‘month’ column, the month need to be in string format and not integer. Otherwise the colour will be mapped using a qualitative colour sequence.
It is useful to have a month name rather than just the month number for the legends of some plots.
A month name can be added to a dataframe from a date column using dt.month_name()
as follows:
df['month'] = df['date'].dt.month_name()
- If there is a meaningful order to discrete/qualitative/categorical data then a continuous colour scale can be used as a discrete sequence.
For example:
color_discrete_sequence= px.colors.sequential.Plasma_r