I am trying to create a bar plot with matplotlib in which x values are in one column and the percentages of these values are in a separate column.
Gender Gain/Loss Final corrected classification Values percent
0 F G Benign 5619 11.658886
1 F G Likely Benign 7288 15.121901
2 F G Likely Pathogenic 324 0.672269
To carry out out my plot I have been working with the code I show below but I am not able to annotate the values of the percentage column next to the bars
Gender Gain/Loss Final corrected classification Values percent
0 F G Benign 5619 11.658886
1 F G Likely Benign 7288 15.121901
2 F G Likely Pathogenic 324 0.672269
To carry out out my plot I have been working with the code I show below but I am not able to annotate the values of the percentage column next to the bars
# Figure Size
fig, ax = plt.subplots(figsize =(16, 9))
# Horizontal Bar Plot
ax.barh(s['Final corrected classification'], s['Values'])
# Remove axes splines
for s in ['top', 'bottom', 'left', 'right']:
ax.spines[s].set_visible(False)
# Remove x, y Ticks
ax.xaxis.set_ticks_position('none')
ax.yaxis.set_ticks_position('none')
# Add padding between axes and labels
ax.xaxis.set_tick_params(pad = 5)
ax.yaxis.set_tick_params(pad = 10)
# Add x, y gridlines
ax.grid(b = True, color ='grey',
linestyle ='-.', linewidth = 0.5,
alpha = 0.2)
# Show top values
ax.invert_yaxis()
# Add annotation to bars
for p in ax.patches:
percentage = s["percent"].astype(int)
x = p.get_x() + p.get_width() + 0.02
y = p.get_y() + p.get_height()/2
ax.annotate(percentage, (x, y))
# Add Plot Title
ax.set_title('Number of variants',
loc ='left', )
# Add Text watermark
fig.text(0.9, 0.15, 'STP', fontsize = 12,
color ='grey', ha ='right', va ='bottom',
alpha = 0.7)
# Show Plot
plt.show()I have been getting different error, so far I got thisError:---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-365-151b04ec814c> in <module>
29
30 for p in ax.patches:
---> 31 percentage = s["percent"].astype(int)
32 x = p.get_x() + p.get_width() + 0.02
33 y = p.get_y() + p.get_height()/2
TypeError: string indices must be integers
[url=https://djcontrollerhub.com/]djcontrollerhub[/url]
Larz60+ write Mar-11-2021, 08:17 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Have done this for you this time. Pleas use bbcode tags on future posts.
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Have done this for you this time. Pleas use bbcode tags on future posts.
