0

This is what my bar chart looks like ..

[![mychart][1]][1]

I wish I could format the axhline text with a thousand separator. Is there a way to do this? I have looked at FuncFormatter(lambda x, pos: "[%.2f]" % x) so I wonder if I could do it within plt.text somehow?

my code for the axhline:

yline = plt.axhline(y, zorder=0, color='indigo')
ytext = plt.text(1995.05, y, 'y = %d' %y, bbox=dict(fc='white',ec='red'))

I have tried as

y_text = plt.text(1995.05, y,'{:,.2f}'.format(y), bbox=dict(fc='white',ec='red'))

and this does not work.

y_text = plt.text(1995.05, y, f'{y:,%d}') 

---> 53 ytext = plt.text(1995.05, y, f'{y:,%d}', bbox=dict(fc='white',ec='darkviolet'))
     54 
     55 
ValueError: Invalid format specifier.


  [1]: https://i.stack.imgur.com/puKDC.jpg
Bluetail
  • 1,093
  • 2
  • 13
  • 27
  • Does this answer your question? [How to print number with commas as thousands separators?](https://stackoverflow.com/questions/1823058/how-to-print-number-with-commas-as-thousands-separators) – JohanC May 11 '20 at 23:01
  • No, it does not. I edited the question. – Bluetail May 11 '20 at 23:28
  • It still does not work, Jordan. ValueError: Unknown format code 'd' for object of type 'float' for plt.text(1995.05, y, f'{y:,d}'). however, plt.text(1995.05, y, 'y = %d' %y) is not causing any error. – Bluetail May 12 '20 at 16:33
  • If `y` is a float, you can use `plt.text(..., ..., f'{y:,.0f}')`. In general, one gets much better answers if one creates some standalone test code. – JohanC May 12 '20 at 16:54
  • y is just an arbitrary number I put in, like y= 40602. so, yes a float. – Bluetail May 12 '20 at 17:41
  • Yay, you're a star! f'{y:,.0f}' works! sorry if I sounded stupid. it is one of the coursera courses with no mentor and i'm learning. – Bluetail May 12 '20 at 17:45

0 Answers0