Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Seaborn plotting blank histogram

I'm trying to plot a histogram using seaborn in python. But all it gives me is a blank figure.

Here is the describe() of my column:

enter image description here

The code:

plt.subplots(figsize=(7,7))
sns.histplot(data=contratos, x='duracao_contrato', bins='fd')

The output:

enter image description here

like image 964
Pedro Henrique Cardoso Avatar asked Oct 19 '25 13:10

Pedro Henrique Cardoso


1 Answers

It seems the automatic bin estimation failed in your case. This can happen for some estimator-data combinations (see another example here).

Generally, estimators like fd should work with seaborn - seaborn passes the bin keyword to numpy because the calculation of the histogram data is performed by numpy. Unfortunately, if estimators fail in their bin calculation, they fail silently, so it is often not clear why the graph is not plotted.

So, what can be done in this case? First, one can try to use bins=auto - a versatile estimator that chooses the "best" of the available estimator functions. If this is unsatisfactory, try other estimators like bins="doane" or bins="stone" (seemingly, you have done this already). If this is still unsatisfactory define the number bins=10 (evenly spaced bins) or ranges bins=[-5000, 300, 350, 370, 400, 8000] (unevenly spaced bins).
BTW, another silent error can be created by using inf in the bin list bins=[-np.inf, 300, 350, 370, 400, np.inf] will probably also create an empty graph (I assume because the bins are used to determine the x-range of the graph).

like image 128
Mr. T Avatar answered Oct 22 '25 04:10

Mr. T



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!