Lab 3 Plotting With matplotlib and Mayavi

Lab 3

Plotting With matplotlib and Mayavi

Lab Objective: Introduce some of the basic plotting functions available in matplotlib and Mayavi.

matplotlib

matplotlib is one of Python's many plotting libraries. It has an active development community and was designed to resemble MATLAB. Because of its high quality and its helpful user interface toolkits, matplotlib serves as our primary plotting library for 2D graphs in this text.

This section seeks to introduce many of the basic plotting functions we will access using matplotlib, but matplotlib has many more. For further reference and information, visit .

Simple Plots

To get started, we import pyplot from matplotlib.

from matplotlib import pyplot as plt

The basic line plotting function in matplotlib is plot(). It takes a set of data points and plots the line that is formed between those points. These plots are pieced together using a state machine environment, which means that we can run several different functions that will display or modify the plot we are creating.

To plot and display our function we typically use an outline similar to the following:

# Definition of x and y coordinates (typically NumPy arrays or lists). plt.plot(x,y) plt.show()

Note that x and y must be the same length in order for plot(x,y) to work. When we define our x and y coordinates, we often use NumPy's linspace(,

, num=50) function since it returns evenly spaced values over a given interval.

27

28

Lab 3. Plotting

Figure 3.1: A simple plot of ex.

These values are returned in an ndarray. So, after importing numpy and pyplot, we can generate a basic plot of the function ex with the following lines of code:

# Increase number of samples from default 50 to 501. x = np.linspace(-2, 3, 501) y = np.exp(x) plt.plot(x, y) plt.show()

This should display a plot similar to the one shown in Figure 3.1.

Multiple Plots

We can also use our interface to display multiple plots at once. The following code will plot lines with random values at integers from 1 to 10. Note that NumPy's random.rand function creates an array of a specified shape and fills it with random samples from a uniform distribution over the interval [0,1). The given dimensions of the array should be positive and, if no argument is given, a single float is returned.

x = np.linspace(1, 10, 10) y = np.random.rand(10, 10) plt.plot(x, y[0], x, y[1], x, y[2], x, y[3], x, y[4], x, y[5], x, y[6], x, y[7],

x, y[8], x, y[9]) plt.show()

29

Figure 3.2: A plot of 10 lines with randomly generated y values. We just used the plot function to plot several different lines at once. Alternatively, we can plot each (x, y[i]) pair separately and overlay the plots to produce the same result. We use a loop to efficiently overlay create each plot.

x = np.linspace(1, 10, 10) y = np.random.rand(10, 10) for n in y:

plt.plot(x, n) plt.show()

A plot that was generated by this code will be similar to the one shown in Figure 3.2.

Note that a suitable domain and range for your plot is automatically chosen unless you specify otherwise.

Problem 1. Plot the function sin(x) from 0 to 2 with a red dashed line. Then plot the function cos(x) on the same domain with a blue dotted line. Implement both with a single call to the plot() function. Information on how to do this can be found in Appendix C.

30

Lab 3. Plotting

Problem 2. Plot the curve 1/(x - 1) from -2 to 6 with a magenta dashed line. Force the plot to only show y values that are between -6 and 6. Make your line width 5. Once again, more information on how to do this can be found in Appendix C. By default, plot() will try to make the graph connected. Correct this so that the graph appears to be discontinuous at x = 1 (as it should be) by modifying the input values.

Your plot should look like the figure below.

Problem

3.

Plot

the

curve

sin(x)

1 x+1

from

0

to 10.

Use blue shading under the curve when it is positive and red when it is

negative (you may want to consider using the fill_between command. Make

the line dotted. Label the x-axis "x-axis", the y-axis "y-axis",and the plot

"My Plot". Enable the grid lines.

Finally, use the scatter command to include a scatter plot of half of the value of the function at each of its maxima and minima in the range. Display

these points as upward-pointing triangles. Don't forget to make sure the x

limits of the plot are still 0 and 10.

Helpful Hint: Since you are working with arrays of discrete values, you will want to find the index values where your x and y values are closest to the actual maxima and minima. As you work, consider the following:

31 ? How would you manually find maxima and minima of a function? ? How could you do something similar with your x and y arrays? Your plot should look like the figure below.

pcolormesh

Pcolormesh is a function in pyplot which produces a pseudocolor plot of a 2D array. Think of it as a 3D plot that assigns color rather than height to the result C of a function in x and y. Since our domain is now two-dimensional, instead of onedimensional as before, we use NumPy's meshgrid function. This function takes two one-dimensional arrays that represent our input values for x and y respectively, and returns a grid of (x, y) coordinates. X represents the x-coordinates of this grid and Y represents the y-coordinates. We then evaluate our function at these points and return the result in a two-dimensional array C, which the pcolormesh function uses to assign colors to the points in our xy domain.

We now use the pcolormesh function to represent the surface z = sin(x) sin(y):

n = 401 x = np.linspace(-6, 6, n) y = np.linspace(-6, 6, n) # Returns a coordinate matrix given coordinate vectors. X, Y = np.meshgrid(x, y) C = np.sin(X) * np.sin(Y) plt.pcolormesh(X, Y, C) plt.show()

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download