Python Forum
Annotating plot bar from values of other a specific column
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Annotating plot bar from values of other a specific column
#1
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

# 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 this

Error:
--------------------------------------------------------------------------- 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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Get an average of the unique values of a column with group by condition and assign it klllmmm 0 286 Feb-17-2024, 05:53 PM
Last Post: klllmmm
  Converting column of values into muliple columns of counts highland44 0 256 Feb-01-2024, 12:48 AM
Last Post: highland44
  Printing specific values out from a dictionary mcoliver88 6 1,415 Apr-12-2023, 08:10 PM
Last Post: deanhystad
  PowerBI: Using Python Regex to look for values in column MarcusR44 1 978 Oct-14-2022, 01:03 PM
Last Post: ibreeden
  How to combine multiple column values into 1? cubangt 15 2,861 Aug-11-2022, 08:25 PM
Last Post: cubangt
  Creating a numpy array from specific values of a spreadsheet column JulianZ 0 1,126 Apr-19-2022, 07:36 AM
Last Post: JulianZ
  How to split file by same values from column from imported CSV file? Paqqno 5 2,801 Mar-24-2022, 05:25 PM
Last Post: Paqqno
  Tkinter Drawing/Annotating on live video KDog 4 2,792 Jan-24-2022, 03:18 PM
Last Post: Minimaxer
  Float Slider - Affecting Values in Column 'Pandas' planckepoch86 0 1,402 Jan-22-2022, 02:18 PM
Last Post: planckepoch86
  Sum the values in a pandas pivot table specific columns klllmmm 1 4,644 Nov-19-2021, 04:43 PM
Last Post: klllmmm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020