Plotting with matplotlib in Python

Effective plots are important to synthesize the information into relevant and persuasive information. The following tutorial details some of the common data plotting functions within Python.

Tutorial Source Code

import numpy as np
x = np.linspace(0,6,100)
y = np.sin(x)
z = np.cos(x)

import matplotlib.pyplot as plt
plt.plot(x,y,'r--',linewidth=3)
plt.plot(x,z,'k:',linewidth=2)
plt.legend(['y','z'])
plt.xlabel('x')
plt.ylabel('values')
plt.xlim([0, 3])
plt.ylim([-1.5, 1.5])
plt.savefig('myFigure.png')
plt.savefig('myFigure.eps')
plt.show()

If using the iPython notebook, exclude the command plt.show() and include %matplotlib inline before loading matplotlib.pyplot as shown below.

import numpy as np
x = np.linspace(0,6,100)
y = np.sin(x)
z = np.cos(x)

%matplotlib inline
import matplotlib.pyplot as plt
plt.plot(x,y,'r--',linewidth=3)
plt.plot(x,z,'k:',linewidth=2)
plt.legend(['y','z'])
plt.xlabel('x')
plt.ylabel('values')
plt.xlim([0, 3])
plt.ylim([-1.5, 1.5])
plt.savefig('myFigure.png')
plt.savefig('myFigure.eps')

Additional Tutorials

This tutorial can also be completed with scripting programming languages like Excel and MATLAB. Click on the appropriate link for additional information and source code.

Generate a BYU Football field with Python.


Generative AI Learning

From this class forward, AI-generated code is a legitimate tool - used with the full pattern: you specify, it generates, you verify, you defend the result. Plotting is the ideal first practice: generated plot code is easy to audit because the figure shows you what it did.

"Quiz me with 4 questions, one at a time, on matplotlib: what plt.plot, plt.xlabel/ylabel, plt.legend, and plt.savefig each do and the order they must happen relative to plt.show, how to put two curves on one axis with distinguishable line styles, why axis labels without units make an engineering plot unusable, and when a log scale (semilogy) is the right choice. Grade my answers and list my misconceptions."
"I will specify a figure; generate matplotlib code ONLY from my spec. My spec: {data source and variables; curves and styles; axis labels with units; title; legend; saved filename and format}. After the code, list every choice you made that my spec left open (figure size, fonts, colors, limits, grid). I will run it, compare the figure to my spec line by line, and tighten the spec until the first-try output is exactly right."

Modern data: A one-day solar generation curve (power vs hour, cloudy day and clear day on the same axes) is a two-line matplotlib exercise that also teaches the domain: the AI can generate the plotting code, but only you can say whether the curve's shape, units, and peak make physical sense.

What to Turn In

Submit a short report (PDF, 1-2 pages) that curates your results into a demonstration of what you learned. You may use Generative AI to help write the report, but you must guide it to the correct visualizations, justifications, and assumptions. Answer these questions:

  1. Include one publication-quality figure you coded yourself (two curves, labels with units, legend, title, saved to PNG) and the code that made it.
  2. Include the spec you gave the AI, the generated code, and the resulting figure. List the unspecified choices the AI made - which one would have misled a reader if you had not caught it?
  3. Iterate the spec once: what did you add, and show the improved figure. What does this teach about specifications in general?
  4. From the quiz prompt: one question you missed and the corrected answer.

Course Information

Excel and VBA

Python

MATLAB

MathCAD

Related Courses

Admin