Question
How to plot a line on the second axis over a HORIZONTAL (not VERTICAL) bar chart with Matplotlib?
I know how to plot a line on the second axis over a VERTICAL bar chart.
Now I want the bars HORIZONTAL and the line top-to-bottom, just like the whole chart rotates 90°.
If I simply replace bar
with barh
, the line is still left-to-right...
Can I do this with Matplotlib?
Here is a sample for VERTICAL bar chart:
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame(
{
"A": [2, 4, 8],
"B": [3, 5, 7],
"C": [300, 100, 200],
},
index=["a", "b", "c"],
)
ax0 = df.plot(y=df.columns[:2], kind="bar", figsize=(10.24, 5.12))
ax1 = df["C"].plot(
kind="line",
secondary_y=True,
color="g",
marker=".",
)
plt.tight_layout()
plt.show()
Let me stress: I do see those questions related to VERTICAL bar charts. Now I'm asking about HORIZONTAL bar charts.
So this is not a duplicate question.