Most viewed recipes tagged "matplotlib"http://code.activestate.com/recipes/tags/matplotlib/views/2012-12-31T17:39:14-08:00ActiveState Code RecipesGenerating random numbers with arbitrary distribution (Python) 2008-11-05T07:52:39-08:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/576556-generating-random-numbers-with-arbitrary-distribut/ <p style="color: grey"> Python recipe 576556 by <a href="/recipes/users/4166965/">Kaushik Ghose</a> (<a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/random_number/">random_number</a>). </p> <p>This is a class that allows you to set up an arbitrary probability distribution function and generate random numbers that follow that arbitrary distribution.</p> Stacked graphs using matplotlib (Python) 2009-01-26T08:11:59-08:00Anand Patilhttp://code.activestate.com/recipes/users/4168675/http://code.activestate.com/recipes/576633-stacked-graphs-using-matplotlib/ <p style="color: grey"> Python recipe 576633 by <a href="/recipes/users/4168675/">Anand Patil</a> (<a href="/recipes/tags/broadband/">broadband</a>, <a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/mobile/">mobile</a>, <a href="/recipes/tags/stacked_graph/">stacked_graph</a>, <a href="/recipes/tags/stream_graph/">stream_graph</a>). </p> <p>Creates stacked graphs (sometimes known as stream graphs, apparently) as recommended by Byron and Wattenberg, <a href="http://www.leebyron.com/else/streamgraph/download.php?file=stackedgraphs_byron_wattenberg.pdf" rel="nofollow">http://www.leebyron.com/else/streamgraph/download.php?file=stackedgraphs_byron_wattenberg.pdf</a></p> Generating correlated random numbers (Python) 2008-09-21T21:21:52-07:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/576512-generating-correlated-random-numbers/ <p style="color: grey"> Python recipe 576512 by <a href="/recipes/users/4166965/">Kaushik Ghose</a> (<a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/random_number/">random_number</a>). </p> <p>From this great <a href="http://www.sitmo.com/doc/Generating_Correlated_Random_Numbers">tutorial</a></p> <p>For two corelated variables, the formula is much as one would get from intuition about the meaning of correlation with some twist due to normalizing the standard deviation: $X_3 = \alpha X_1 + \sqrt{1-\alpha^2} X_2$ Where $X_1$ and $X_2$ are two independent random variables, and $\alpha$ is the coefficient of correlation between $X_1$ and $X_3$.</p> <p>In a more general sense: <br /> Let $C$ be the correlation matrix desired. Let $X_1, X_2..., X_N$ be $N$ independent random variables arranged in a row matrix $R = [X_1, X_2,....,X_N]$. Then $Q = RU$ where $U^TU = C$ gives us $N$ random variables $Q = [Y_1, Y_2, ..., Y_N]$ with the required property.</p> Plotting maps with Polar Stereographic projection focused in a region with Basemap (Python) 2012-12-15T11:49:52-08:00PGhttp://code.activestate.com/recipes/users/4184598/http://code.activestate.com/recipes/578379-plotting-maps-with-polar-stereographic-projection-/ <p style="color: grey"> Python recipe 578379 by <a href="/recipes/users/4184598/">PG</a> (<a href="/recipes/tags/map/">map</a>, <a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/plotting/">plotting</a>, <a href="/recipes/tags/projections/">projections</a>, <a href="/recipes/tags/stereographic/">stereographic</a>). Revision 3. </p> <p>When plotting maps with either 'npstere' or 'spstere' projections with the Basemap toolkit for Matplotlib, the pole will always be placed in the center of the figure. With this function you can zoom the map to the area of interest by giving the latitude/longitudes coordinates of the bounds.</p> zero curve bootstrapping and forward curve generation (Python) 2012-09-10T08:07:56-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/578257-zero-curve-bootstrapping-and-forward-curve-generat/ <p style="color: grey"> Python recipe 578257 by <a href="/recipes/users/4166679/">alexander baker</a> (<a href="/recipes/tags/finance/">finance</a>, <a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/sympy/">sympy</a>). </p> <p>example of a bootstrapping and forward curve generation this can be used to build a set of curves for different currencies</p> Customizing polar plots in matplotlib (Python) 2008-09-06T05:31:02-07:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/576493-customizing-polar-plots-in-matplotlib/ <p style="color: grey"> Python recipe 576493 by <a href="/recipes/users/4166965/">Kaushik Ghose</a> (<a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/plots/">plots</a>, <a href="/recipes/tags/polar/">polar</a>). </p> <p>This snipped illustrates the use of thetagrids and rgrids to customize the polar plot grid</p> Benford's Law demo (Python) 2010-10-19T10:56:51-07:00Glenn Hutchingshttp://code.activestate.com/recipes/users/4175415/http://code.activestate.com/recipes/577431-benfords-law-demo/ <p style="color: grey"> Python recipe 577431 by <a href="/recipes/users/4175415/">Glenn Hutchings</a> (<a href="/recipes/tags/benford/">benford</a>, <a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/plotting/">plotting</a>). </p> <p>Here's a simple program to demonstrate <a href="http://en.wikipedia.org/wiki/Benford%27s_law">Benford's Law</a>, which also shows the simple power of <a href="http://matplotlib.sourceforge.net">matplotlib</a>. It reads from a bunch of files (or stdin, if none specified), extracts the leading digits of all number-like strings found, and plots the distribution in a window together with the expected result if Benford's law applies.</p> GAE Matplotlib Demo (Python) 2012-12-31T17:39:14-08:00Dima Tisnekhttp://code.activestate.com/recipes/users/4068698/http://code.activestate.com/recipes/578393-gae-matplotlib-demo/ <p style="color: grey"> Python recipe 578393 by <a href="/recipes/users/4068698/">Dima Tisnek</a> (<a href="/recipes/tags/app_engine/">app_engine</a>, <a href="/recipes/tags/gae/">gae</a>, <a href="/recipes/tags/google_app_engine/">google_app_engine</a>, <a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>Google App Engine python 2.7 runtime includes support for numpy and matplotlib since 13-Dec-2012, however, by default, matplotlib is not supported on the development server. This workaround let you run both in developer mode and deployed on google app engine:</p> Script that compares various Interest Rate term structure models. (Python) 2012-09-10T08:02:34-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/578256-script-that-compares-various-interest-rate-term-st/ <p style="color: grey"> Python recipe 578256 by <a href="/recipes/users/4166679/">alexander baker</a> (<a href="/recipes/tags/finance/">finance</a>, <a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>A common model used in the financial industry for modelling the short rate (think overnight rate, but actually an infinitesimally short amount of time) is the Vasicek model. # Although it is unlikely to perfectly fit the yield curve, it has some nice properties that make it a good model to work with. The dynamics of the Vasicek model are describe below.</p> <p>In this model, the parameters are constants, and the random motion is generated by the Q measure Brownian motion . An important property of the Vasicek model is that the interest rate is mean reverting to , and the tendency to revert is controlled by . Also, this process is a diffusion process, hence Markovian, which will lead to some nice closed form formulas. Finally, the future value of the interest rate is normally distributed with the distribution .</p> Polynomial explorer (Python) 2008-09-11T07:21:11-07:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/576501-polynomial-explorer/ <p style="color: grey"> Python recipe 576501 by <a href="/recipes/users/4166965/">Kaushik Ghose</a> (<a href="/recipes/tags/interactive_graphs/">interactive_graphs</a>, <a href="/recipes/tags/matplotlib/">matplotlib</a>). Revision 2. </p> <p>Polynomial explorer. You tell this module what order of polynomial you want and it will set up a figure with a graph of that polynomial plotted with x = -1 to +1. It will set up a second figure with a set of coefficient axes. You can click on the coeffiecient axes to set the coefficents. The graph will then update to reflect the values of the new coefficients.</p> <p>This code illustrates the use of mouse interaction using matplotlib</p> Speeding up computations using a lookup table part I (Python) 2011-07-02T15:14:33-07:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/577776-speeding-up-computations-using-a-lookup-table-part/ <p style="color: grey"> Python recipe 577776 by <a href="/recipes/users/4166965/">Kaushik Ghose</a> (<a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/optimization/">optimization</a>). </p> <p>I needed to use the cumulative normal distribution and normal probability density functions repeatedly for some data analysis. I found that I could speed things up drastically by using a lookup table and matplotlib's builtin interpolation function.</p> Simple example of embeding plots in wx and running them interactively (Python) 2010-09-21T17:30:30-07:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/577402-simple-example-of-embeding-plots-in-wx-and-running/ <p style="color: grey"> Python recipe 577402 by <a href="/recipes/users/4166965/">Kaushik Ghose</a> (<a href="/recipes/tags/interactive/">interactive</a>, <a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/wx/">wx</a>). </p> <p>Simple code to show how to incorporate a pylab plot into wx and then interact with it. This can form the basis of windows/apps that plot various variables that can be changing in the background.</p> Mandelbrot trajectories (Python) 2011-04-06T18:18:48-07:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/577642-mandelbrot-trajectories/ <p style="color: grey"> Python recipe 577642 by <a href="/recipes/users/4166965/">Kaushik Ghose</a> (<a href="/recipes/tags/graph/">graph</a>, <a href="/recipes/tags/interactive/">interactive</a>, <a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/plotting/">plotting</a>, <a href="/recipes/tags/widget/">widget</a>). </p> <p>An interactive graph to plot the trajectory of points on and off the mandelbrot set. Illustrates the use of sliders in matplotlib</p>