Related
I want to remove the percentage values on top of each plot, or possibly round them
width = 0.2
x = np.arange(len(labels))
fig2,ax = plt.subplots()
rects1 = ax.bar(x - width/2, precision_data, width, label='precision',color ='firebrick')
rects2 = ax.bar(x + width/2 , recall_data, width, label='recall',color = 'royalblue')
ax.set_ylabel('Score %')
ax.set_title('precision-recall average classifiers scores')
ax.set_xticks(x, labels)
ax.legend()
ax.bar_label(rects1)
ax.bar_label(rects2)`
To remove the text on top of your bars, simply comment out ax.bar_label(rects1) and ax.bar_label(rects2):
To round the labels, you may use the fmt argument: ax.bar_label(labels, fmt='%.2f')
This question already has answers here:
How to add value labels on a bar chart
(7 answers)
Closed last year.
I have the following code:
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('ggplot')
def autolabel(rects):
"""Attach a text label above each bar in *rects*, displaying its height."""
for rect in rects:
height = int(rect.get_height())
ax.annotate('{}'.format(height),
xy=(rect.get_x() + rect.get_width() / 2, height),
xytext=(0, 3), # 3 points vertical offset
textcoords="offset points",
ha='center', va='bottom', fontsize=6)
data = [108, 140.9, 187, 237.6, 299.2, 360.9, 413.3, 431.9, 437.2, 441.9]
set1 = [140.973, 161.588, 202.391, 213.57, 408.55, 442.648, 491.883, 517.456, 534.018, 545.594]
set2 = [140.386, 156.932, 200.106, 213.789, 401.426, 440.09, 490.252, 516.478, 533.255, 545.232]
set3 = [141.046, 162.663, 202.05, 213.613, 408.678, 442.685, 491.894, 517.552, 534.028, 545.858]
stage = [1,2,3,4,5,6,7,8,9,10]
x = np.arange(len(stage)) # the label locations
y = [50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600]
width = 0.20 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(x - 3*width/2, data, width, color = "tab:gray", label='Data')
rects2 = ax.bar(x - width/2, set1, width, color = "tab:red", label='Set 1')
rects3 = ax.bar(x + width/2, set2, width, color = "tab:blue", label='Set 2')
rects4 = ax.bar(x + 3*width/2, set3, width, color = "tab:purple", label='Set 3')
ax.set_yticks(y)
ax.set_yticklabels(y, fontsize=10)
ax.set_ylim(0,580)
ax.set_xticks(x)
ax.set_xticklabels(stage, fontsize=10)
ax.legend(fontsize=8)
ax.grid(True)
autolabel(rects1)
autolabel(rects2)
autolabel(rects3)
autolabel(rects4)
fig.savefig("plot.png", dpi=300)
which gives me the following bar plot:
Bar Plot
Can anyone help me to plot this with readable text on top of all the bars? I tried increasing the width for more text visibility but then the bars at second point on the x-axis and so on are overlapped with previous bars.
matplotlib now has a method for adding labels to bars, bar_label. You can replace your custom function with that and add your options there. To make your labels fit, without reducing the size of the text further, you can rotate by 90 degrees e.g.
ax.bar_label(rects1, fmt="%d", fontsize=6, rotation=90, padding=3)
I have created the following figure, but I want as annotation to add the percentage for each bar above, not the numbers. Can someone help how to do this in this case of three bars side-by-side. Note: I want the sum of the percentages to be 100% per group of three bars. I know only how to do this only for each category...
fig = plt.figure(figsize=(15, 8));
ax5 = fig.add_subplot(1, 1, 1);
ax5.set_xticklabels(ff.index.tolist(), size=12)
# Custom Y axis-ticks
y_ticks = np.arange(0, 400, 50)
yrange = (y_ticks[0], y_ticks[-1])
ax5.set_ylim(yrange)
ax5.set_yticklabels(y_ticks, size = 12)
ax5.set_yticks(y_ticks)
# set the xlim
ax5.set_xlim(0, len(labels))
# get your locations
dim = np.arange(0.35,len(labels),1);
# set the locations of the xticks to be on the integers
ax5.set_xticks(dim)
# Custom X - label
ax5.set_xlabel('Mode of Information', size=16, fontweight='bold')
# Custom X - label
ax5.set_ylabel('Number of volunteers', size=16, fontweight='bold')
rects1 = ax5.bar(x + width, Rem, width, label='Remained the same', color='coral',
edgecolor='black')
rects2 = ax5.bar(x + (width*2), Inc, width, label='Increased', color='forestgreen',
edgecolor='black')
rects3 = ax5.bar(x + (width*3), Dec, width, label='Decreased', color='royalblue',
edgecolor='black')
for i,rect in enumerate(rects1): # for each bar
height = rect.get_height()
ax5.text(rect.get_x() + rect.get_width() / 2, rect.get_height() + 3, '%s'% (height),
ha='center', va='bottom', color = 'black', size = 12)
for i,rect in enumerate(rects2): # for each bar
height = rect.get_height()
ax5.text(rect.get_x() + rect.get_width() / 2, rect.get_height() + 3, '%s'% (height),
ha='center', va='bottom', color = 'black', size = 12)
for i,rect in enumerate(rects3): # for each bar
height = rect.get_height()
ax5.text(rect.get_x() + rect.get_width() / 2, rect.get_height() + 3, '%s'% (height),
ha='center', va='bottom', color = 'black', size = 12)
You could make a list of totals which gives you the total within each group:
totals = [x.get_height() + y.get_height() + z.get_height()
for x, y, z in zip(rects1, rects2, rects3)]
And then in the three annotation loops, divide the height text by totals[i]:
# '%s' % (height) # old
'%.1f%%' % (100 * height / totals[i]) # new
I have been playing around with Python for the last couple of days and found a lot of good resources about labelling, but I am failing to make it properly display negative values. Because the autolabel() function takes the height of the bar, which seems to always be a positive value, the labels are displayed way up in the graph and are of course not displayed as negative values. Can I somehow get the values that make up these bars or how do I get these labels down where they belong and show them as negative?
import pandas as pd
import matplotlib.pyplot as plt
from builtins import list
import matplotlib
matplotlib.style.use('ggplot')
import numpy as np
n_groups = 2
# create plot
fig, ax = plt.subplots()
fig.canvas.set_window_title('Mindestlohn Bundesweit')
index = np.arange(n_groups)
bar_width = 0.20
opacity = 0.8
list_reallohn_week_vollzeit = [-8.159698443426123, 11.395025597733763]
list_reallohn_week_teilzeit = [-1.048913873322391, 28.99318154295449]
list_reallohn_week_mini = [-7.552596893170488, 7.959096278017519]
rects1 = plt.bar(index + 0.00, list_reallohn_week_vollzeit, bar_width,
alpha=opacity,
color='b',
label='Vollzeit')
rects2 = plt.bar(index + bar_width, list_reallohn_week_teilzeit, bar_width,
alpha=opacity,
color='g',
label='Teilzeit')
rects3 = plt.bar(index + bar_width * 2,list_reallohn_week_mini, bar_width,
alpha = opacity,
color='c',
label='Mini Job')
label_week_lists = ('2014 vor MdL', '2015 Nicht MdL berechtigt', '2015 mit MdL')
plt.ylabel('EUR')
plt.title('Reallöhne pro Woche')
plt.xticks(index + bar_width, label_week_lists)
plt.legend(bbox_to_anchor=(1, 1),
bbox_transform=plt.gcf().transFigure)
def autolabel(rects, ax):
# Get y-axis height to calculate label position from.
(y_bottom, y_top) = ax.get_ylim()
y_height = y_top - y_bottom
for rect in rects:
height = rect.get_height()
# Fraction of axis height taken up by this rectangle
p_height = (height / y_height)
# If we can fit the label above the column, do that;
# otherwise, put it inside the column.
if p_height > 0.95: # arbitrary; 95% looked good to me.
label_position = height - (y_height * 0.05)
else:
label_position = height + (y_height * 0.01)
ax.text(rect.get_x() + rect.get_width() / 2., label_position,
'%d' % int(height),
ha='center', va='bottom')
autolabel(rects1, ax)
autolabel(rects2, ax)
autolabel(rects3, ax)
plt.show()
mathplotlib is not very well documented in that department. Try using the dir() function to reveal the available options you have on the container you're working on. i found there is a .get_y() function which retuns negative numbers in that case
try this code instead
import pandas as pd
import matplotlib.pyplot as plt
from builtins import list
import matplotlib
matplotlib.style.use('ggplot')
import numpy as np
n_groups = 2
# create plot
fig, ax = plt.subplots()
fig.canvas.set_window_title('Mindestlohn Bundesweit')
index = np.arange(n_groups)
bar_width = 0.20
opacity = 0.8
list_reallohn_week_vollzeit = [-8.159698443426123, 11.395025597733763]
list_reallohn_week_teilzeit = [-1.048913873322391, 28.99318154295449]
list_reallohn_week_mini = [-7.552596893170488, 7.959096278017519]
rects1 = plt.bar(index + 0.00, list_reallohn_week_vollzeit, bar_width,
alpha=opacity,
color='b',
label='Vollzeit')
rects2 = plt.bar(index + bar_width, list_reallohn_week_teilzeit, bar_width,
alpha=opacity,
color='g',
label='Teilzeit')
rects3 = plt.bar(index + bar_width * 2,list_reallohn_week_mini, bar_width,
alpha = opacity,
color='c',
label='Mini Job')
label_week_lists = ('2015 Nicht MdL berechtigt', '2015 mit MdL')
plt.ylabel('EUR')
plt.title('Reallöhne pro Woche')
plt.xticks(index + bar_width, label_week_lists)
plt.legend(bbox_to_anchor=(1, 1),
bbox_transform=plt.gcf().transFigure)
def autolabel(rects, ax):
# Get y-axis height to calculate label position from.
(y_bottom, y_top) = ax.get_ylim()
y_height = y_top - y_bottom
for rect in rects:
# print(dir(rect))
height = 0
if rect.get_y() < 0:
height = rect.get_y()
else:
height = rect.get_height()
print(rect.get_height())
print( str(rect.get_y()) )
# Fraction of axis height taken up by this rectangle
p_height = (height / y_height)
# If we can fit the label above the column, do that;
# otherwise, put it inside the column.
if p_height > 0.95: # arbitrary; 95% looked good to me.
label_position = height - (y_height * 0.05)
else:
label_position = height + (y_height * 0.01)
ax.text(rect.get_x() + rect.get_width() / 2., label_position,
'%d' % int(height),
ha='center', va='bottom')
autolabel(rects1, ax)
autolabel(rects2, ax)
autolabel(rects3, ax)
plt.show()
If you print rect.get_height() values you get something like:
-8.159698443426123
11.395025597733763
-1.048913873322391
28.99318154295449
-7.552596893170488
7.959096278017519
Therefore height of a bar maybe a negative.
To mark negative bars modify if statement in auto label function as follow:
if p_height > 0.95: # arbitrary; 95% looked good to me.
label_position = height - (y_height * 0.05) if (height > 0) else height + (y_height * 0.05)
else:
label_position = height + (y_height * 0.01) if (height > 0) else height - (y_height * 0.05)
You have to ajust coefficients in else branch (0.05) manually because position of a below label depends on a font size of a label. (Position of a label for positive bar does not affected by font size because a label is above the bar).
Finale for my font settings:
So for the life of me i can't figure out how to get the labels to show decimal places and not just 0,1,2
i need them to be in decimal form below is my code in python 3
#ROE and Growth
Tax_Burden = stock.loc['Net Income']/stock.loc['Pre-Tax Income']
Interest_Burden= stock.loc['Pre-Tax Income']/stock.loc['Operating Income']
Operating_Margin= stock.loc['Operating Income']/stock.loc['Revenue']
Asset_Turnover= stock.loc['Revenue']/stock.loc['Total Assets Average']
Leverage_Ratio= stock.loc['Total Assets Average']/stock.loc['Total Equity Average']
roe=Tax_Burden*Interest_Burden*Operating_Margin*Asset_Turnover*Leverage_Ratio
Growth = roe * (1-stock.loc['Dividend Payout Ratio'])
astart = 21
aend = 31
annual = [Operating_Margin[astart:aend],Tax_Burden[astart:aend],Interest_Burden[astart:aend],Asset_Turnover[astart:aend],Leverage_Ratio[astart:aend],roe[astart:aend],Growth[astart:aend]]
N = len(annual[0])
ind = np.arange(N) # the x locations for the groups
width = .12 # the width of the bars
fig, ax = plt.subplots(figsize=(20,10))
rects1 = ax.bar(ind, annual[0], width, color='y')
rects2 = ax.bar(ind+width, annual[1], width, color='r')
rects3 = ax.bar(ind+width*2, annual[2], width, color='b')
rects4 = ax.bar(ind+width*3, annual[3], width, color='k')
rects5 = ax.bar(ind+width*4, annual[4], width, color='c')
rects6 = ax.bar(ind+width*5, annual[5], width, color='k')
rects7 = ax.bar(ind+width*6, annual[6], width, color='r')
# add some text for labels, title and axes ticks
ax.set_ylabel('Percentage')
ax.set_title('ROE Annual')
ax.set_xticks(ind + width / 2)
ax.set_xticklabels(list(stock.loc['Fiscal Period'][astart:aend]))
#ax.legend((rects1[0], rects2[0]), ('workinprogress'))
def autolabel(rects, ax):
# Get y-axis height to calculate label position from.
(y_bottom, y_top) = ax.get_ylim()
y_height = y_top - y_bottom
for rect in rects:
height = rect.get_height()
# Fraction of axis height taken up by this rectangle
p_height = (height / y_height)
# If we can fit the label above the column, do that;
# otherwise, put it inside the column.
if p_height > 0.95: # arbitrary; 95% looked good to me.
label_position = height - (y_height * 0.05)
else:
label_position = height + (y_height * 0.01)
ax.text(rect.get_x() + rect.get_width()/2, label_position,
'%d' % int(height),
ha='center', va='bottom')
autolabel(rects1,ax)
autolabel(rects2,ax)
autolabel(rects3,ax)
autolabel(rects4,ax)
autolabel(rects5,ax)
autolabel(rects6,ax)
autolabel(rects7,ax)
plt.show()
i am aware it is not pretty as of now and not lazy need to make sore more functions but can't seem to get past this issue. thanks for looking at.
EDIT: For those looking in the future the issue was the S operator here matplotlib documentation. Jay helped clarify below. i am attaching my code and new chart so can be copied for ease. still needs a little tweaking but that is personal preference.
astart = 21
aend = 31
annual = [Operating_Margin[astart:aend],Tax_Burden[astart:aend],Interest_Burden[astart:aend],Asset_Turnover[astart:aend],Leverage_Ratio[astart:aend],roe[astart:aend],Growth[astart:aend]]
N = len(annual[0])
ind = np.arange(N) # the x locations for the groups
width = .12 # the width of the bars
fig, ax = plt.subplots(figsize=(20,10),facecolor='#c8f2e5')
rects1 = ax.bar(ind, annual[0], width, color='#f29ca2')
rects2 = ax.bar(ind+width, annual[1], width, color='#61eaf2')
rects3 = ax.bar(ind+width*2, annual[2], width, color='#6da4d9')
rects4 = ax.bar(ind+width*3, annual[3], width, color='#f2bb12')
rects5 = ax.bar(ind+width*4, annual[4], width, color='c')
rects6 = ax.bar(ind+width*5, annual[5], width, color='#ce44f2')
rects7 = ax.bar(ind+width*6, annual[6], width, color='r')
ax.set_facecolor('#a7cff2')
# add some text for labels, title and axes ticks
ax.set_ylabel('Percentage',size=20)
ax.set_title('ROE Annual',size=30)
ax.set_xticks(ind + width / 2)
ax.set_xticklabels(list(stock.loc['Fiscal Period'][astart:aend]),size=14)
vals = ax.get_yticks()
ax.set_yticklabels(['{:3.2f}%'.format(x*100) for x in vals])
ax.legend((rects1[0], rects2[0], rects3[0], rects4[0], rects5[0], rects6[0], rects7[0]),('Operating Margin', 'Tax Burden','Interest Burden','Asset Turnover', 'Leverage Ratio','ROE','Growth'))
def autolabel(rects, ax):
# Get y-axis height to calculate label position from.
(y_bottom, y_top) = ax.get_ylim()
y_height = y_top - y_bottom
for rect in rects:
height = rect.get_height()
# Fraction of axis height taken up by this rectangle
p_height = (height / y_height)
# If we can fit the label above the column, do that;
# otherwise, put it inside the column.
if p_height > 0.95: # arbitrary; 95% looked good to me.
label_position = height - (y_height * 0.05)
else:
label_position = height + (y_height * 0.01)
ax.text(rect.get_x() + rect.get_width()/2, label_position,
'%.2f' % float(height),
ha='center', va='bottom',color='k',fontsize=12)
#
autolabel(rects1,ax)
autolabel(rects2,ax)
autolabel(rects3,ax)
autolabel(rects4,ax)
autolabel(rects5,ax)
autolabel(rects6,ax)
autolabel(rects7,ax)
plt.show()
I think problem is with below statement. Instead of int, use float
ax.text(rect.get_x() + rect.get_width()/2, label_position,
'%.2f' % float(height),
ha='center', va='bottom')
The solution by Jay did not work for me, but this did:
for p in ax.patches:
ax.annotate(str(p.get_height()), (p.get_x() * 1.005, p.get_height() * 1.002))