Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

enter image description here

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.

like image 307
Marc Frame Avatar asked May 28 '18 17:05

Marc Frame


People also ask

How do you invert axis in Seaborn?

The value of Y-axis in a seaborn heatmap is reversed when home icon or H button is pushed.

How do you plot multiple graphs in Python using Seaborn?

In Seaborn, we will plot multiple graphs in a single window in two ways. First with the help of Facetgrid() function and other by implicit with the help of matplotlib. data: Tidy dataframe where each column is a variable and each row is an observation.

Can Seaborn use pandas DataFrame?

Seaborn provides an API on top of Matplotlib that offers sane choices for plot style and color defaults, defines simple high-level functions for common statistical plot types, and integrates with the functionality provided by Pandas DataFrame s.


1 Answers

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

enter image description here

like image 118
user3483203 Avatar answered Sep 19 '22 13:09

user3483203