Set axis maximum with seaborn distplot

Question:

Is it possible to set the minimum and maximum displayed limits for seaborn distplots?

I am trying to loop over columns of a pandas dataframe but all my outputs get the same axes.

for v in var_list: 
    df[v].dropna(inplace=True) 
    var=df[v].max()
    vstar = v + "_output.png"
    splot = sns.distplot(df[v])
#    sns.plt.xlim(0, var)
    splot.figure.savefig(vstar)
    splot.autoscale()

I have made a few attempts with sns.plt.xlim() and autoscale() but neither seems to do the trick. What am I missing?

Asked By: MicrobicTiger

||

Answers:

You should be able to get what you want by just using plt.xlim(0, var) directly:

In [24]: np.random.seed(0)

In [25]: data = np.random.randn(1000)

In [26]: sns.distplot(data)
Out[26]: <matplotlib.axes._subplots.AxesSubplot at 0xfa291967f0>

In [27]: plt.savefig('plot1.png')

enter image description here

In [39]: plt.clf()

In [40]: sns.distplot(data)
Out[40]: <matplotlib.axes._subplots.AxesSubplot at 0xfa291bcd30>

In [41]: plt.xlim(-10, 10)
Out[41]: (-10, 10)

In [42]: plt.savefig('plot2.png')

enter image description here

Answered By: fuglede

For sns.histplot and sns.displot, we could use the argument binrange:

splot = sns.displot(df[v], binrange=(0, var))
Answered By: Shahrokh Bah
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.