【问题标题】:Seaborn plot with second y axis带有第二个 y 轴的 Seaborn 图
【发布时间】:2023-04-07 04:12:01
【问题描述】:

我想知道如何制作带有两个 y 轴的图,以便我的图看起来像这样:

通过添加另一个 y 轴来达到类似的效果:

我只使用我的情节中的这行代码来从我的数据框中获取前 10 个 EngineVersions:

sns.countplot(x='EngineVersion', data=train, order=train.EngineVersion.value_counts().iloc[:10].index);

【问题讨论】:

    标签: python matplotlib seaborn


    【解决方案1】:

    我认为您正在寻找类似的东西:

    import matplotlib.pyplot as plt
    x = [1,2,3,4,5]
    y = [1000,2000,500,8000,3000]
    y1 = [1050,3000,2000,4000,6000]
    
    fig, ax1 = plt.subplots()
    
    ax2 = ax1.twinx()
    ax1.bar(x, y)
    ax2.plot(x, y1, 'o-', color="red" )
    
    ax1.set_xlabel('X data')
    ax1.set_ylabel('Counts', color='g')
    ax2.set_ylabel('Detection Rates', color='b')
    
    plt.show()
    

    输出:

    【讨论】:

    • 你如何用 sns 做到这一点?
    • 对于@gdubs 来说可能为时已晚,但以防万一有人感兴趣 - 您可以像往常一样使用 Seaborn (sns) 绘制图表,只需创建新的 ax (ax2 = ax1.twinx() ),然后使用 'ax' 参数将适当的 Axes 对象转发到您的 Seaborn 函数,例如:ax1.bar(x, y) => sns.barplot(x=x, y=y, ax=ax1) ax2。 plot(x, y1, 'o-', color="red" ) => sns.lineplot(x=x, y=y1, color="red", ax=ax2)
    【解决方案2】:

    @gdubs 如果您想使用 Seaborn 的库执行此操作,此代码设置对我有用。不是在 matplotlib 中的绘图函数“外部”设置 ax 赋值,而是在 Seaborn 中的绘图函数“内部”进行设置,其中ax 是存储绘图的变量。

    import seaborn as sns # Calls in seaborn
    
    # These lines generate the data to be plotted
    x = [1,2,3,4,5]
    y = [1000,2000,500,8000,3000]
    y1 = [1050,3000,2000,4000,6000]
    
    fig, ax1 = plt.subplots() # initializes figure and plots
    
    ax2 = ax1.twinx() # applies twinx to ax2, which is the second y axis. 
    
    sns.barplot(x = x, y = y, ax = ax1, color = 'blue') # plots the first set of data, and sets it to ax1. 
    sns.lineplot(x = x, y = y1, marker = 'o', color = 'red', ax = ax2) # plots the second set, and sets to ax2. 
    
    # these lines add the annotations for the plot. 
    ax1.set_xlabel('X data')
    ax1.set_ylabel('Counts', color='g')
    ax2.set_ylabel('Detection Rates', color='b')
    
    plt.show(); # shows the plot. 
    

    输出: Seaborn output example

    【讨论】:

      【解决方案3】:

      您可以尝试此代码以获得与您最初想要的非常相似的图像。

      import seaborn as sb
      from matplotlib.lines import Line2D
      from matplotlib.patches import Rectangle
      
      x = ['1.1','1.2','1.2.1','2.0','2.1(beta)']
      y = [1000,2000,500,8000,3000]
      y1 = [3,4,1,8,5]
      g = sb.barplot(x=x, y=y, color='blue')
      g2 = sb.lineplot(x=range(len(x)), y=y1, color='orange', marker='o', ax=g.axes.twinx())
      g.set_xticklabels(g.get_xticklabels(), rotation=-30)
      g.set_xlabel('EngineVersion')
      g.set_ylabel('Counts')
      g2.set_ylabel('Detections rate')
      g.legend(handles=[Rectangle((0,0), 0, 0, color='blue', label='Nontouch device counts'), Line2D([], [], marker='o', color='orange', label='Detections rate for nontouch devices')], loc=(1.1,0.8))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-15
        • 2019-01-23
        • 2023-03-10
        • 2018-06-23
        • 1970-01-01
        • 1970-01-01
        • 2017-01-15
        相关资源
        最近更新 更多