Wednesday, December 25, 2019

Basic Matplotlib Plots

A simple plot

Before you plot on matplotlib, you must have a plot () function within the matplotlib.pyplot sub package. This is to give you the basic plot with x-axis and y-axis variables. Alternatively, you can also use format parameters to represent the line style you are using. To determine the format parameters and options used, the following commands apply:

$ ipython -pylab
In [1] : help(plot)

In the example above, you are creating two unique lines. The first one, which will act as the default line, is the solid line style, while the second one will have a dashed line. Study the code snippet below. We will use it to describe the procedure on how to create a simple plot.

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 20)
plt.plot(x, .5 + x)
plt.plot(x, 1 + 2 * x, '--')
plt.show()


Use the following procedure to plot the lines described above:


Step 1:

Determine the x coordinates using linspace () , a NumPy function. The x coordinates start at 0 and end at 20, hence you should have the following function:

x = np.linspace(0, 20)

Step 2:

Plot the lines on your axis in the following order:

plt.plot(x, .5 + x)
plt.plot(x, 1 + 2 * x, '--')

Step 3:

At this point, you have two options. You can save the plot or view it on a screen. The savefig() function is used to save the file. If you have to view it, the show () function is used. To view the function on the screen, use the following plotting function:

plt.show()

The plot obtained is shown below:

Share:

0 comments:

Post a Comment