2017-10-18 132 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