0

I have working code as follows:

active_parking = pd.pivot_table(parking[parking['Bldg Status'] == 'ACTIVE'],
                                index='Owned/Leased',
                                values='Total Parking Spaces',
                                aggfunc='mean'
                                )
active_parking['% of Total'] = ((active_parking['Total Parking Spaces'] / active_parking['Total Parking Spaces'].sum()) * 100)
print(active_parking, '\n')

active_parking.plot(kind='bar')
for i, number in enumerate(active_parking['Total Parking Spaces']):
    plt.text(x=i, y=number, s=number, horizontalalignment='center', weight='bold')
for i, number in enumerate(active_parking['% of Total']):
    plt.text(x=i, y=number, s=number, horizontalalignment='left', weight='bold')
plt.xticks(rotation=0)
plt.show()

And it produces this output:

              Total Parking Spaces  % of Total
Owned/Leased                                  
LEASED                   44.707349   37.546059
OWNED                    74.365997   62.453941 

But my plot has two issues that I can't seem to solve:

1)  I want the displayed value on top of each bar limited to two decimal places.
2)  After solving the above issue, how do I center the value over each bar?

enter image description here

Even if I crop the text display with this: pd.options.display.float_format = '{:.2f}'.format, the plot still shows 14 decimal places.

MarkS
  • 1,455
  • 2
  • 21
  • 36

1 Answers1

0

Use:

active_parking.plot(kind='bar')
for i, number in enumerate(active_parking['Total Parking Spaces']):
    plt.text(x=i-.23, y=number + 0.9, s=round(number, 2), weight='bold')
for i, number in enumerate(active_parking['% of Total']):
    plt.text(x=i+.02, y=number + 0.9, s=round(number, 2), weight='bold')
plt.xticks(rotation=0)
plt.ylim(top=active_parking.values.max() + 8)
plt.show()
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • 1
    That solved the precision, but I still can't get the displayed value centered over each bar. The closest I get is by using 'right' for Total Parking Spaces and 'left' for % of Total. And those only work because I have two columns. If I had more than two it wouldn't work. Is there a way to specify that each value is to be centered over each column? – MarkS Feb 09 '20 at 15:38
  • @MarkS - Can you check [this](https://stackoverflow.com/a/34598688/2901002) ? – jezrael Feb 09 '20 at 15:46
  • ```@jezrael``` Probably my lack of experience, but I tried the link you sent but it made it worse. The text is now along the x-axis (not on top of each bar as before) and shifted way to the right relative to each bar. I tried adjusting some of the settings but the output never changed. – MarkS Feb 09 '20 at 16:05
  • @MarkS - Answer was edited, I manually add some constatnt to `x` and `y` values, also set `ylim` to add `8` to maximal value of all data. – jezrael Feb 09 '20 at 16:20
  • 1
    ```@jezrael``` Now I see. The 'x' and 'y' values need to be manipulated. Plus the ylim parameter. Much obliged. – MarkS Feb 09 '20 at 17:58