简体   繁体   English

如何使用matplotlib / seaborn和pandas数据框创建带有共享x轴的上下条形图

[英]how to create upside down bar graphs with shared x-axis with matplotlib / seaborn and a pandas dataframe

在此处输入图片说明

I want to create a graph similar to this. 我想创建一个与此类似的图形。 Is this possible using matplotlib / seaborn. 是否可以使用matplotlib / seaborn。 If so, what resources can I use in order to learn how to style matplotlib / seaborn graphs and how can I get the two graphs to line up like this. 如果是这样,我可以使用哪些资源来学习如何设置matplotlib / seaborn图的样式,以及如何使两个图这样排列。

Use a common x-axis and convert one of the datasets to contain negative values. 使用公共x轴并将其中一个数据集转换为包含负值。

y = ['{} to {} years'.format(i, i+4) for i in range(0, 90, 4)]
d_1 = np.random.randint(0, 150, 23)
d_2 = -1 * d_1

Then plot: 然后绘制:

fig, ax = plt.subplots()
ax.bar(y, d_1)
ax.bar(y, d_2)

# Formatting x labels
plt.xticks(rotation=90)
plt.tight_layout()
# Use absolute value for y-ticks
ticks =  ax.get_yticks()
ax.set_yticklabels([int(abs(tick)) for tick in ticks])

plt.show()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM