Retirement Simulator #7: Organize multiple plots with subplots! and learn about plt.pause()

Published Dec. 31, 2021, 9:32 p.m.

You can be a trillionaire!  If you work for 1000 years.  Maybe. 

Ok.  Last time we plotted our sample distribution and we felt good about it.  In our code, comment out or delete the lines:

# plt.show()
# sys.exit()

We are going to learn a little about organizing plots so that we can display more than one plot on the screen.  Multiple plots on one figure are called 'subplots'.  We have already imported matplotlib.pyplot so we are good to go.

We did not expressly create a figure in our python code but to get the subplots we want we will do it now.  Somewhere high up in our code we should add the following:

fig, axs = plt.subplots(2,2)

This syntax tells python that we want a single figure with a 2x2 array of plots.  Each plot is associated with the axs variable. The axs variable is made such that axs[0,0] is the first plot, axs[0,1] is the second, axs[1,0] is the third and axs[1,1] is the fourth.

Anywhere in our code where we had plt.hist or plt.plot we need to replace with axs[0,0].hist and axs[0,0].plot.

### Replace plt.hist or plt.plot with 
axs[0,0].hist(....
axs[0,0].plot(....

This tells python which of our 4 plots we want to populate ([0,0] is top left.)  Now add a

plt.show()

at the end of the code and test.  You should see a figure with 4 subplots. Only the top left plot should have anything in it if you used axs[0,0].

Now we would like to plot the trajectory of our retirement.  As a bonus we can use the plt.pause() feature in python and get a brief animation of our retirement fund making money.

First, lets make two empty lists and a "dummy" variable that we will use as an iterator:

money = []
dates = []
i=0

Be sure to make them outside of (and before) the while loop.

Then at the end of the while loop (and inside of it) add:

    ...
    ...
    money.append(retirement)
    dates.append(todays_date)
    i += 1

Then add the plot commands:

    ...
    ...
    money.append(retirement)
    dates.append(todays_date)
    i += 1
    axs[0,1].plot(money, dates)
    plt.pause(0.0001)

The number argument in the plt.pause function tells python how long to pause after running the plot update commands.  I picked a number to be negligible as possible.

Now if we run our python code we should see a plot flashing at us with newly and differently colored line plots constantly updating while the program is running.  This is fine, but it starts to eat up a lot of memory and run slowly.  We can speed it up and get a nearly identical effect with the following code:

    ...
    ...
    money.append(retirement)
    dates.append(todays_date)
    i += 1
    if i%25 == 0:
        axs[0,1].plot(money, dates)
        plt.pause(0.0001)

The "if" statement in words would be: "If i modulo 25 is 0 then..."  which is a mathematical way of saying "Cycle 25 times then do this (again)."

With that last bit added to our code you should see it run much faster.  You hopefully also noticed that the final amounts from two consecutive simulations were different.  In my example they were as different as 100,000 and 40,000 (or so).  That is in spite of putting the same amount of money into the account!  The only difference was the random interest rates that were chosen.  Next time we will put this simulation in a loop and start analyzing the different outcomes to see which ones are most probable.  See you then.

skip_nextRetirement Simulator #8: Multiverse Investing: Simulate 1000 random investors
  • Retirement Simulator Introduction and Overview

  • Retirement Simulator #1: March through time with datetime and put money under the mattress!

  • Retirement Simulator #2: Apply an interest rate and verify it!

  • Retirement Simulator #3: Use a Gaussian distribution of rates!

  • Retirement Simulator #3+: Verify our Gaussian distribution!

  • Retirement Simulator #4: Make a US market rates distribution

  • Retirement Simulator #5: Fit the Market Data with scipy curve_fit

  • Retirement Simulator #6: Sampling from our custom market distribution

  • Retirement Simulator #7: Organize multiple plots with subplots! and learn about plt.pause()
    (currently viewing)
  • Retirement Simulator #8: Multiverse Investing: Simulate 1000 random investors

  • Retirement Simulator #9: Track averages and final outcomes

  • Retirement Simulator #10: Test and Compare Different Retirement dates (And make it pretty!)