Fighting with Matplotlib
So I was doing an assignment from a Python book I was reading. Seemed simple enough. Estimate how much money you would have in retirement based on starting capital, yearly expenses, and the length of your retirement. How better to understand the results than putting the numbers in a chart?
I turned to the common Matplotlib python package. Funny story - I originally thought it was called Mathplotlib. My first graph came up. I was impressed on how much graphics I could get for the short amount of code I had written.
One thing that bugged me was that the y-axis values were displayed in scientific notation. That was because I was assuming I started my retirement with a huge nest egg. By default, Matplotlib will show those big numbers in scientific notation. Ugly.
After some investigation, I found a quick way to override this was to specify a font style of "plain". That would prevent Matplotlib.from using scientific notation. Here is the code to accomplish that:
matplotlib.pyplot.ticklabel_format(style='plain',useMathText=True)
It still irked me that the numbers, while not in scientific notation, still did not come across as dollar amounts. I wanted to see a dollar sign in the front. And I wanted commas after every 3 digits. Turns out the way to accomplish that is to take over the formatting of the y-axis values. Not too difficult. Maybe I will write about the details in a future post.