Loading [MathJax]/extensions/Safe.js

Tuesday, June 27, 2017

Python Plotting Tutorial -- Part 2

In this part we'll cover a few miscellaneous things...



Comments
Python code is allowed to contain comments. These are lines which python ignores, so you can write anything you like. The comment symbol is #. Whenever you see #, the rest of the line is a comment. Example:


Numpy Arrays vs. Lists
When you want to store a set of numbers which go together, you need to use some kind of array. Python has a built in data type called lists. We prefer to use numpy arrays. This example should demonstrate the difference:
 
The first (a) is a list. The second (b) is a numpy array. As you can see, multiplying the array by two doubles each value. Not so for the list.


Creating Arrays With More Points, Smooth Curve Plots
In Part 1 we created the x variable by hand. To get a smoother graph with more points, we can use the command np.linspace(min, max, num_points). This creates an array with num_points evenly spaced points between min and max. For example:
 
With this method, you can make a smoother plot of x squared:
 
When plotting a smooth curve with a solid line (like in the next section), it is usually a good idea to use 1000 to 10000 points.



Plots With Multiple Curves
It's easy to add more curves to your plot:
 
 
The zorder argument controls which one goes on top. Here, red shows on top of blue.


Multiple Figures
To create multiple figures within the same script, use the plt.figure() command:
 
 


 That's it for now.

No comments:

Post a Comment