Our Next Script¶
Plotting Functions¶
Now, we will plot some actual functions in MatPlotLib. First, we will start with a sine wave. To do this, create a new script named Sine_plot.py and put in the normal import statements:
Next, we need to generate the data. To do this, we will use a python built-in function, range, that creates a list of ascending integers:
Now that we have our x-values, we need to calculate our y-values in a pythonic way:
This is called list comprehension. It is essentially a for-loop, where it loops through all the values in a list. Then, we call the NumPy sine function on each of those values and store them into another list, called y_values. Last, we want to plot the values we have, and show the plot we generated:
Your script should look like this at the end:
After we run the new script from the terminal:
We should get a plot that looks like this:

However, that looks rather jagged. To get an idea of what is happening, we can add markers to the points that are plotted by adding an option to the ax.plot() command:

What can we do to improve the smoothness of the plot? The way to do this is to increase the number of points plotted. Right now, we are just plotting the integers from 0 to 9, but we want to plot some points in between. We could manually add points to a list for the x-values, but there is a better way: using NumPy's linspace function. It generates a NumPy array (which is easier to manipulate with math functions than a list), with three imputs to the function. They are, in order, the starting number, the ending number, and the number of points to make up the range. Modify the x_values assignment to use the NumPy linspace function like so:
y_values assignment because the x_values is now a NumPy array:
marker='*' option. Now, we run the script from the terminal:
And get a plot that looks like this:

That is much smoother now!
Styling Plots¶
Now that we can generate plots and graph data on them, we will talk about how to make them look pretty. We will go into more depth later in this workshop, but here are some high-level options we can use.
First, we can talk about global style sheets. These are overall packaged styles that affect the entire script. To change all plots in the script to look like figures made with R's ggPlot package use the command:
Doing this in our Sine_plot.py script yields a plot that looks like this:

You can see all available style sheets by running this command:
Formatting Plots¶
Now, there are a few other MatPlotLib functions we may want to use to make our plot more readable and useful, such as:
- Aspect Ratio
- Title
- Axis Labels
- Axis Limits
- Legend
These are all set and used after the figure you are trying to modify has been created, but before you show it.
The aspect ratio controls how the x- and y-axes relate to each other within the plot. For this case, we will want that to be one, but it could be the default automatic setting, or something completely different. To explicitly set the aspect ratio of a plot, use this command (which sets the aspect ratio to one):
Next, the title is text that appears above the axes, which may not be the entire figure, as we will discover when we start putting multiple axes in the same figure. To set this for the axes in question, run the command:
r in front of the string says to Python to ignore any escape sequences (started by the \\) so it can be passed to the LaTeX parser instead of Python trying to interpret it itself. It is generally good practice to include the r before the string if you are doing any kind of math in the string.
The axis labels are labels that are put next to each of the x- and y-axes:
Again, we have the dollar signs in the labels, to convert those symbols to their math counterparts.The axis limits set the extent for each of the axes, or in other words, what the x-y bounds of the plot are:
Lastly, the legend can differentiate between the different lines on a plot, especially when there are many. To use the legend, there are two parts: the line label, and the legend invocation. The line label is added to the ax.plot() function, and the legend invocation is added after all the plotting is done:
Putting these all together, your script should look like this:
Notice that now the plot is no longer square, as the aspect ratio and the plot limits have dictated that the plot is longer in the x-direction than it is in the y-direction.
In the next section we will talk about some more advanced plotting topics, such as line styling and multiple lines: Next Section