Simple plot
In [1]: x = linspace(-2*pi, 2*pi, 1000) In [2]: y = sinc(x) In [3]: plot(x,y) Out[3]: [] In [4]: grid(True) In [5]: legend(["$sinc(x)$"]) Out[5]: In [6]: xlabel("time($t$)") Out[6]: In [7]: ylabel("amplitude") Out[7]:
seen from Türkiye
seen from China
seen from United States

seen from Germany
seen from Belarus

seen from Malaysia
seen from Australia
seen from United States

seen from United States

seen from Germany
seen from Malaysia

seen from United States

seen from Germany
seen from South Africa

seen from Türkiye
seen from China
seen from United States
seen from United States

seen from Italy

seen from United States
Simple plot
In [1]: x = linspace(-2*pi, 2*pi, 1000) In [2]: y = sinc(x) In [3]: plot(x,y) Out[3]: [] In [4]: grid(True) In [5]: legend(["$sinc(x)$"]) Out[5]: In [6]: xlabel("time($t$)") Out[6]: In [7]: ylabel("amplitude") Out[7]:
pylab confusions
There are three pylabs that one may encounter in using Python. Two have been around for a while, and the third just showed up less than a month ago.
The “real” pylab is the procedural interface to matplotlib, i.e. a MATLAB-like command line interface. It imports matplotlib.pyplot and numpy into a single namespace. You can use it from ipython’s prompt by calling the magic function “%pylab”. It is no longer recommended by the matplotlib people. The recommended way is to import with abbreviated namespace names, and use the qualified functions. For example:
import matplotlib.pyplot as pltimport numpy as np x = np.linspace(0, 2, 100) plt.plot(x, x, label='linear')plt.plot(x, x**2, label='quadratic')plt.plot(x, x**3, label='cubic') plt.xlabel('x label')plt.ylabel('y label') plt.title("Simple Plot") plt.legend() plt.show()
Then there is the idea/proposal by Keir Mierle to improve on the pylab idea of a single package one might use to utilize Python for interactive analysis. This is written up in the SciPy wiki, but does not seem to have been updated since 2012. And finally, if you are like me, and have not been thinking too hard, and typed “pip install pylab” you get this new package from PyPI, first added on 2015-04-23. It does nothing but pull in several other Python packages, i.e. it serves as a metapackage. You can see the source is basically a dummy, with all the action in the requirements defined in setup.py. via Blogger http://bit.ly/1cDTXeX
MIT 6.00SC | Lecture 13 | Some Basic Probability and Plotting Data
Introduction
In the last lecture, we had a error in our code, because the Random walk code, did not output correct value of small samples as we had manually checked.
The problem was in the simWalks() method, which used wrong arguments, we had used this:-
distances.append(walk(f, homer, numTrials))
But we should have used:-
distances.append(walk(f, homer, numSteps))
So the complete corrected…
View On WordPress
Matplotlib trendline
Drawing a trendline of a scatter plot in matplotlib is very easy thanks to numpy's polyfit function.
Most of the code below is taken from http://docs.scipy.org/doc/numpy/reference/generated/numpy.polyfit.html.
Polyfit minimizes the squared error and can find a line or any higher-degree polynomial fit, as specified by the third argument.
Assuming we have a scatter of two vectors - x and y.
# plot the data itself
pylab.plot(x,y,'o')
# calc the trendline (it is simply a linear fitting)
z = numpy.polyfit(x, y, 1)
p = numpy.poly1d(z)
pylab.plot(x,p(x),"r--")
# the line equation:
print "y=%.6fx+(%.6f)"%(z[0],z[1])
The attached plot provides the average pagerank (http://en.wikipedia.org/wiki/PageRank) of several ASes (http://en.wikipedia.org/wiki/Autonomous_system_(Internet)) over time. You can see the trendline for each set of scatter plots.
You can easily use this method for any higher degree fitting. For example, a parabolic fit will be:
z = numpy.polyfit(x, y, 2)
And the rest of the code remains the same.
Matplotlib CDF plot
cdf_plot.py Download this file
I was searching for a matlab cdfplot in pylab but couldn't something which is as simple as the one matlab has. So I ported the one matlab has to numpy and pylib, and it turned out quite nice.
Attached is the code for the cdf_plot. It's very easy to use, simply create a numpy array and cdf_plot it. Also, it has a __main__ so you can run it and see it works.
For example, the following plots a simple CDF plot of an array:
python
$ import numpy
$ import cdf_plot
$ x=numpy.array([1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 6, 7, 8, 9])
$ cdf_plot.cdf_plot( x )
A cool feature that does not exist in matlab, is the ability to add markers in specific values. This is simply by adding a "values" key in the parameters list:
$ cdf_plot.cdf_plot( x, values="2:rx,4:bo" )
This adds a red x marker on x=2 and a blue o marker on x=4.
painting with random walks
this week I tried some ways to draw random walks on PyLab. random walks are aleatory "decisions" following a statistical distribution. in this way we can start drawing at point x, flip a coin and decide to go to another point x+i. that's it, over and over again.
for this series of paintings I used an uniform distribution to calculate a list of theta values to be used in Euler's formula. with theta we can calculate the real and imaginary parts of complex numbers, those are respectively x and y inside the cartesian plane. after having all those coordinates we do accumulative sum, yielding the coordinates for a uniform distributed random walk.
here are the series, with thousands of random walks superposed, using some color palettes.