【问题标题】:Seaborn BarPlot invert y axis and keep x axis on bottom of chart areaSeaborn BarPlot 反转 y 轴并将 x 轴保持在图表区域的底部
【发布时间】:2017-11-15 21:09:08
【问题描述】:

在 Excel 中,我可以绘制如下图:

让它看起来像这样:

通过反转 Y 轴并将“Horizo​​ntal Axis Crosses”设置为“max”。

我想在 Seaborn 中做同样的事情。我可以使用.invert_yaxis() 翻转 y_axis,但我无法像在 Excel 中那样保持图表底部的条形。

import seaborn as sns
barplot = sns.barplot(x='abc'
                      ,y='def'
                      ,data=df
                      ,ci=None
                      )
barplot.invert_yaxis()
barplot.figure

这会产生如下内容:

如何将条形从顶部移动到底部?

我正在使用 Python 3.6 和 seaborn 0.7.1

我的问题似乎与这个类似,但这个问题不清楚并且没有答案: Pyplot - Invert Y labels without inverting bar chart

【问题讨论】:

    标签: python python-3.x matplotlib seaborn


    【解决方案1】:

    我发现你可以这样做:

    plt.ylim(reversed(plt.ylim()))
    

    这让您可以继续使用 Seaborn。

    【讨论】:

      【解决方案2】:

      seaborn.barplotpyplot.bar 的封装,您可以使用 pyplot.bar 创建带有倒置 y 轴和从图表底部到 y 轴上方较低值的条形图:

      import matplotlib.pyplot as plt
      import pandas as pd
      import numpy as np
      
      df = pd.DataFrame({"x":range(5), "y": [1,1.2,1.4,1.6,1.8]})
      
      plt.bar(df.x, 2*np.ones(len(df))-df.y, bottom= df.y )
      plt.gca().invert_yaxis()
      plt.ylim(2,0)
      plt.show()
      

      【讨论】:

        猜你喜欢
        • 2014-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-06
        • 1970-01-01
        • 2020-08-05
        • 1970-01-01
        相关资源
        最近更新 更多