如何在条形图的y轴上打印值/比例

时间:2014-12-20 18:23:08

标签: matplotlib seaborn

这是我到目前为止所做的。然而,我的问题是我无法在条形图的y轴上打印值/比例?有任何想法吗?我还会添加其他什么样的风格?

import seaborn as sb
from matplotlib import pyplot

%matplotlib inline

sb.axes_style("white")
sb.set_style("ticks")
sb.set_context("talk")

x1 = np.array(['U', 'G'])
x2 = np.array(['H', 'W'])

f, (ax1, ax2) = pyplot.subplots(1, 2, figsize=(12, 6))

y1 = np.array([831824, 3306662])
y2 = np.array([1798043, 1508619])

sb.barplot(x1, y1, ci=None, palette="Blues", hline=.0001, ax=ax1)
sb.barplot(x1, y2, ci=None, palette="Reds", hline=.0001, ax=ax2)

ax1.set_ylabel("Occurences")
ax1.set_xlabel("Totals")

ax2.set_ylabel("Occurences")
ax2.set_xlabel("Types")

sb.despine(bottom=True)
pyplot.setp(f.axes, yticks=[])
pyplot.tight_layout(h_pad=3)

sb.despine()

enter image description here

2 个答案:

答案 0 :(得分:2)

基于@ john-cipponeri的回答:

使用在pyplot.*调用的轴上运行的函数仅在最后打开的轴上运行,在您的情况ax2中,这是正确的图。使用轴实例可以将其生效。用这个替换你的代码的tour last block,我希望它符合你预期的情节:

ax1.grid(axis='y', linestyle='-')
ax2.grid(axis='y', linestyle='-')
pyplot.tight_layout(h_pad=3)
sb.despine()

答案 1 :(得分:1)

你可以尝试一种线型。

pyplot.grid(axis='y', linestyle='-')
相关问题