Contact Us!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

| Download
Views: 16
Visibility: Unlisted (only visible to those who know the link)
Image: ubuntu2204
Kernel: Python 3 (system-wide)

Making Contour Plots with Python

It is often useful to make a plot of an electric potential for a plane. The pylab library includes the contour function which draws contour (equipotential) lines and the contourf function which draws filled contours.

The following line loads the pylab library.

import pylab as pl

The meshgrid command is used to make two grids where X contains the x coordinate for each point and Y contains the y coordinate for each point. A third grid (called V in this example) is filled with a function of the x and y components.

xlist = pl.linspace(-2.0, 2.0, 5) ylist = pl.linspace(-2.0, 2.0, 5) X, Y = pl.meshgrid(xlist, ylist) print(X) print(Y) V= pl.sqrt(X**2 + Y**2) print(V)
[[-2. -1. 0. 1. 2.] [-2. -1. 0. 1. 2.] [-2. -1. 0. 1. 2.] [-2. -1. 0. 1. 2.] [-2. -1. 0. 1. 2.]] [[-2. -2. -2. -2. -2.] [-1. -1. -1. -1. -1.] [ 0. 0. 0. 0. 0.] [ 1. 1. 1. 1. 1.] [ 2. 2. 2. 2. 2.]] [[2.82842712 2.23606798 2. 2.23606798 2.82842712] [2.23606798 1.41421356 1. 1.41421356 2.23606798] [2. 1. 0. 1. 2. ] [2.23606798 1.41421356 1. 1.41421356 2.23606798] [2.82842712 2.23606798 2. 2.23606798 2.82842712]]

The 5x5 grid above is small enough to print, but more points are needed to make a smooth contour plot. The lines below make a 400x400 grid. The more points used, the smoother the contour plot will be, but the longer it will take. You must be careful that the function is not singular at any of the grid points. This can often be accomplished by adjusting the number of points used in the each dimension of the grid to avoid having a grid point where the function blows up.

xlist = pl.linspace(-2.0, 2.0, 400) ylist = pl.linspace(-2.0, 2.0, 400) X, Y = pl.meshgrid(xlist, ylist) V= pl.sqrt(X**2 + Y**2)

An example of using the contour command to draw contour lines (for example, equipotential lines) is given below. The name CP1 is used to refer to the contour plot. That name is used in the clabel command to add labels to the contour lines. The title command adds a title to a figure. The xlabel and ylabel commands add labels to the axes.

pl.figure() CP1 = pl.contour(X, Y, V) pl.clabel(CP1, fontsize=10) pl.title('Contour Plot') pl.xlabel('x (cm)') pl.ylabel('y (cm)') pl.show()
Image in a Jupyter notebook

An example of using the contourf command to make filled contours is shown below. The colorbar command (instead of clabel) makes a color bar which shows what values the colors in the diagram represent. Once again, a name (CP2) is used to refer to the contour plot.

pl.figure() CP2 = pl.contourf(X, Y, V) pl.colorbar(CP2) pl.title('Contour Plot') pl.xlabel('x (cm)') pl.ylabel('y (cm)') pl.show()
Image in a Jupyter notebook

A list of “levels” can be given as a fourth argument to the contour and contourf commands. For contour, contour lines will be drawn at each value in the list. For contourf, there will be filled regions between the values in the list.

It is also possible display both contour lines and filled contours in the same figure. In the example below, the colors of the contours and the labels are set to black (k) so that they stand out from the filled contours. The fmt option to the clabel command formats the numerical labels so that they are displayed with two numbers, one of which is after the decimal. Also, the fontsize option sets the size of the labels.

levels = [0.0, 0.5, 1, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0] CP3 = pl.contour(X, Y, V, levels, colors='k') pl.clabel(CP3, colors = 'k', fmt = '%2.1f', fontsize=14) CP4 = pl.contourf(X, Y, V, levels) pl.colorbar(CP4)
<matplotlib.colorbar.Colorbar at 0x7fa68f9a8d10>
Image in a Jupyter notebook

Additional Documentation