2017-10-18 124 views
1
import numpy as np 
import matplotlib as mpl 
import matplotlib.pyplot as plt 
import seaborn as sns 

d = ['d1','d2','d3','d4','d5','d6'] 
value = [111111, 222222, 333333, 444444, 555555, 666666] 

y_cumsum = np.cumsum(value) 
sns.barplot(d, value) 

sns.pointplot(d, y_cumsum) 
plt.show() 

我想使barrelot和pointplot pareto圖。但是我不能將百分比打印到右側。順便說一下,如果我製作了自己重疊的遊戲。Seaborn右ytick

plt.yticks([1,2,3,4,5]) 

像在圖像中重疊。 enter image description here

編輯:我的意思是我想要在圖表右側的百分比(0,25%,50%,75%,100%)。

+0

你的蜱蟲出現的原因當你手動設置它們時,它們在同一個地方是因爲當你的比例尺達到70,000時,1,2,3,4,5基本上處於相同的位置。你可以編輯,以澄清你想要的百分比符號的位置(右邊的第二個軸?或左邊的每個ytick的右邊?)以及你想要它的百分比? –

+0

@Joel Ostblom我只想在百分比的價值清單總和中,在右手邊。我其實並沒有創造新的數字。其實我還不明白呢 – yigitozmen

+0

我的意思是0%,25%,50%,100% – yigitozmen

回答

1

從我的理解,你想要顯示的數字右側的百分比。要做到這一點,我們可以使用twinx()創建第二個y軸。所有我們需要做的就是要適當地設置該第二軸的極限,並設置一些自定義標籤:

import matplotlib.pyplot as plt 
import numpy as np 
import seaborn as sns 

d = ['d1','d2','d3','d4','d5','d6'] 
value = [111111, 222222, 333333, 444444, 555555, 666666] 

fig, ax = plt.subplots() 
ax2 = ax.twinx() # create a second y axis 

y_cumsum = np.cumsum(value) 
sns.barplot(d, value, ax=ax) 

sns.pointplot(d, y_cumsum, ax=ax) 

y_max = y_cumsum.max() # maximum of the array 

# find the percentages of the max y values. 
# This will be where the "0%, 25%" labels will be placed 
ticks = [0, 0.25*y_max, 0.5*y_max, 0.75*y_max, y_max] 

ax2.set_ylim(ax.get_ylim()) # set second y axis to have the same limits as the first y axis 
ax2.set_yticks(ticks) 
ax2.set_yticklabels(["0%", "25%","50%","75%","100%"]) # set the labels 
ax2.grid("off") 

plt.show() 

這將產生如下圖所示:

enter image description here