Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Horizontal stacked bar chart with annotations

# transpose df from the OP so Party is the in the columns and Ward is the index
dft = df.T

# display(dft)
                               Labour  LD  Indep  Green  Tory
Abbey                              16  56      4      0    24
Albrighton                          0   0     32      0    68
Alveley & Claverley                 0  25      0      0    75
Bagley                             30  30      0      0    40
Battlefield                        34   0      0      9    57
Bayston Hill, Column & Sutton      53   4      3      7    33
Belle Vue                          43  28      0      5    24

# plot
ax = dft.plot.barh(stacked=True, figsize=(16, 10))

plt.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))

# annotations:
for p in ax.patches:
    left, bottom, width, height = p.get_bbox().bounds
    if width > 0:
         ax.annotate(f'{width:0.0f}', xy=(left+width/2, bottom+height/2), ha='center', va='center')
Comment

PREVIOUS NEXT
Code Example
Python :: child urls python 
Python :: Book.__init__() missing 5 required positional arguments 
Python :: python get combobox value 
Python :: python 2 factor authentication 
Python :: python datediff days 
Python :: the most effective search method in python with example 
Python :: colab show all value 
Python :: auto clicker 
Python :: bagging algorithm 
Python :: how to make change the default from python 3.8 to python 3.10.5 on Mint 20 
Python :: print command in python 
Python :: django null first 
Python :: a Python Numbers 
Python :: subprocess readline blocking problem 
Python :: turtle screen close error fix 
Python :: custom point annotation pyplot scatter 
Python :: Count occurrence of each term in dataframe except for NaN 
Python :: How to Merge QuerySets in Django Rest Framework 
Python :: python kdtree import 
Python :: python dataframe update if not new row 
Python :: python or in if statement 
Python :: tkinter titre fenetre 
Python :: a problem of predicting whether a student succeed or not based of his GPA and GRE. for logistic regression 
Python :: Python Script to check how many images are broken 
Python :: reportlab drawimage issues with png transparency background 
Python :: morphological filter example python 
Python :: multiply every nth element 
Python :: top automotive blogs 
Python :: pandas filter rows by fuzzy values 
Python :: Ornhgvshy vf orggre guna htyl 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =