I'm using pandas to plot some data.
If I plot this:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'a': [100, 200, 150, 175],
'b': [430, 30, 20, 10]})
fig, ax1 = plt.subplots(figsize=(15, 10))
df['b'].plot(kind='bar', color='y')
df['a'].plot(kind='line', marker='d')
Everything plots fine.
If I plot the bar axis on the secondary axis, the bar plot will be in front of the line plots, obstructing the lines from being viewed, like this.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'a': [100, 200, 150, 175],
'b': [430, 30, 20, 10]})
fig, ax1 = plt.subplots(figsize=(15, 10))
df['b'].plot(kind='bar', color='y', secondary_y=True)
df['a'].plot(kind='line', marker='d')
How do I make a bar plot/line plot where...
A stacked Bar plot is a kind of bar graph in which each bar is visually divided into sub bars to represent multiple column data at once. To plot the Stacked Bar plot we need to specify stacked=True in the plot method. We can also pass the list of colors as we needed to color each sub bar in a bar.
you could put line on primary axis.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'a': [100, 200, 150, 175],
'b': [430, 30, 20, 10]})
fig, ax1 = plt.subplots(figsize=(15, 10))
df['b'].plot(kind='bar', color='y')
df['a'].plot(kind='line', marker='d', secondary_y=True)
Or, create two axes ax1
and ax2
with twinx()
.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'a': [100, 200, 150, 175],
'b': [430, 30, 20, 10]})
fig, ax1 = plt.subplots(figsize=(15, 10))
ax2 = ax1.twinx()
df['b'].plot(kind='bar', color='y', ax=ax1)
df['a'].plot(kind='line', marker='d', ax=ax2)
ax1.yaxis.tick_right()
ax2.yaxis.tick_left()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With