0
import matplotlib
import matplotlib.pyplot as plt
import numpy as np

colors = dict()
colors['sats'] = 'b'
colors['cache'] = 'orange'
colors['3h'] = 'k'
colors['memory'] = 'g'
colors['integer'] = 'r'
#####################################
sz = np.array([ 2.,  4.,  6.,  8.])


t1 = np.array([  5.9718688 ,  13.23303584,  23.9000157 ,  37.94884449])
t2 = np.array([  7.38123836,  16.45442501,  29.56201748,  46.60925727])
t3 = np.array([   8.87223103,   19.89443438,   36.27273578,   57.65372517])
t4 = np.array([   9.88902238,   22.11467593,   40.2324561 ,   63.90151877])
t4 -= t3
t3 -= t2
t2 -= t1
plt.bar(sz, t1, color=colors['sats'], edgecolor='white', label="SATS")
plt.bar(sz, t2, bottom=np.array(t1), color=colors['cache'], edgecolor='white', label="Caching values")
plt.bar(sz, t3, bottom=np.array(t1)+np.array(t2), color=colors['memory'], edgecolor='white', label="Optimizing memory access")
plt.bar(sz, t4, bottom=np.array(t1)+np.array(t2)+np.array(t3), color=colors['integer'], edgecolor='white', label="Integer SATS")
plt.ylim(top=70)
plt.xlabel('Filter radius')
plt.ylabel('Cumulative speedup ratio')
plt.legend()
plt.savefig("benchmark-cpu-relativit.pdf", transparent = True, bbox_inches = 'tight', pad_inches = 0)
plt.close()

Add text on top of all bars

So I want to add the total value on top of the bars. For example, first bar should have 10, second bar should have 22, ...

I tried barh but I get error TypeError: bar() got multiple values for keyword argument 'bottom'

Duke Le
  • 332
  • 3
  • 14
  • Check here: https://stackoverflow.com/questions/21688028/displaying-totals-above-stacked-bars-in-barchart-matplotlib-pyplot/21688214 – IoaTzimas Sep 14 '20 at 09:28
  • Does this answer your question? [Displaying totals above stacked bars in barchart: matplotlib.pyplot](https://stackoverflow.com/questions/21688028/displaying-totals-above-stacked-bars-in-barchart-matplotlib-pyplot) – JohanC Sep 14 '20 at 11:56

1 Answers1

1

I made mine a function. My next goal is to get totals for all areas in my case counties, and display that as well. Feel free to take out the function aspect. Not needed.

This part I believe you have, but wanted to keep everything together

def getTotalCounts(dfSS): 
county = dfSS.groupby(['County']).count()  # this will put "COUNTY"on Y-Axis and  
                                           #counts all the values from other columns
counts = county.loc[:,"Number of Vehicles Removed"]  # left side :  all rows , right side: is the column we want to look at. 

counts.plot(kind='bar', x='county', y='counts') #this makes our bar chart from the 
                                                #data.

#This added totals for each section to the top of our bar chart, centered

   for i in range(len(county)):   
    plt.text(i, counts[i], counts[i], ha='center', va='bottom') #i for each index. [i] 
     # for the height of counts(the bar/number of "whatever you're counting")
     # the 2nd count[i] is for displaying numbers but we can put "text" there

More add-ons if needed

plt.title('Total Vehicle Removal Counts By County') #title the bar chart
plt.xlabel("Counties")  # labels the X-Axis on our graph
plt.ylabel('Vehicles Removed')  # labels the Y-Axis on our graph
plt.legend()  # add a legend for our graph

# plt.xticks([1,2,3]) # change to x tick values and how much they go or down by
# plt.yticks([1,2,3]) # change to y tick values and how much they go or down by

plt.show()  # show or print out the graph to view

Side note this is not for stacked bar charts, but individual bars in our bar chart.

JQTs
  • 142
  • 2
  • 11