【问题标题】:How to change the X axis range in seaborn如何在seaborn中更改X轴范围
【发布时间】:2019-02-22 08:26:47
【问题描述】:

默认情况下,seaborn 在 distplots 中置换 X 轴范围从 -5 到 35。但我需要以 1 个单位显示 X 轴范围从 1 到 30 的分布图。我该怎么做?

【问题讨论】:

    标签: python matplotlib seaborn


    【解决方案1】:

    要对此类绘图进行最灵活的控制,请创建自己的坐标区对象,然后将 seaborn 绘图添加到其中。然后,您可以对 x 轴等功能执行标准 matplotlib 更改,或使用通过 matplotlib API 提供的任何正常控件。

    在python 3.8.12、matplotlib 3.4.3、seaborn 0.11.2 测试

    import matplotlib.pyplot as plt
    import seaborn as sns
    
    data = [5,8,12,18,19,19.9,20.1,21,24,28] 
    
    fig, ax = plt.subplots()
    sns.histplot(data, ax=ax)  # distplot is deprecate and replaced by histplot
    ax.set_xlim(1,31)
    ax.set_xticks(range(1,32))
    plt.show()
    

    ax 和 fig 对象暴露后,您现在可以随心所欲地编辑图表,并轻松地使用fig.set_size_inches(10,8)) 更改大小等操作!

    【讨论】:

      【解决方案2】:

      我不知道这是否是您正在寻找的,但我相信:

      import matplotlib.pyplot as plt
      import seaborn as sns
      tips = sns.load_dataset("tips")
      sns.set_style("whitegrid")
      g = sns.lmplot(x="tip", y="total_bill", data=tips,
       aspect=2)
      g = (g.set_axis_labels("Tip","Total bill(USD)").
      set(xlim=(0,15),ylim=(0,100)))
      plt.title("title")
      plt.show(g)
      

      如您所见,关键部分是xlim=(0,15),您可以在其中指定您想要的范围。在你的情况下:

      xlim=(1,30)

      我是从here那里拿到的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-21
        • 1970-01-01
        • 2015-02-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多