Python Pandas Plotting two BARHs side by side
I'm trying to make a plot similar to this one,
The left chart (graph with legend) is taken from df_2
, and the right chart is taken from df_1
.
However, I cannot make two plots side by side by dividing the y axis.
Here's my current way of building:
df_1[target_cols].plot(kind='barh', x='LABEL', stacked=True, legend=False)
df_2[target_cols].plot(kind='barh', x='LABEL', stacked=True).invert_xaxis()
plt.show()
As a result of the code, there will be two graphs in two different "canvases".
- How can I make them side by side by dividing the y axis?
- How do I remove the Y-axis label for the left side chart (chart derived from
df_2
)?
Any suggestions would be much appreciated. Thank.
+3
source to share
1 answer
You can create general subheadings with plt.subplots(sharey=True)
. Then draw the dataframes in two subheadings.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
a = np.random.randint(5,15, size=10)
b = np.random.randint(5,15, size=10)
df = pd.DataFrame({"a":a})
df2 = pd.DataFrame({"b":b})
fig, (ax, ax2) = plt.subplots(ncols=2, sharey=True)
ax.invert_xaxis()
ax.yaxis.tick_right()
df["a"].plot(kind='barh', x='LABEL', legend=False, ax=ax)
df2["b"].plot(kind='barh', x='LABEL',ax=ax2)
plt.show()
+6
source to share