2017-06-13 144 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