Seaborn 条形图反转 y 轴并将 x 轴保持在图表区域底部

8
在Excel中,我可以将一个看起来像这样的图形: enter image description here 变成这个样子:

enter image description here

通过反转Y轴并将“横坐标交叉点”设置为“最大值”来实现此功能。我希望在Seaborn中也能做到同样的事情。我可以使用.invert_yaxis()翻转y轴,但我无法像在Excel中那样将条形图保持在图表底部。
import seaborn as sns
barplot = sns.barplot(x='abc'
                      ,y='def'
                      ,data=df
                      ,ci=None
                      )
barplot.invert_yaxis()
barplot.figure

我可以翻译如下内容,这是关于编程的:

产生类似以下的东西: enter image description here

如何将条形图从顶部移动到底部开始呢?

我正在使用 Python 3.6 和 seaborn 0.7.1。

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

2个回答

6

seaborn.barplotpyplot.bar的一个包装器,您可以使用pyplot.bar创建绘制反转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()

enter image description here


这是我长时间以来看到的最令人困惑/反直觉的图表。 - Eike P.
(不是对你回答的批评,显然。) - Eike P.

6
我发现您可以简单地执行以下操作:
plt.ylim(reversed(plt.ylim()))

这使得您可以继续使用Seaborn。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接