{ "cells": [ { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "
First, let's make some data to plot.
" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "starttime = 0\n", "stoptime = 15\n", "nsteps = 500\n", "S = 45400\n", "I = 2100\n", "R = 2500\n", "deltat = float((stoptime-starttime)/nsteps)\n", "S_sol = [S]\n", "I_sol = [I]\n", "R_sol = [R]\n", "tvals = [starttime]\n", "for step in range(nsteps):\n", " S_prime = -.00001*S_sol[-1]*I_sol[-1]\n", " I_prime = .00001*S_sol[-1]*I_sol[-1] - I_sol[-1]/14\n", " R_prime = I_sol[-1]/14\n", " S_sol.append(S_sol[-1] + deltat*S_prime)\n", " I_sol.append(I_sol[-1] + deltat*I_prime)\n", " R_sol.append(R_sol[-1] + deltat*R_prime)\n", " tvals.append(tvals[-1] + deltat)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "As before, we can make a lovely labeled plot of some of the data.
" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list_plot(zip(tvals,S_sol),title='Time in days vs. Number of Susceptibles')" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "But what if we want to see all the data at once? Here's what we have to do: Make a separate plot for each data set, name each of them, and then tell Sage to show them all together.
" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Splot = list_plot(zip(tvals,S_sol), color='blue', title='Time in days vs. Stuff')\n", "Iplot = list_plot(zip(tvals,I_sol), color='red')\n", "Rplot = list_plot(zip(tvals,R_sol), color='green')\n", "SIRplot = Splot + Iplot + Rplot\n", "SIRplot.show()" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "You might want a slightly more descriptive title for the plot.
\n", "\n", "
Now, this does allow us to do something pretty fancy---we can make more than one approximation and compare. The code for each approximation has to give different names to the lists of data, so that we don't overwrite the data we generated earlier. Here's an example (and notice that if you want to do a third approximation, you'll have to change about 20 variable names).
\n", "" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "starttime = 0\n", "stoptime = 10\n", "nsteps = 10\n", "S = 45400\n", "I = 2100\n", "R = 2500\n", "deltat = float((stoptime-starttime)/nsteps)\n", "S_sol2 = [S]\n", "I_sol2 = [I]\n", "R_sol2 = [R]\n", "tvals2 = [starttime]\n", "for step in range(nsteps):\n", " S_prime = -.00001*S_sol2[-1]*I_sol2[-1]\n", " I_prime = .00001*S_sol2[-1]*I_sol2[-1] - I_sol2[-1]/14\n", " R_prime = I_sol2[-1]/14\n", " S_sol2.append(S_sol2[-1] + deltat*S_prime)\n", " I_sol2.append(I_sol2[-1] + deltat*I_prime)\n", " R_sol2.append(R_sol2[-1] + deltat*R_prime)\n", " tvals2.append(tvals2[-1] + deltat)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "
We could show all of this data at once, or we can mix and match---two mix-and-match examples are given here, and you should make some examples of your own as well.
" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Splot2 = list_plot(zip(tvals2,S_sol2), color='purple', title='Time in days vs. Stuff')\n", "Iplot2 = list_plot(zip(tvals2,I_sol2), color='orange')\n", "Rplot2 = list_plot(zip(tvals2,R_sol2), color='yellow')\n", "SIRplot2 = Splot2 + Iplot2 + Rplot2\n", "SIRplot2.show()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "SirPlottyA = Splot + Splot2\n", "SirPlottyA.show()" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "SirPlottyB = Iplot + Rplot2\n", "SirPlottyB.show()" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "When you run out of colors (or if you need to print something out on a greyscale printer), you'll have to resort to dashed lines. In order to get this to work, the plotjoined=True option must be included.
" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "PlottoS = list_plot(zip(tvals,S_sol),plotjoined=True, color='green', linestyle='-.', thickness=4)\n", "PlottoI = list_plot(zip(tvals,I_sol),plotjoined=True, color='orange', linestyle='--', thickness=2)\n", "PlottoR = list_plot(zip(tvals,R_sol),plotjoined=True, color='purple', linestyle=':', thickness=3)\n", "SIRPlotto = PlottoS + PlottoI + PlottoR\n", "SIRPlotto.show()" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "Possibly you'll want to show a different portion of the graph than Sage chooses to show you. Here's how to do it...
" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "SIRPlotto.show(xmin=4, xmax=9, ymin=10000, ymax=25000)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "SIRPlotto.show(xmin=-10, xmax=20, ymin=-10000, ymax=100000)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "Go back to an instance of code with a reasonable amount of data, say the S_sol2, I_sol2, etc. code. Below we define a command that will format lists of data into a table.
" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def tab_list(y, headers = None):\n", " '''\n", " Converts a list into an html table with borders.\n", " '''\n", " s = '' + str(q) + ' | '\n", " for x in y:\n", " s = s + '
---|
' + str(q) + ' | '\n", " s = s + '
Now let's actually use the command. (Make sure that this is the most recent data you've generated, or else put in the correct number for nsteps.)
" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "table_range = range(0,floor(nsteps/2)) + range(len(S_sol2)-floor(nsteps/2),len(S_sol2))\n", "html(tab_list([[i,tvals2[i],S_sol2[i],I_sol2[i],R_sol2[i]] for i in table_range], headers = ['step','t','S','I','R']))" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "Nice, eh?
\n", "\n", "
\n", "
(Note in case you are comparing with the Mathematica version of this notebook: Sage doesn't have an easy way to do animations; among other things, it requires extra software be installed.)
" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [] } ], "metadata": { "kernelspec": { "display_name": "SageMath 8.0", "language": "", "name": "sagemath" }, "language": "python", "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.13" } }, "nbformat": 4, "nbformat_minor": 2 }