In most cases, the most simple figures are the best. In what follows, we are going to cover some simple plotting methods that include the most common plot types. The module matplotlib
offers several different way to create the same figure, we'll try to do our best in introducing only the simplest concepts. We are only going to go through some of the keyword arguments of matplotlib
's functions, though. Therefore, look for more parameters in the matplotlib
example galleries, or in the docstrings of plot
, hist
, pcolor
, contour
and contourf
functions.
# import libraries
%pylab inline
In many cases, we would like to plot some data or an analytic function. If we want to plot a function, then first we have to create some points that we evaulate later with the function. In the next example, we are going to evaulate the function $\sin(x)$ on the $[-\pi,\pi]$ interval in 25 points.
x=linspace(-pi,pi,25);
y=sin(x);
The plotting is done by the plot
function.
plot(x,y)
Let us examine some keyword arguments, that alter the output of the plot
command. The keyword color
changes the colour of the plotted data. This figure contains all the colours used in matplotlib
. Let us change the color of the curve in the next figure.
plot(x,sin(x),color='red')
With the help of the keyword linewidth
, we can change the width of the plotted line.
plot(x,sin(x),linewidth=3)
The keyword linestyle
sets the style of the plotted line. Its value can be either a string with a meaning, like 'dashed'
, or some symbols.
plot(x,sin(x),linestyle='dashed') # dashed line
If we give it an empty string, and we use the keyword marker
, then the figure is only going to displey points. This way, we can also plot unordered data.
plot(x,sin(x),linestyle='',marker='o')
By using both keywords at once, the markers are going to be connected by a line in the given style.
plot(x,sin(x),linestyle='dashed',marker='s')
Of course, we can set many more keyword arguments. What do they do?
props=dict(color='green', linestyle='dashed', marker='o', markerfacecolor='blue',
markeredgecolor='red',markeredgewidth=2, markersize=12 )
plot(x, sin(x), **props)
We can set the axis limits with the xlim()
and ylim()
commands, which have to be called after the plot
function.
plot(x,sin(x))
xlim(-pi,pi);
The plot
function has many more keyword arguments. In its docstring, these are very well documented.
?plot
If we give two or more plot
commands after each other in one cell, then two or more functions are going to be plotted in the figure.
plot(x,sin(x))
plot(x,cos(x))
A special case is when one plot
command only contains a single point. With this method, we can mark interesing points on the function graph.
plot(x,sin(x))
plot(1,sin(1),'o')
If we plot more functions, then the label
keyword and the legend()
function help in matching the functions to the plotting styles.
plot(x,sin(x),label='sin(x)',color='red',linestyle='-',linewidth=3)
plot(x,cos(x),label='cos(x)',color='blue',linestyle='--',linewidth=3)
legend()
The functions xlabel()
and ylabel()
create axis labels.
plot(x,sin(x))
xlabel(r'ido',fontsize=20)
ylabel('kiteres',fontsize=20)
The functions xticks()
and yticks()
format the axis ticks. Have a look at their docstrings!
plot(x,sin(x))
xticks([-pi,-pi/2,0,pi/2,pi],[r'$-\pi$',r'$-\pi/2$',r'$0$',r'$\pi/2$',r'$\pi$'],fontsize=20);
yticks(linspace(-1,1,9));
# what has changed?
Finally, let us look at an example, that uses almost all of the above tricks, and that creates a publication-quality figure.
plot(x,sin(x),label='sin(x)',color='red',linestyle='-',linewidth=3)
plot(x,cos(x),label='cos(x)',color='blue',linestyle='--',linewidth=3)
xticks([-pi,-pi/2,0,pi/2,pi],[r'$-\pi$',r'$-\pi/2$',r'$0$',r'$\pi/2$',r'$\pi$'],fontsize=20);
yticks(linspace(-1,1,3),fontsize=20);
xlabel('Time',fontsize=20)
ylabel('Desplacement',fontsize=20)
legend(loc='upper left',fontsize=20)
xlim(-pi,pi)
grid(True) # what does this function do?
Many other plotting functions inherit the same keyword arguments from the original plot
function. For example, we can use the same styling arguments for the 3D plotting as well. Labelling and formatting the axes is also general in matplotlib
, not just for the plot()
command.
We can even plot measurement data with the plot()
function. But measurement data is often loaded with error. Let us see a simple example! In the following, we are going to use a real sunspot measurement datafile for the sample commands. The datafile can be founs in the data
subdirectory as SN_m_tot_V2.0.txt
. First, we have to load the content of the file into Python
. We can do that by using the loadtxt
function of the numpy
module. This function loads simply structured data into an array
.
dat=loadtxt('data/SN_m_tot_V2.0.txt'); # loading sunspot datafile
The above command loaded into the dat
array the data from the file. The third column of the file contains the time of the observation in years, the fourth contains the number of sunspots observed. Let's plot the last hunfred points from the datafile.
plot(dat[-100:,2],dat[-100:,3],linestyle='',marker='o')
The fifth column contains the statistical errors of the measurements. If we want to see the datapoints and their error on the same figure, we can do it by using the errorbar()
function.
errorbar(dat[-100:,2],dat[-100:,3],dat[-100:,4],linestyle='',marker='o')
The errorbar()
function expects three columns, the first two for the position of the datapoints, and the third for the error. Similary to plot()
, we can control its appearance through keyword arguments. The next example illustrates some of them:
props=dict(linestyle='',marker='o',ecolor='green',capsize=4,capthick=2)
errorbar(dat[-100:,2],dat[-100:,3],dat[-100:,4],**props)
We can get further information on the options from the docstring:
?errorbar
We can put the two types of plots onto one figure as well. The enxt example shows the result of the simultaneous usage of the plot()
and errorbar()
functions.
errorbar(dat[-300:,2],dat[-300:,3],dat[-300:,4],linestyle='',marker='o')
plot(dat[-300:,2],70*cos(2*pi/11*dat[-300:,2]-pi*0.25)+70,color='red',linewidth=3)
Histograms are important tools for statistical analysis. Last time we already saw a simple example for the creation of a histogram with the function hist()
. Let's see further examples for keyword arguments of the hist
function! First, let us generate some random numnbers.
measurement1=randn(10000);
measurement2=random.normal(2,0.5,10000);
As we;ve already seen, the most simple way of creating a histogram is:
hist(measurement1)
If we want to increase the histogram's resolution, we can achive it through the bins
argument. If bins
receives a number as an input, then the number of 'categories' will be equal to bins
on the histogram.
hist(measurement1,bins=100);
But we can give the edges of the bins arbitrarily with the help of a list!
hist(measurement1,bins=[-2,0,1,2,3]);
If we would like to use a continuous line instead of bars, then the keyword argument histtype
can be used.
hist(measurement1,histtype='step');
More histograms can be put onto one figure.
hist(measurement1,bins=100,linewidth=0);
hist(measurement2,bins=100,linewidth=0);
If two distributions would cover each other by too much, the keyword alpha
helps in making them transparent to a certain degree.
hist(measurement1,bins=100,linewidth=0,alpha=0.5);
hist(measurement2,bins=100,linewidth=0,alpha=0.5);
The display of bivariate functions is very similar to that of univariate functions, and begins with sampling or data collection. The already known linspace()
function combined with the meshgrid()
function can sample a 2D parameter space:
x2,y2 = meshgrid(linspace(-4,4,50),linspace(-3,3,50)) # sampling points
z2 = sin(x2) ** 10 + cos(10 + y2 * x2) * cos(x2) # function evaluation
The above code thus generated three arrays. The first two contain the $x$ and $y$ coordinates of sampling points in a ford of two 2D arrays. The variable z2
contains the value of the function at those points. We only have to plot now! This is done by the pcolor()
function.
pcolor(x2,y2,z2)
The meaning of the colour code can be visualized with the help of the colorbar()
function.
pcolor(x2,y2,z2)
colorbar()
There are many colour scales. Each has its own advantages and disadvantages. The colour scales of matplotlib
can be evoked by the keyword cmap
using their names.
pcolor(x2,y2,z2,cmap='magma')
colorbar()
Bivariate functions can be visualized using contour lines. A countour line consists of points, where the given bivariate function has the same value. The function contour()
generates these contour lines for us:
contour(x2,y2,z2)
If we are only interested in given contour values, we can address them by using the levels
keyword.
contour(x2,y2,z2,levels=[-0.5,0,0.5])
Uding the clabel()
function, the values are displayed on the lines.
cs=contour(x2,y2,z2,levels=[-0.5,0,0.5])
clabel(cs)
The function contourf
unites the good properties of the pcolor()
and contour()
functions. Its coloured figures fit eh shape of the visualized function better.
contourf(x2,y2,z2)
The keyword levels
can increase the resolution.
contourf(x2,y2,z2,levels=linspace(-1.5,1.5,100))
colorbar()
It may occur, that the sampling is not uniform. Then, we can visualize datapoints using triangulation. Let us create some random points on the plane!
tx,ty = [8*rand(50*50)-4,6*rand(50*50)-3] # sampling points
Let us evaulate the function in these random points!
tz = sin(tx) ** 10 + cos(10 + ty * tx) * cos(tx) # function evaluation
If the data points are unordered, then we have to use the functions tripcolor()
, tricontour()
and tricontourf()
instead of the functions pcolor()
, contour()
and contourf()
in the same manner as before.
tripcolor(tx,ty,tz)
tricontourf(tx,ty,tz,linspace(-1.5,1.5,100))
In physics, we use vector fields quite often. Let us think of meteorology or electrodynamics. Let us create the gradient of the previously plotted function. The gradient vector in the plane is given by the following:
u=-y2*sin(x2*y2 + 10)*cos(x2) + 10*sin(x2)**9*cos(x2) - sin(x2)*cos(x2*y2 + 10)
v=-x2*sin(x2*y2 + 10)*cos(x2)
The funciton quiver()
assigns a vector given by the arrays u
and v
to every point defined on the plane by the arrays x2
and y2
. Thus, in every point (x2[i],y2[i])
, there is a vector (u[i],v[i])
!
quiver(x2,y2,u,v)
Let us draw only every third vector in red and a bit thicker.
quiver(x2[::3, ::3], y2[::3, ::3], u[::3, ::3], v[::3, ::3],
color='red',width=0.005)
For further details, see the documentation.
?quiver
An alternative to quiver()
is the plotting of the field lines, or streamplot()
.
streamplot(x2[::3, ::3], y2[::3, ::3], u[::3, ::3], v[::3, ::3])
For further details, see the documentation.
?streamplot
streamplot()
and quiver()
may be used together with pcolor()
and contour-making routines.
pcolor(x2,y2,z2)
colorbar()
quiver(x2[::2,::2],y2[::2,::2],u[::2,::2],v[::2,::2],color='white',width=0.005)
contourf(x2,y2,z2,levels=linspace(-1.5,1.5,100))
colorbar()
streamplot(x2[::3, ::3], y2[::3, ::3], u[::3, ::3], v[::3, ::3],color='white')
When finishing a figure, we may be in need of an annotation here and there, or we'd like to highlight certain parts of the figure. We can do that by using the text()
and the annotation()
functions. The function text()
has three input arguments, the first two being coordinates, the thrid a string. Let us see an example!
plot(x,y);
text(1,0,'This is a label.')
text(-3,0,'This is another label.',fontsize=13,color='red')
If we want to emphasize a certain part, then the best option is the function annotate()
. This puts a pointing arrow and an explanation text onto the figure. The first input argument is a string, that is the explanation, and apart from that, we can control the behaviour of the function using keyword arguments. The keyword xy
is the position of the point we want to highlight. The keyword xytext
contains the explanation itself, and arrowprops
is a dictionary that defined the properties of the arrow.
plot(x,y)
annotate('local minimum', xy=(-pi/2, -1), xytext=(-pi, 0.5),arrowprops=dict(color='red',width=3),fontsize=20)
?annotate
Finally, let us see some examples for figure arrays. The function subplot()
can create an array or grid of figures. If we put more subplot()
commands into one code cell, then the plotting routines after every subplot()
are going to plot into new figures. The meaning of the notation subplot(n,m,i)
is the following: what comes next, put it into the i
th subplot of an n
$\times$m
figure grid. i
cannot be greater than n*m
!
figsize(12,4) # longer horizontal figure dimensions
subplot(1,2,1)
plot(x,y)
subplot(1,2,2)
pcolor(x2,y2,z2)
figsize(6,8) # longer vertical figure dimensions
subplot(2,1,1)
plot(x,y)
subplot(2,1,2)
pcolor(x2,y2,z2)
We can save our figures to image files using the savefig()
command.
figsize(6,4) # setting the usual figure dimensions
plot(x,y)
savefig('my_figure.png')