How to set x lim to 99.5 percentile of my data series for matplotlib histogram?

3.1k views Asked by At

I'm currently pumping out some histograms with matplotlib. The issue is that because of one or two outliers my whole graph is incredibly small and almost impossible to read due to having two separate histograms being plotted. The solution I am having problems with is dropping the outliers at around a 99/99.5 percentile. I have tried using:

plt.xlim([np.percentile(df,0), np.percentile(df,99.5)]) 
plt.xlim([df.min(),np.percentile(df,99.5)])

Seems like it should be a simple fix, but I'm missing some key information to make it happen. Any input would be much appreciated, thanks in advance.

1

There are 1 answers

3
BrenBarn On

To restrict focus to just the middle 99% of the values, you could do something like this:

trimmed_data = df[(df.Column > df.Column.quantile(0.005)) & (df.Column < df.Column.quantile(0.995))]

Then you could do your histogram on trimmed_data. Exactly how to exclude outliers is more of a stats question than a Python question, but basically the idea I was suggesting in a comment is to clean up the data set using whatever methods you can defend, and then do everything (plots, stats, etc.) on only the cleaned dataset, rather than trying to tweak each individual plot to make it look right while still having the outlier data in there.