Wednesday, January 1, 2020

Another simple plot

In this post we plot two (x;y) data sets: a smooth line curve and some data represented by red circles. In this plot, we label the x and y axes, create a legend, and draw lines to indicate where x and y are zero. The plot is shown below:



The code that creates this plot is shown below:

import numpy as np
import matplotlib.pyplot as plt

# read data from file
xdata, ydata = np.loadtxt('wavePulseData.txt', unpack=True)

# create x and y arrays for theory
x = np.linspace(-10., 10., 200)
y = np.sin(x) * np.exp(-(x/5.0)**2)

# create plot
plt.figure(1, figsize = (6,4) )
plt.plot(x, y, 'b-', label='theory')
plt.plot(xdata, ydata, 'ro', label="data")
plt.xlabel('x')
plt.ylabel('transverse displacement')
plt.legend(loc='upper right')
plt.axhline(color = 'gray', zorder=-1)
plt.axvline(color = 'gray', zorder=-1)

# save plot to file
plt.savefig('WavyPulse.pdf')

# display plot on screen
plt.show()

First, the script loads the NumPy and matplotlib modules, then reads data from a data file into two arrays, xdata and ydata, and then creates two more arrays, x and y. The first pair or arrays, xdata and ydata, contain the x-y data that are plotted as blue circles in Figure shown above; the arrays created in lines 8 and 9 contain the x-y data that are plotted as a green line.

The functions that create the plot begin on line 12. Let’s go through them one by one and see what they do. You will notice that keyword arguments (kwargs) are used in several cases.

figure() creates a blank figure window. If it has no arguments, it creates a window that is 8 inches wide and 6 inches high by default, although the size that appears on your computer depends on your
screen’s resolution. For most computers, it will be much smaller. You can create a window whose size differs from the default using the optional keyword argument figsize, as we have done here. If
you use figsize, set it equal to a 2-element tuple where the elements, expressed in inches, are the width and height, respectively, of the plot. Multiple calls to figure() opens multiple windows: figure(1) opens up one window for plotting, figure(2) another, and figure(3) yet another.

plot(x, y, optional arguments) graphs the x-y data in the arrays x and y. The third argument is a format string that specifies the color and the type of line or symbol that is used to plot the data. The string 'bo' specifies a blue (b) circle (o). The string 'g-' specifies a green (g) solid line (-). The keyword argument label is set equal to a string that labels the data if the legend function is called subsequently.

xlabel(string) takes a string argument that specifies the label for the graph’s x-axis.

ylabel(string) takes a string argument that specifies the label for the graph’s y-axis.

legend() makes a legend for the data plotted. Each x-y data set is labeled using the string that was supplied by the label keyword in the plot function that graphed the data set. The loc keyword argument specifies the location of the legend. The title keyword can be used to give the legend a title.

axhline() draws a horizontal line across the width of the plot at y=0. Writing axhline(y=a) draws a horizontal line at y=a, where y=a can be any numerical value. The optional keyword argument color is a string that specifies the color of the line. The default color is black. The optional keyword argument zorder is an integer that specifies which plotting elements are in front of or behind others. By default, new plotting elements appear on top of previously plotted elements and have a value of zorder=0. By specifying zorder=-1, the horizontal line is plotted behind all existing plot elements that
have not be assigned an explicit zorder less than 1. The keyword zorder can also be used as an argument for the plot function to specify the order of lines and symbols. Normally, for example,
symbols are placed on top of lines that pass through them.

axvline() draws a vertical line from the top to the bottom of the plot at x=0. See axhline() for an explanation of the arguments.

savefig(string) saves the figure to a file with a name specified by the string argument. The string argument can also contain path information if you want to save the file someplace other than the
default directory. Here we save the figure to a subdirectory named figures of the default directory. The extension of the filename determines the format of the figure file. The following formats are
supported: png, pdf, ps, eps, and svg.

show() displays the plot on the computer screen. No screen output is produced before this function is called.

To plot the solid blue line, the code uses the 'b-' format specifier in the plot function call. It is important to understand that matplotlib draws straight lines between data points. Therefore, the curve
will appear smooth only if the data in the NumPy arrays are sufficiently dense. If the space between data points is too large, the straight lines the plot function draws between data points will be visible. For plotting a typical function, something on the order of 100–200 data points usually produces a smooth curve, depending on just how curvy the function is. On the other hand, only two points are required to draw a smooth straight line.

Share:

0 comments:

Post a Comment