Skip to content
Advertisement

Get data points from Seaborn distplot

I use

sns.distplot 

to plot a univariate distribution of observations. Still, I need not only the chart, but also the data points. How do I get the data points from matplotlib Axes (returned by distplot)?

Advertisement

Answer

You can use the matplotlib.patches API. For instance, to get the first line:

sns.distplot(x).get_lines()[0].get_data()

This returns two numpy arrays containing the x and y values for the line.

For the bars, information is stored in:

sns.distplot(x).patches

You can access the bar’s height via the function patches.get_height():

[h.get_height() for h in sns.distplot(x).patches]
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement