1D Plotting Python

From ComputingForScientists

Jump to: navigation, search

Contents

  1. References
  2. Notes
    1. Importing Matplotlib
    2. Plotting array elements
    3. Annotation
    4. Line Color
    5. Marker Style
    6. Line Style
    7. Style Combinations
    8. Multiple lines
    9. Axis Numbering
    10. Axis Limits
  3. Problems
    1. Scalar Time Series Plots I
    2. Scalar Time Series Plots II

1. References

2. Notes

2.1. Importing Matplotlib

The start of any Python program where Matplotlib plot commands are used must start with

# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt

where the only optional part is plt, which declares the namespace of all Matplotlib functions. All calls to Matplotlib plotting functions must be prefixed by plt..

2.2. Plotting array elements

In Python, line plots are created using the plot function prefixed by the namespace declared in the import command (plt).

A = [1.0,4.0,16.0,32.0];
plt.plot(A)
plt.show()

Note that no plot will be shown unless show() is called.

From raw.githubusercontent.com on May 18 2019 20:23:27.

The plot(A) command caused a plot of the values of A to be shown with the values in A shown on the y-axis. The x-axis values were assumed to be [1.,2.,3.,4.]. The above commands are equivalent to

A = [1.0,4.0,16.0,32.0];
x = [1.,2.,3.,4.]
plt.plot(x,A)
plt.show()
From raw.githubusercontent.com on May 18 2019 20:23:27.

To change the x-axis values, modify the first array that is passed to the plot function.

A = [1.0,4.0,16.0,32.0];
x = [10.,20.,30.,40.]
plt.plot(x,A)
plt.show()
From raw.githubusercontent.com on May 18 2019 20:23:27.

2.3. Annotation

To add a grid, use grid() after the plot command. To add a axis labels and a title, use xlabel, ylabel, and title

A = [1.0,4.0,16.0,32.0];
x = [1.,2.,3.,4.];
plt.plot(x,A)
plt.grid()
plt.xlabel('Time [seconds]');
plt.ylabel('Height [meters]');
plt.title('Experiment 1 results');
plt.show()
From raw.githubusercontent.com on May 18 2019 20:23:27.

2.4. Line Color

A style argument may be specified when calling the plot function. This set of commands will create a red line.

A = [1.0,4.0,16.0,32.0];
plt.plot(A,'r')
plt.show()
From raw.githubusercontent.com on May 18 2019 20:23:27.

Other colors may be used by specifying a set of r,g,b values. This set of commands will create a gray line.

A = [1.0,4.0,16.0,32.0];
plt.plot(A,color=[0.5,0.5,0.5])
plt.show()
From raw.githubusercontent.com on May 18 2019 20:23:27.

2.5. Marker Style

A = [1.0,4.0,16.0,32.0];
plt.plot(A,'*')
plt.show()
From raw.githubusercontent.com on May 18 2019 20:23:27.

Marker colors may be specified using the same syntax as the line color

A = [1.0,4.0,16.0,32.0];
plt.plot(A,'*',color=[0.5,0.5,0.5])
plt.show()
From raw.githubusercontent.com on May 18 2019 20:23:28.

Marker size may be specified using MakerSize followed by a comma and an integer.

A = [1.0,4.0,16.0,32.0];
plt.plot(A,'*',markersize=10)
plt.show()
From raw.githubusercontent.com on May 18 2019 20:23:28.

2.6. Line Style

By default, the points are connected with lines. Options include -,:,-.,--

A = [1.0,4.0,16.0,32.0];
plt.plot(A,'-',linewidth=5)
plt.show()
From raw.githubusercontent.com on May 18 2019 20:23:28.

Line width may be specified using LineWidth followed by a comma and an integer.

A = [1.0,4.0,16.0,32.0];
plt.plot(A,'r-')
plt.show()
From raw.githubusercontent.com on May 18 2019 20:23:28.

2.7. Style Combinations

Multiple styles may be specified. For example, to create a red solid line, use r-

A = [1.0,4.0,16.0,32.0];
plt.plot(A,'r-', linewidth=3,markersize=10)
From raw.githubusercontent.com on May 18 2019 20:23:28.

Multiple styles may be specified. For example, to create a red solid line, use r-

A = [1.0,4.0,16.0,32.0];
plt.plot(A,'r*-', linewidth=3,markersize=10)
From raw.githubusercontent.com on May 18 2019 20:23:28.

To create a red solid line with stars at the connecting points, use r*-

A = [1.0,4.0,16.0,32.0];
plt.plot(A,'r*-', linewidth=3,markersize=10)
plt.show()
From raw.githubusercontent.com on May 18 2019 20:23:28.

2.8. Multiple lines

To create a legend, use legend:

A = [1.0,4.0,16.0,32.0];
B = [1.1,4.4,16.9,32.9];
l1, = plt.plot(A,'b')
l2, = plt.plot(B,'r')
plt.legend(['A','B'])
plt.show()
From raw.githubusercontent.com on May 18 2019 20:23:28.

To create a legend, use legend:

A = [1.0,4.0,16.0,32.0];
B = [1.1,4.4,16.9,32.9];
l1, = plt.plot(A,'b')
l2, = plt.plot(B,'r')
plt.legend(['A','B'],loc='upper left')
plt.show()
From raw.githubusercontent.com on May 18 2019 20:23:29.

2.9. Axis Numbering

In this example, note that by default Matplotlib chose to label the values in 0.5 increments. This is not a good default - all of the x-values are integers. The following example shows how to modify the values that are labeled.

A = [1.0,4.0,16.0,32.0];
plt.plot(A,'r*-', linewidth=3,markersize=10)
plt.xticks([0,1,2,3])
plt.show()

To modify the y-position labels, use yTicks instead of xticks.

From raw.githubusercontent.com on May 18 2019 20:23:29.

2.10. Axis Limits

In this example, note that by default Matplotlib chose to label the values in 0.5 increments. This is not a good default - all of the x-values are integers. The following example shows how to modify the values that are labeled.

A = [1.0,4.0,16.0,32.0];
plt.plot(A,'r*-', linewidth=3,markersize=10)
plt.xticks([0,1,2,3])
plt.xlim([-0.1 3.1])
plt.show()

To modify the y-position labels, use yTicks instead of xticks.

From raw.githubusercontent.com on May 18 2019 20:23:29.

3. Problems

3.1. Scalar Time Series Plots I

Create a plot with a smooth-looking line of y = x2 over the range x = [0-4]. The plot must have a grid, labels and a green line of width 3. The axes must be labeled.

3.2. Scalar Time Series Plots II

The use Python to compute the population for two different scenarios over 100 years:

  1. An initial population of 100 and a growth rate of 1%/yr.
  2. An initial population of 95 and a growth rate of 1.1%/yr.

Plot both population scenarios on the same axes and draw a vertical line at the year in which the populations are nearest each other. The plot should contain a legend and axis labels on the plot.

Personal tools