4

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

5
  • 3
    Did the bin estimation fail? Can happen in some estimator-data combinations. What happens if you define bins=10? Commented Feb 25, 2021 at 16:20
  • Yeah, I think it was the problem. It's a bit weird that I didn't receive any warning message. After removing the negative values and setting the bins number, it worked! Commented Feb 25, 2021 at 16:30
  • The negative numbers should not be the problem. Another bin estimator may work perfectly well with your dataset. Please see here for the relevant bin keywords - seaborn passes this to numpy. And some datasets do not fulfill implicit prerequisites of some estimators. However, it is indeed annoying that they fail silently. Commented Feb 25, 2021 at 16:34
  • Before posting here I was trying with fd, but it took a time and then showed this blank histogram too. The other ones (sturges, scott, doane), create too large bins (due to the wide range of the data), and just give a big bar on the bin 0. I guess that the bin methods are probably defining too large bins. After setting it manually, it looked better. Thanks you so much @Mr.T!!! Please asnwer officialy so I can mark as resolved. Commented Feb 25, 2021 at 16:54
  • I can reproduce the problem where bins = 'fd' however bins = 'doane' appears to render the histogram. It would be useful if seaborn could throw a warning to tell us why bins = 'fd' does not render a chart successfully Commented Aug 24, 2022 at 12:35

1 Answer 1

3

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).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.