【问题标题】:Set axis maximum with seaborn distplot使用 seaborn distplot 设置轴最大值
【发布时间】:2019-02-07 15:48:34
【问题描述】:

是否可以为 seaborn distplots 设置最小和最大显示限制?

我正在尝试遍历 pandas 数据框的列,但我的所有输出都得到相同的轴。

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

我用 sns.plt.xlim()autoscale() 做了一些尝试,但似乎都没有成功。我错过了什么?

【问题讨论】:

    标签: python pandas seaborn


    【解决方案1】:

    你应该可以直接使用plt.xlim(0, var)得到你想要的:

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

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

    【讨论】:

    • 顺便说一句,如果您更愿意使用面向对象的 API 并直接在 sns.distplot 返回的 Axes 上进行操作(这对于更复杂的绘图很快变得有益),您还可以在保存图形之前使用splot.set_xlim(0, var)。最终结果是一样的
    猜你喜欢
    • 2017-10-12
    • 2017-12-29
    • 2017-08-22
    • 2019-01-11
    • 1970-01-01
    • 2015-05-25
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    相关资源
    最近更新 更多