Skip to content

Instantly share code, notes, and snippets.

@pierdom
Last active August 3, 2023 11:56
Show Gist options
  • Save pierdom/d639a1d3b8934ee31db8b2ab9997ae92 to your computer and use it in GitHub Desktop.
Save pierdom/d639a1d3b8934ee31db8b2ab9997ae92 to your computer and use it in GitHub Desktop.
[Plot histograms with pre-computed counters] Plot histograms with Marplotlib hist function or Seaborn distplot function using pre-counted values using 'weights' argument. Very useful for plotting distributions of values queried from a very large dataset, where it is impossible to retrieve and load in memory every element of the distribution inde…
#!/usr/bin/env python3
import matplotlib.pyplot as plt
import seaborn as sns
# dictionary with pre-counted bins
test = {1:1,2:1,3:1,4:2,5:3,6:5,7:4,8:2,9:1,10:1}
# with matplotlib
plt.hist(list(test.keys()), weights=list(test.values()))
# with seaborn (use hist_kws to send arugments to plt.hist, used underneath)
sns.distplot(list(test.keys()), hist_kws={"weights":list(test.values())})
@sphynx
Copy link

sphynx commented Nov 1, 2020

Thanks, that was very useful!

In current version of Seaborn (0.11.0) it's also possible to plot it like this (discrete=True makes it nicer by setting some other parameters to good defaults):

keys = list(test.keys())
values = list(test.values())
sns.histplot(x=keys, weights=values, discrete=True)

It's also possible to easily convert from plotting counts to plotting probabilities by providing an extra argument stat='probability'

https://seaborn.pydata.org/generated/seaborn.histplot.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment