Histograms#

Documentation:
https://plotly.com/python/histograms/
https://plotly.com/python-api-reference/generated/plotly.express.histogram.html

import plotly.express as px

Continuous x axis#

tips_df = px.data.tips()
tips_df.head()
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4
tips_fig = px.histogram(
	data_frame=tips_df,
	x='tip',
)
tips_fig

Categorical x axis#

tips_fig = px.histogram(
	data_frame=tips_df,
	x='time',
)
tips_fig

Changing the order of the categories#

tips_fig = px.histogram(
	data_frame=tips_df,
	x='time',
	category_orders={
		'time': ['Lunch', 'Dinner'],
	},
)
tips_fig

Column segments#

tips_fig = px.histogram(
	data_frame=tips_df,
	x='time',
	category_orders={
		'time': ['Lunch', 'Dinner'],
	},
	color='smoker',
)
tips_fig

Styling of histograms#

See here for color names:
https://www.w3.org/TR/css-color-3/#svg-color

tips_fig = px.histogram(
	data_frame=tips_df,
	x='tip',
	# list of colors to use
	color_discrete_sequence=['darkcyan', 'darkolivegreen'],
	# number of bins
	nbins=10,
	# height and width
	height=800,
	width=800,
	# adds numbers to the bars
	text_auto=True,
	# how to color the different segments in each bar
	color='sex',
)
tips_fig

Violin plots#

df = px.data.experiment()
df.tail()
experiment_1 experiment_2 experiment_3 gender group
95 108.156964 105.971541 64.524029 female treatment
96 91.739992 111.125377 64.260993 male control
97 95.410347 84.448322 75.505991 female control
98 106.362406 115.522382 123.469689 male treatment
99 94.269237 104.651064 92.387490 female treatment
fig = px.strip(df, x='group', y='experiment_3')
fig