Last active
August 3, 2023 11:56
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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())}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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):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