2017-06-13 200 views
2

在Excel中,我可以采取类似如下的图表: enter image description hereSeaborn BarPlot反转Y轴,并保持x轴的图表区域的底部

,使它看起来像这样:

enter image description here

通过反转Y轴并将“水平轴交叉”设置为“最大”。

我想在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 

将会产生这样的: enter image description here

如何能打动我的试条,从顶部开始,到从底部开始?

我使用Python 3.6和0.7.1 seaborn

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

回答

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

enter image description here