I have three bar charts that I have created. I would like to plot them all together using the fig, ax = plt.subplots function that is very popular. I can not get them to plot correctly together, but I can get them to show individually when I run each line below seperately
ax1 = buyreturns['return30'].plot.bar(grid=True, color=(buyreturns['return30'] > 0).map({True: 'g', False: 'r'}))
ax2 = buyreturns['return60'].plot.bar(grid=True, color=(buyreturns['return60'] > 0).map({True: 'g', False: 'r'}))
ax3 = buyreturns['return90'].plot.bar(grid=True, color=(buyreturns['return90'] > 0).map({True: 'g', False: 'r'}), legend = x)
I would like to get these to show up stacked on top of one another using the fig, ax = plt.subplots function.
I have tried the following code and the visual that is returned is an amalgamation of all the three barcharts, see the first picture below
fig, (ax1, ax2, ax3) = plt.subplots(3, 1, sharex=False, sharey= False, figsize=(24,16))
ax1 = buyreturns['return30'].plot.bar(grid=True, color=(buyreturns['return30'] > 0).map({True: 'g', False: 'r'}))
ax2 = buyreturns['return60'].plot.bar(grid=True, color=(buyreturns['return60'] > 0).map({True: 'g', False: 'r'}))
ax3 = buyreturns['return90'].plot.bar(grid=True, color=(buyreturns['return90'] > 0).map({True: 'g', False: 'r'}), legend = x)
plt.show()
I have also tried this following code and it returns what is in the second picture.
fig, (ax1, ax2, ax3) = plt.subplots(3, 1, sharex=False, sharey= False, figsize=(24,16))
ax1.bar(len(buyreturns), buyreturns['return30'], bar_width, color=(buyreturns['return30'] > 0).map({True: 'g', False: 'r'}))
ax2.bar(len(buyreturns), buyreturns['return60'], bar_width, color=(buyreturns['return60'] > 0).map({True: 'g', False: 'r'}))
ax3.bar(len(buyreturns), buyreturns['return90'], bar_width, color=(buyreturns['return90'] > 0).map({True: 'g', False: 'r'}))
plt.show()
Any input on how to fix these issues and return a 3 barchart graph would be great! Thanks
use ax=<the reference to your axes>
in the call to plot()
fig, (ax1, ax2, ax3) = plt.subplots(3, 1, sharex=False, sharey= False, figsize=(24,16))
buyreturns['return30'].plot.bar(grid=True, color=(buyreturns['return30'] > 0).map({True: 'g', False: 'r'}), ax=ax1)
buyreturns['return60'].plot.bar(grid=True, color=(buyreturns['return60'] > 0).map({True: 'g', False: 'r'}), ax=ax2)
buyreturns['return90'].plot.bar(grid=True, color=(buyreturns['return90'] > 0).map({True: 'g', False: 'r'}), ax=ax3)
Similar to Diziet's answer, but more programmatic :
fig, axes = plt.subplots(3, 1, sharex=False, sharey= False, figsize=(24,16))
# range(30,91,30) represents (30,60,90)
for i, ax in zip(range(30,91,30), axes):
col_name = f'return{i}'
colors = (buyreturns[col_name] > 0).map({True: 'g', False: 'r'})
buyreturns[col_name].plot.bar(grid=True,
color=colors,
ax=ax)
plt.show()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.