Seaborn对了

时间:2017-10-18 22:01:38

标签: python-3.x matplotlib seaborn

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

我正在尝试用barplot和pointplot制作帕累托图。但我不能打印右边的百分比ytick。顺便说一句,如果我手工制作它会重叠自己。

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

像图像中那样重叠。 enter image description here

编辑:我的意思是我想要在图形的右侧按季度百分比(0%,25%,50%,75%,100%)。

1 个答案:

答案 0 :(得分: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