0

Below is the dataframe that I am trying to print using Python 3.6.3. Prior to changing the colors using the code below, my chart prints perfectly find. However, now the chart prints without any bars.

Also, I can't figure out how to change all the bar chart colors using the list below. I'm working on a project in Jupyter and the chart prints out fine. The only problem there is that the colors are still not adjusted according to the list. Only orange and green print out.

              market_cap_usd  market_cap_perc
id
bitcoin         1.862130e+11        33.481495
ethereum        1.141650e+11        20.527111
ripple          4.906157e+10         8.821376
bitcoin-cash    2.782094e+10         5.002265
cardano         1.522458e+10         2.737413
neo             1.041332e+10         1.872338
stellar         9.829044e+09         1.767283
litecoin        9.758786e+09         1.754651
eos             8.518116e+09         1.531575
nem             7.917498e+09         1.423583

# Colors for the bar plot
COLORS = ['orange', 'green', 'orange', 'cyan', 'cyan', 'blue', 'silver', 'orange', 'red', 'green']

# Plotting market_cap_usd as before but adding the colors and scaling the y-axis  
ax = cap10.plot.bar(color=COLORS, title=TOP_CAP_TITLE, logy=True)

# Annotating the y axis with 'USD'
ax.set_ylabel('USD')

# Final touch! Removing the xlabel as it is not very informative
# ... YOUR CODE FOR TASK 5 ...
ax.set_xlabel('')
plt.show(ax)
user8851623
  • 722
  • 8
  • 11

1 Answers1

1

You can modify this solution by custom colors and set ylim:

COLORS1 = ['orange', 'green', 'orange', 'cyan', 'cyan', 
           'blue', 'silver', 'orange', 'red', 'green']
COLORS2 = [ 'green', 'orange', 'cyan', 'cyan', 'blue', 
            'silver', 'orange', 'red', 'green','orange']

TOP_CAP_TITLE = 'title'

fig = plt.figure() # Create matplotlib figure

ax = fig.add_subplot(111) # Create matplotlib axes
ax2 = ax.twinx() # Create another axes that shares the same x-axis as ax.

width = 0.4
cap10['market_cap_usd'].plot(kind='bar', title=TOP_CAP_TITLE, color=COLORS1, ax=ax, width=width, position=1, logy=True)
cap10['market_cap_perc'].plot(kind='bar', color=COLORS2, ax=ax2, width=width, position=0)

# Annotating the y axis with 'USD'
ax.set_ylabel('USD')
ax2.set_ylabel('perc')

#set max limit for y of second plot
ax2.set_ylim([0,cap10['market_cap_perc'].max() + 20])

ax.set_xlabel('')
ax2.set_xlabel('')
plt.show()

graph

Another solution is plot each column separately:

TOP_CAP_TITLE = 'title'

ax = plt.subplot(211)
ax2 = plt.subplot(212)

cap10['market_cap_usd'].plot(kind='bar',title=TOP_CAP_TITLE, color=COLORS1, ax=ax, logy=True)
cap10['market_cap_perc'].plot(kind='bar', color=COLORS2, ax=ax2)

# Annotating the y axis with 'USD'
ax.set_ylabel('USD')
ax2.set_ylabel('perc')

#if want remove labels of x axis in first plot
ax.set_xticklabels([])
ax.set_xlabel('')
ax2.set_xlabel('')
plt.show()

graph1

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252