I have a report that I'm trying to add several subplots to. Using this example, I was able to create a combined line/bar fig How to align the bar and line in matplotlib two y-axes chart?
However, I now need to add this as a subplot to an existing plot (if that makes sense). Everything is working for me in terms of formatting, but for some reason I'm not able to show both the line and the bar, only 1 at a time. This is the code I've configured, can someone please let me know what I'm doing wrong?
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(8.5,11))
ax2 = plt.subplot2grid((4, 2), (0, 1))
def billions(x, pos):
'The two args are the value and tick position'
return '$%1.1fB' % (x*1e-9)
def millions(x, pos):
'The two args are the value and tick position'
return '$%1.1fM' % (x*1e-6)
formatterb = plt.FuncFormatter(billions)
formatterm = plt.FuncFormatter(millions)
barax = ax2.twinx()
data = growthtable[['date','total','profit']]
barax.set_ylabel('Total')
ax2.set_ylabel('Profit')
barax.xaxis.tick_top()
barax.yaxis.set_major_formatter(formatterm)
ax2.yaxis.set_major_formatter(formatterb)
barax.set_title('Revenue and Profits')
data['Revenue'].plot(kind='bar',ax=ax2,facecolor='blue')
data['Profit'].plot(ax=ax2)
Seems very simple/standard, but for some reason depending on the order I place the last 2 lines I either get to see profit or revenue, not both.
Updating the code I get this:
ax2 = plt.subplot2grid((4, 2), (0, 1))
def billions(x, pos):
'The two args are the value and tick position'
return '$%1.1fB' % (x*1e-9)
def millions(x, pos):
'The two args are the value and tick position'
return '$%1.1fM' % (x*1e-6)
formatterb = FuncFormatter(billions)
formatterm = FuncFormatter(millions)
barax = ax2.twinx()
barax.plot(data.index, data['Profit'], linewidth=3, color='#a64dff')
barax.xaxis.tick_top()
barax.yaxis.set_major_formatter(formatterm)
ax2.yaxis.set_major_formatter(formatterb)
ax2.bar(data.index, data['Revenue'], color='blue')
However as you can see I'm trying to change bar size and color, but that isn't working?