简体   繁体   中英

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. 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.

Use a common x-axis and convert one of the datasets to contain negative values.

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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