I plotted a horizontal stacked bar chart and when labeling the bars using the input data values the number of decimal places is more than 10 in some of the labels as shown in the image. How can I round off these numbers to 1 decimal place in my script. Input data also has one decimal place.
Input data:
Forestland Shrubland (Ha) Invasion Cropland Forestland Shrubland Total_Damage Samburu_2021 1.4 64.1 47.2 112.7 Samburu_2020 2.1 30.2 49.6 81.9 Laikipia_2021 20.9 20.0 19.8 60.7 Laikipia_2020 5.3 11.9 17.1 34.3
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from matplotlib import pyplot
labels = {0:'Cropland', 1:'Forestland', 2:'Shrubland'}
colors = {0:'#FFDB5C', 1:'#358221',2:'#EECFA8'}
patches = [mpatches.Patch(color=colors[i], label=f'{labels[i]}') for i in range(len(labels))]
# creating a stacked bar plot visualization
fig,ax = plt.subplots(figsize = (12,5))
ax.barh(y = combined_df.index,width = combined_df['Cropland'].values, height = 0.6, color = '#FFDB5C')
ax.barh(y = combined_df.index, width = combined_df['Forestland'].values, height = 0.6, left =combined_df['Cropland'].values,color = '#358221')
ax.barh(y = combined_df.index, width = combined_df['Shrubland'].values, height = 0.6, left =combined_df['Forestland'].values + combined_df['Cropland'].values ,color = '#EECFA8')
for patch in ax.patches:
x,y = patch.get_xy()
w, h = patch.get_width(), patch.get_height()
ax.text(x+w/2, y+h/2, str(w),
ha='left', va='center',
color='black', fontweight='normal', fontsize=9,
)
ax.set_xticks(np.arange(0,120,5))
ax.set_yticks(np.arange(len(combined_df.index)))
#labelling the figure
#plt.title('Total area where NDVI reduction was observed in Samburu and Laikipia counties categorized by LULC ')
plt.xlabel('Area in hectares (ha)')
plt.ylabel('County')