Difference between revisions of "Python:Plotting"

From PrattWiki
Jump to navigation Jump to search
(Notes)
 
(42 intermediate revisions by the same user not shown)
Line 1: Line 1:
This page will primarily go over some examples of different ways to plot data sets. It assumes
+
This page will primarily go over some examples of different ways to plot data sets. It assumes the lines
import matplotlib.pyplot as plt
+
<syntaxhighlight lang="python">
is in the program.
+
import math as m
 +
import numpy as np
 +
import matplotlib.pyplot as plt
 +
</syntaxhighlight >
 +
are in the program.
  
 
== Introduction ==
 
== Introduction ==
 +
For a good tutorial on using the matplotlib.pyplot module - especially the object-oriented approach, see [https://realpython.com/python-matplotlib-guide/ Python Plotting With Matplotlib (Guide)] by Brad Solomon as recommended by [https://bme.duke.edu/faculty/mark-palmeri Dr. Mark Palmeri], Duke BME.
 +
 +
Note that commands given as plt.COMMAND() may sometimes apply to either a figure or a set of axes or both.  In November of 2019, plot infrastructure on this page was formalized to independently create the figure and one or more handles to axes.  The primary reason for this is that the formal method is required in order to create 3D plots and so practicing it with 2D plots will prepare programmers for all cases.
 +
 +
=== Notes ===
 +
In Fall of 2022, it looks like figures may not update correctly unless (a) saved, (b) resized, (c) explicitly told to update, or (d) interactive plotting is turned on.  That is:
 +
 +
* Saving the figure with
 +
fig.savefig("FILE.EXT")
 +
* Resizing the window using the mouse
 +
* Explicitly updating with
 +
fig.canvas.draw()
 +
* Turning interactive plotting on with
 +
plt.ion()
 +
 +
This is with Spyder 5.2.2.
 +
 +
=== Simple Examples ===
 +
If you are here to see some basic ways to get plots to work, this is the section for you!  The main steps for creating most (relatively simple) plots are:
 +
* Import appropriate modules
 +
* Create figure and axis handles
 +
* Make plot
 +
* Add title, labels, etc.
 +
* Save the plot
 +
Note also that depending on your development environment, graphics may show up in their own window or in the console.  These examples generally assume you are having graphics show up in their own window - it makes it much easier to make alterations to graphs after their initial creation.  If you are using Spyder, the way to get graphs to show up in their own window is:
 +
* Within the Tools menu select Preferences
 +
* From the Preferences list on the left select IPython console
 +
* In the right window, click the Graphics tab
 +
* In the second section (Graphics backend), select Automatic
 +
* Click the OK button at the bottom right of the window.
 +
==== Plotting a few points ====
 +
Here is a complete example that will generate and save a single figure window with a single set of axes within it:
 +
[[File:Example1 PunPlot.png|thumb]]
 +
<syntaxhighlight lang=python>
 +
# Imports
 +
import matplotlib.pyplot as plt
 +
# Data
 +
x = [1, 2, 4]
 +
y = [2, 1, 5]
 +
# Set up figure and axis
 +
fig = plt.figure(num=1, clear=True)
 +
ax = fig.add_subplot(1, 1, 1)
 +
# Plot using red circles
 +
ax.plot(x, y, 'ro')
 +
# Set labels and turn grid on
 +
ax.set(xlabel='x', ylabel='y', title='Three Points')
 +
ax.grid(True)
 +
# Use space most effectively
 +
fig.tight_layout()
 +
# Save as a PNG graphics file
 +
fig.savefig('Example1.png')
 +
</syntaxhighlight>
 +
Note that the <code>plt.figure(num=1, clear=True)</code> line does not work correctly in some versions of matplotlib - the way you will detect the problem is if you re-run the code and there either looks to be two different plots on top of each other or your current plots takes up less of the screen and the text gets dark.  If that happens to you, look at the fix in the [[#Clearing_Figures]] section below.
 +
 +
==== Plotting a couple lines ====
 +
This example uses the numpy module to create an array and apply trig and basic math to that array.  It also shows how to use the <code>label</code> kwarg in concert with the legend method to produce a legend.  Finally, notice in the <code>ax.set()</code> method that there is an <code>r</code> in front of the <code>ylabel</code> text.  If you want to use basic <math>\LaTeX</math> commands in labels, titles, or text, you need to pass them in raw strings.  Finally, the <code>markevery</code> command allwys you to specify how often to put a data marker when plotting a data set.
 +
[[File:Example2 PunPlot.png|thumb]]
 +
<syntaxhighlight lang=python>
 +
# Imports
 +
import matplotlib.pyplot as plt
 +
import numpy as np
 +
# Data
 +
t = np.linspace(0, 2*np.pi, 101)
 +
x1 = 1 + 2 * np.cos(t)
 +
x2 = 2 + 1 * np.sin(t)
 +
# Set up figure and axis
 +
fig = plt.figure(num=1, clear=True)
 +
ax = fig.add_subplot(1, 1, 1)
 +
# Plot using red circles
 +
ax.plot(t, x1, 'gs-', label='First Line', markevery=10)
 +
ax.plot(t, x2, 'bd:', label='Second Line', markevery=20)
 +
# Set labels and turn grid on
 +
ax.set(xlabel='Time $t$, sec', ylabel=r'Value $\xi$, m', title='Two Lines')
 +
ax.grid(True)
 +
ax.legend(loc='best')
 +
# Use space most effectively
 +
fig.tight_layout()
 +
# Save as a PNG file
 +
fig.savefig('Example2.png')
 +
</syntaxhighlight>
  
 
=== The <code>plt.plot()</code> Function ===
 
=== The <code>plt.plot()</code> Function ===
 
The <code>plt.plot()</code> function is used to plot sets of data on a 2-D grid.
 
The <code>plt.plot()</code> function is used to plot sets of data on a 2-D grid.
 
What follows comes from matplotlib.pyplot's <code>help</code> function  (some paragraphs have been snipped out).  The line styles, symbols, and colors are formatted as a clearer table.  
 
What follows comes from matplotlib.pyplot's <code>help</code> function  (some paragraphs have been snipped out).  The line styles, symbols, and colors are formatted as a clearer table.  
<source lang="text">
+
<syntaxhighlight lang="text">
  
 
plot(*args, **kwargs)
 
plot(*args, **kwargs)
Line 44: Line 128:
 
     The following format string characters are accepted to control
 
     The following format string characters are accepted to control
 
     the line style or marker (and) the following color abbreviations are supported:
 
     the line style or marker (and) the following color abbreviations are supported:
</source>
+
</syntaxhighlight >
 
<center>
 
<center>
 
{| style="text-align:center" cellpadding="3"
 
{| style="text-align:center" cellpadding="3"
Line 112: Line 196:
 
</center>
 
</center>
  
<source lang=text>     
+
<syntaxhighlight lang=text>     
 
     In addition, you can specify colors in many weird and
 
     In addition, you can specify colors in many weird and
 
     wonderful ways, including full names (``'green'``), hex
 
     wonderful ways, including full names (``'green'``), hex
Line 147: Line 231:
 
         plot(x, y, color='green', linestyle='dashed', marker='o',
 
         plot(x, y, color='green', linestyle='dashed', marker='o',
 
             markerfacecolor='blue', markersize=12).
 
             markerfacecolor='blue', markersize=12).
</source>
+
</syntaxhighlight >
 
 
<!--
 
=== The <code>subplot</code> Function ===
 
The <code>subplot</code> function is used to plot multiple plot windows
 
within the same figure.
 
What follows comes from MATLAB's <code>help</code> function in MATLAB R2009a<ref name="Help:Subplot">Quoted from MATLAB help file for <code>plot</code></ref> (some
 
paragraphs have been snipped out).
 
<source lang="text">
 
SUBPLOT Create axes in tiled positions.
 
    H = SUBPLOT(m,n,p), or SUBPLOT(mnp), breaks the Figure window
 
    into an m-by-n matrix of small axes, selects the p-th axes for
 
    for the current plot, and returns the axis handle.  The axes
 
    are counted along the top row of the Figure window, then the
 
    second row, etc.  For example,
 
 
 
        SUBPLOT(2,1,1), PLOT(income)
 
        SUBPLOT(2,1,2), PLOT(outgo)
 
 
 
    plots income on the top half of the window and outgo on the
 
    bottom half. If the CurrentAxes is nested in a uipanel the
 
    panel is used as the parent for the subplot instead of the
 
    current figure.
 
 
 
    SUBPLOT(m,n,p), if the axis already exists, makes it current.
 
    SUBPLOT(m,n,p,'replace'), if the axis already exists, deletes it and
 
    creates a new axis.
 
    SUBPLOT(m,n,p,'align') places the axes so that the plot boxes
 
    are aligned instead of preventing the labels and ticks from
 
    overlapping.
 
    SUBPLOT(m,n,P), where P is a vector, specifies an axes position
 
    that covers all the subplot positions listed in P.
 
    SUBPLOT(H), where H is an axis handle, is another way of making
 
    an axis current for subsequent plotting commands.
 
 
    SUBPLOT('position',[left bottom width height]) creates an
 
    axis at the specified position in normalized coordinates (in
 
    in the range from 0.0 to 1.0).
 
 
    SUBPLOT(m,n,p, PROP1, VALUE1, PROP2, VALUE2, ...) sets the
 
    specified property-value pairs on the subplot axis. To add the
 
    subplot to a specific figure pass the figure handle as the
 
    value for the 'Parent' property.
 
 
    If a SUBPLOT specification causes a new axis to overlap an
 
    existing axis, the existing axis is deleted - unless the position
 
    of the new and existing axis are identical.  For example,
 
    the statement SUBPLOT(1,2,1) deletes all existing axes overlapping
 
    the left side of the Figure window and creates a new axis on that
 
    side - unless there is an axes there with a position that exactly
 
    matches the position of the new axes (and 'replace' was not specified),
 
    in which case all other overlapping axes will be deleted and the
 
    matching axes will become the current axes.
 
% snip
 
</source>
 
Note that subplots are numbered first from left to right, then by row - in other words, like a telephone dial.  This is transposed from how just about everything else is numbered in MATLAB.
 
  
=== The <code>orient</code> Function ===
 
The <code>orient</code> function is used to change how the plot is oriented
 
on the page when printing.  Note that it does not affect how the plot
 
looks on the screen.  Furthermore, the <code>orient</code> command must
 
be used each time a new figure opens for that figure to have a specified
 
orientation.
 
What follows comes from MATLAB's <code>help</code> function in MATLAB R2009a<ref name="Help:Orient">Quoted from MATLAB help file for <code>orient</code></ref> (some
 
paragraphs have been snipped out).
 
<source lang="text">
 
ORIENT Set paper orientation for printing.
 
    ORIENT is used to set up the orientation of a Figure or Model
 
    window for printing.
 
 
    ORIENT LANDSCAPE causes subsequent PRINT operations from the current
 
    Figure window to generate output in full-page landscape orientation
 
    on the paper.
 
 
    ORIENT ROTATED causes subsequent PRINT operations from the current
 
    Figure window to generate output in full-page rotated orientation
 
    on the paper.
 
 
    ORIENT PORTRAIT causes subsequent PRINT operations from the current
 
    Figure window to generate output in portrait orientation.
 
 
    ORIENT TALL causes the current Figure window to map to the whole page
 
    in portrait orientation for subsequent PRINT operations.
 
 
    ORIENT, by itself, returns a string containing the paper
 
    orientation, either PORTRAIT, LANDSCAPE, ROTATED or TALL
 
        of the current Figure.
 
 
    ORIENT(FIGHandle) or ORIENT(MODELName) returns the current
 
    orientation of the Figure or Model.
 
 
    ORIENT( FIG, ORIENTATION) specifies which figure to orient and how to
 
    orient it based on the rules given above.  ORIENTATION is one of
 
    'landscape', 'portrait', 'rotated', or 'tall'.
 
% snip
 
</source>
 
-->
 
 
=== The <code>plt.savefig()</code> Function ===
 
=== The <code>plt.savefig()</code> Function ===
The <code>plt.savefig()</code> function is used to save a plot to a file.  The type of sile is determined by the extension of the file name.  For example,  
+
The <code>plt.savefig()</code> function is used to save a plot to a file.  The type of file is determined by the extension of the file name.  For example,  
  plt.savefig('blah.eps')
+
  plt.savefig('blah.png')
will save the file to a color Encapsulated PostScript file, while
+
will save the file to a portable network graphics file, while
 
  plt.savefig('blah.pdf')
 
  plt.savefig('blah.pdf')
will save it to a PDF.  From the help file for the command, Most backends support png, pdf, ps, eps and svg.
+
will save it to a PDF.  From the help file for the command, "Most backends support png, pdf, ps, eps and svg."  If you have created a figure handle, you should use the <code>safefig</code> on that handle - that is, use
<!--
+
fig.savefig('blah.png')
 +
instead of using <code>plt</code>.
 +
 
 
=== Example ===
 
=== Example ===
[[File:Usfplot.png|thumb|Example showing a figure split into 2x3 subplots with subplot 3 holding an empty axis and subplot 4 holding a graph of the unit step function.]]
+
[[File:UsfplotPython.png|thumb|Example showing a figure split into 2x3 subplots with subplot 3 holding an empty axis and subplot 4 holding a graph of the unit step function.]]
The following MATLAB code demonstrates how to fill the fourth window
+
The following Python code demonstrates how to fill the fourth window
of a 2x3 plot figure, save the figure as a PostScript file, and
+
of a 2x3 plot figure and save the figure as a PNG file; the resulting figure is in the thumbnail at right.
preview it with <code>kghostview</code>; the resulting figure is in the thumbnail at right.
+
<syntaxhighlight lang="Python">
<source lang="matlab">
+
# Import required modules
% Create a vector of numbers between -1 and 1 with 0.001 between numbers
+
import numpy as np
x=-1:0.001:1;
+
import matplotlib.pyplot as plt
  
%This formula for usf is one of several ways to define the unit step function
+
# Create a 100 numbers between -1 and 1
usf = @(t) t>=0;  
+
x = np.linspace(-1, 1, 100);  
  
%Create a figure with two rows of three figures and make subplot 4 (bottom row left) current
+
# This formula for usf is one of several ways to define the unit step function
subplot(2,3,4)  
+
def usf(t):
 +
    return (t>=0)*1.0;
  
% Plot the values of the function usf(x) against the vector x
+
# Create a figure with two rows of three figures
plot(x, usf(x))  
+
fig = plt.figure(num=1, clear=True)
 +
ax = fig.subplots(2, 3)
  
% Change the axes so the function is not covered by the subplot box
+
# Plot the values of the function usf(x) against the vector x in bottom left
axis([-1, 1, -1, 2])
+
ax[1][0].plot(x, usf(x))  
  
% Give the current subplot a title
+
# Change the axes so the function is not covered by the subplot box
title('Unit Step Function')
+
ax[1][0].axis([-1, 1, -1, 2])
  
% Set the x-label
+
# Set labels and title
xlabel('x')
+
ax[1][0].set(title='Unit Step Function', xlabel='x', ylabel='u(x)')
  
% Set the y-label
+
# Clear the rest other than bottom left and top right
ylabel('u(x)')
+
fig.delaxes(ax[0][0])
 +
fig.delaxes(ax[0][1])
 +
fig.delaxes(ax[1][1])
 +
fig.delaxes(ax[1][2])
  
% Make subplot 3 (top row right) current - it will be empty in the example
+
# Issue tight layout to fix label overlaps
subplot(2,3,3)  
+
fig.tight_layout()
  
% Use landscape (wide) orientation
+
# Send the current figure to a file named usfplot.png
orient landscape
+
fig.savefig('usfplot.png')
 +
</syntaxhighlight>
  
% Send the current figure to a file named usfplot.eps;
+
=== Example Redux ===
% Note that the `.eps' is automatically added
+
[[File:UsfplotPython.png|thumb|Example showing a figure split into 2x3 subplots with subplot 3 holding an empty axis and subplot 4 holding a graph of the unit step function.]]
print -deps usfplot
+
The following Python code demonstrates how to fill the fourth window
 +
of a 2x3 plot figure and save the figure as a PNG file; the resulting figure is in the thumbnail at right. In this case, instead of creating 6 subplots and deleting four of them, the code below only adds the subplots we want.
 +
<syntaxhighlight lang="Python">
 +
# Import required modules
 +
import numpy as np
 +
import matplotlib.pyplot as plt
  
% The "!" character lets MATLAB act as if you were at a regular terminal.
+
# Create a 100 numbers between -1 and 1
% This command thus lets you view usfplot.eps using UNIX's kghostview
+
x = np.linspace(-1, 1, 100);
!kghostview usfplot.eps
+
 
</source>
+
# This formula for usf is one of several ways to define the unit step function
<!--
+
def usf(t):
\begin{figure}[h!]
+
    return (t>=0)*1.0;
\begin{center}
+
 
\epsfig{file=../GENERAL/MATLAB/sixplot.ps, width=2.4in, angle=90}
+
# Create a figure
\caption{Sample Output}
+
fig = plt.figure(num=1, clear=True)
\end{center}
+
 
\end{figure}
+
# Create axes for panels 3 and 4 in a 2x3 configuration
 +
ax3 = fig.add_subplot(2, 3, 3)
 +
ax4 = fig.add_subplot(2, 3, 4)
 +
 
 +
# Plot the values of the function usf(x) against the vector x in bottom left
 +
ax4.plot(x, usf(x))
 +
 
 +
# Change the axes so the function is not covered by the subplot box
 +
ax4.axis([-1, 1, -1, 2])
 +
 
 +
# Give the current subplot a title
 +
ax4.set_title('Unit Step Function')
 +
 
 +
# Set the x-label
 +
ax4.set_xlabel('x')
 +
 
 +
# Set the y-label
 +
ax4.set_ylabel('u(x)')
 +
 
 +
# Issue tight layout to fix label overlaps
 +
fig.tight_layout()
 +
 
 +
# Send the current figure to a file named usfplot.png
 +
fig.savefig('usfplot.png')
 +
</syntaxhighlight>
 +
 
 +
== Setting Up Subplots ==
 +
See [[Python:Plotting/Subplots]] for more on setting up subplots.
  
 +
== Python Settings ==
 +
For this course, you will generally want to have your graphics set to automatic; to make this change in Spyder:
 +
*Open the preferences window
 +
**On Windows, go to the Tools menu near the top right of the window and select Preferences
 +
**On MACs, go to the python menu near the top left of the window and select Preferences
 +
*In the Preferences window at the left select IPython console
 +
*In the right half of the Preferences window, select the Graphics tab
 +
*In the Backend pulldown, select Automatic
 +
*Click OK in the Preferences window
  
% LocalWords:  MATLAB's filename kghostview
 
-->
 
<!--
 
 
== General Plotting Tips ==
 
== General Plotting Tips ==
 
You must make sure that your data sets are presented properly.  Here are some guidelines:
 
You must make sure that your data sets are presented properly.  Here are some guidelines:
Line 313: Line 348:
 
<tr>
 
<tr>
 
<td width=100%>
 
<td width=100%>
<source lang="matlab">
+
<syntaxhighlight lang="python">
xlabel('{\it v}, m/s')
+
ax.set(xlabel='$v$, m/s')
xlabel('{\it v} (m/s)')
+
ax.set(xlabel='$v$ (m/s)')
xlabel('Velocity, m/s')
+
ax.set(xlabel='Velocity, m/s')
xlabel('Velocity (m/s)')
+
ax.set(xlabel='Velocity (m/s)')
xlabel('Velocity ({\it v}), m/s');
+
ax.set(xlabel='Velocity ($v$), m/s')
xlabel('Velocity ({\it v}, m/s)');
+
ax.set(xlabel='Velocity ($v$, m/s)')
</source>
+
</syntaxhighlight >
 
</td></tr></table>
 
</td></tr></table>
:You should be consistent within a single graph as far as which version you use, and you should pick which format you believe conveys the information most efficiently and effectively.
+
: Note that you can use basic $$\LaTeX$$ commands (Greek letters, superscripts, etc) but if you do, you need to make sure the kwarg gets the raw version of the string; specifically, if you want to have the Greek letter $$\theta$$ and you try
* Make sure your titles make sense and that you have your NET ID in one of the titles for each figure you create.  If you have multiple subplots, you only need one of the titles to have your NET ID.  Typically, the title will read "DEP. VAR vs. INDEP. VAR for DESCRIPTION (NET ID)" where DEP. VAR is your dependent variable (typically on the ''y'' axis), INDEP. VAR is your independent variable (typically on the ''x'' axis), DESCRIPTION is something that will differentiation this particular plot from others that might have the same variables (for example a data, or an experiment number), and NET ID is your NET ID.
+
<syntaxhighlight lang="python">
 +
ax.set(xlabel='$\theta(t)$, rad')
 +
</syntaxhighlight >
 +
:you will end up getting "''heta(t)'', rad" since the "\t" is processed as a tab character.  If you use:
 +
<syntaxhighlight lang="python">
 +
ax.set(xlabel=r'$\theta(t)$, rad')
 +
</syntaxhighlight >
 +
: you will get the correct result.
 +
* You should be consistent within a single graph as far as which version of axis labels you use, and you should pick which format you believe conveys the information most efficiently and effectively.
 +
* Make sure your titles make sense.  Typically, the title will read "DEP. VAR vs. INDEP. VAR for DESCRIPTION" where DEP. VAR is your dependent variable (typically on the ''y'' axis), INDEP. VAR is your independent variable (typically on the ''x'' axis), and DESCRIPTION is something that will differentiation this particular plot from others that might have the same variables (for example a data, or an experiment number).
 
* Data points should be represented by symbols and model equations should be represented by lines.  Be sure to use a legend to identify each item plotted if there are multiple data sets on the same graph.
 
* Data points should be represented by symbols and model equations should be represented by lines.  Be sure to use a legend to identify each item plotted if there are multiple data sets on the same graph.
* Typically, you will want to set the axis limits such that no data point is on the figure boundary.  Certainly you do not want a line to be plotted on top of a figure boundary.  After you make a plot, if there is a data point on an edge, look at the current axes and go out a little bit. Just make sure if you end up fundamentally changing your code that you adjust the axes accordingly. Alternately, you can use the [[#Keeping Data Off The Edge| gzoom]] function below to automatically expand the graph.
+
* Typically, you will want to set the axis limits such that no data point is on the figure boundary.  Certainly you do not want a line to be plotted on top of a figure boundary.  After you make a plot, if there is a data point on an edge, look at the current axes and go out a little bit. Just make sure if you end up fundamentally changing your code that you adjust the axes accordingly.
* If you end up using the <code>hold on</code> command, you should only use it '''after''' the first plotting command is issued; if not, there are some parts of a typical plot that will not show up, such as the complete plot frame. 
 
  
== Keeping Data Off The Edge ==
+
== Using Different Line Styles ==
Typically, you will not want data points to land on the edge of a graph.  If you are just plotting lines, this is not a problem but if you are actually putting symbols at each point, those symbols should not cross the border out of the axes.  To combat this, you can put a short program called <code>gzoom.m</code> into your directory and run it. The default case is to add 10% to each direction of the graph in the active figure.  The first argument specifies what the expansion value should be (if you want it different from 0.1) while the second argument specifies which figure should be expanded (if you want it to be different from the active figure). The gzoom function is:
+
Most of the time, you will be plotting three or fewer different lines on a single window, and they can thus be distinguished from one another in [[Python]] by using different line styles:
<source lang="matlab">
+
<html>
function gzoom(n, f)
+
<iframe src="https://trinket.io/embed/python3/4a45aaa186" width="100%" height="400" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
% gzoom.m
+
</html>
% Michael R. Gustafson II (mrg@duke.edu)
+
<!--  
% Uploaded to pundit.pratt.duke.edu on September 6, 2008
+
<syntaxhighlight lang="python">
%
+
import numpy as np
% Function to determine the size of a figure window and increase the size
+
import matplotlib.pyplot as plt
% GZOOM will increase figure 1 by a factor of
 
%      0.1 of the original size in each direction
 
% GZOOM(r) will increase figure 1 by a factor of
 
%      r of the original size in each direction 
 
% GZOOM(r, f) will increase figure f by a factor of
 
%      r of the original size in each direction
 
  
if nargin==0
+
x = np.linspace(0, 1, 100)
    f=gca; n=0.1;
+
y1 = x**0.5
elseif nargin==1
+
y2 = x**1
    f=gca;
+
y3 = x**2
end
 
v=axis;
 
xr = (v(2)-v(1));
 
yr = (v(4)-v(3));
 
axis(f, [v + n*[-xr xr -yr yr]]);
 
</source>
 
  
== Using Different Line Styles ==
+
fig = plt.figure(num=1, clear=True)
Most of the time, you will be plotting three or fewer different lines on a single window, and they can thus be distinguished from one another in [[MATLAB]] by using different line styles.  There are two different ways to get multiple data sets plotted in the same figure - use the <code>hold on</code>...<code>hold off</code> paradigm or put all the "plot triplets" in a single plot command.  What follows is an example of the latter case:
+
ax = fig.add_subplot(1, 1, 1)
<source lang="matlab">
+
 
x = linspace(0, 1, 100);
+
ax.plot(x, y1, 'k-', label='$y=x^{0.5}$')
y1 = x.^0.5;
+
ax.plot(x, y2, 'k--', label='$y=x$')
y2 = x.^1;
+
ax.plot(x, y3, 'k:', label='$y=x^2$')
y3 = x.^2;
+
ax.legend(loc='best')
plot(x, y1, 'k-', x, y2, 'k--', x, y3, 'k:');
+
ax.set(xlabel = 'x', ylabel = 'y', title = '$y=x^n$ for Three Values of n')
legend('y=x^{0.5}', 'y=x', 'y=x^2', 'location', 'best');
+
 
title('y=x^n for Three Values of n (mrg)');
+
fig.tight_layout()
xlabel('x');
+
fig.savefig('CurvePlotPython.png')
ylabel('y');
+
</syntaxhighlight >
print -deps CurvePlot % note that adds .eps to filename
 
</source>
 
 
The figure this creates will be:
 
The figure this creates will be:
 
<center>
 
<center>
[[Image:CurvePlot.png]]
+
[[Image:CurvePlotPython.png]]
 
</center>
 
</center>
Note the legend - the use of the final arguments ''' 'location', 'best' ''' will make MATLAB look for the best spot in the window for the legend - which is to say, somewhere ''not'' conflicting with any of the lines if at all possibleAlso note that the text in the legend and in the title contains basic [[LaTeX]] code for obtaining the superscripts.
+
-->
 +
Note the legend command argument.  The default location is upper right but that may land on the data.  Telling it to use the best location will have Python look for the most-blank part of the plotThere are many other options for location - try <code>help(plt.legend)</code> and look at the section about <code>loc</code> in the Other Parameters.
  
 
== Using Different Point Styles ==
 
== Using Different Point Styles ==
Sometimes there will be more data sets on a graph than there are line styles in MATLAB.   
+
Sometimes there will be more data sets on a graph than there are line styles in Python.   
 
In cases like this, you may think to use symbols at the
 
In cases like this, you may think to use symbols at the
 
points.  The problem with that becomes clear if you have a large number of data points -  
 
points.  The problem with that becomes clear if you have a large number of data points -  
 
you do not want to try to jam hundreds of symbols onto a curve.
 
you do not want to try to jam hundreds of symbols onto a curve.
 
The left graph in the figure below shows what a disaster that
 
The left graph in the figure below shows what a disaster that
could be.  Instead, you can plot a '''line''' with all your data but plot
+
could be.  Instead, you can plot a '''line''' with all your data but then tell Python to
points on top of the line with only some of the data.  The right side of the figure  
+
plot points only every so often.  This is done with the ''markevery'' kwarg.  The right side of the figure  
shows the result of this operation.  The code for  
+
shows the result of this operation.   
both graphs in the figure is given below it.
+
<html>
 +
<iframe src="https://trinket.io/embed/python3/9f30e477a0" width="100%" height="400" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 +
</html>
 +
<!--
 +
The code for both graphs in the figure is given below it.
 
<center>
 
<center>
[[Image:Deccurves.png]]
+
[[Image:DeccurvesPython.png]]
 
</center>
 
</center>
  
<source lang="matlab">
+
<syntaxhighlight lang="python">
x = linspace(0, 1, 100);
+
import numpy as np
y1 = x.^0.5;
+
import matplotlib.pyplot as plt
y2 = x.^1;
 
y3 = x.^2;
 
subplot(1,2,1) % left graph (ewwwwwww)
 
plot(x, y1, 'k-s', x, y2, 'k-o', x, y3, 'k-p');
 
legend('y=x^{0.5}', 'y=x', 'y=x^2', 'location', 'best');
 
title('y=x^n for Three Values of n (mrg)');
 
xlabel('x');
 
ylabel('y');
 
  
subplot(1,2,2) % right graph (ooo.  aah.  wow)
+
x = np.linspace(0, 1, 100)
plot(x, y1, 'k-', x, y2, 'k-', x, y3, 'k-'); % lines only!
+
y1 = x**0.5
Incr = 5;
+
y2 = x**1
Indices = 1:Incr:length(x);
+
y3 = x**2
hold on % holds lines and now add points
+
fig = plt.figure(num=1, clear=True)
PointPlot=plot(...
+
ax = fig.subplots(1, 2)
    x(Indices), y1(Indices), 'ks',...
 
    x(Indices), y2(Indices), 'ko',...
 
    x(Indices), y3(Indices), 'kp');
 
hold off
 
legend(PointPlot, 'y=x^{0.5}', 'y=x', 'y=x^2',  'location', 'best');
 
title('y=x^n for Three Values of n (mrg)');
 
xlabel('x');
 
ylabel('y');
 
</source>
 
  
Note that the assignment of one of the plots to the variable <code>PointPlot</code> is required so that the legend can be told which set of
+
# left graph (ewwwwwww)
plots to use as a basis for the legend.  Without this, it would be
+
ax[0].plot(x, y1, 'k-s', x, y2, 'k-o', x, y3, 'k-d')
possible for the legend to be based on the plot using solid lines (which are
+
ax[0].legend(['$y=x^{0.5}$', '$y=x$', '$y=x^2$'], loc='best')
the same for all three data sets) rather than to be based on the
+
ax[0].set(xlabel = 'x', ylabel = 'y', title = 'Three Power Curves')
plot using the symbols.
+
 
 +
# right graph (ahhhhhhh)
 +
ax[1].plot(x, y1, 'k-s', x, y2, 'k-o', x, y3, 'k-d', markevery=10)
 +
ax[1].legend(['$y=x^{0.5}$', '$y=x$', '$y=x^2$'], loc='best')
 +
ax[1].set(xlabel = 'x', ylabel = 'y', title = 'Three Power Curves')
 +
 
 +
fig.tight_layout()
 +
fig.savefig('DeccurvesPython.png')
 +
</syntaxhighlight >
 +
-->
  
 
== Using Different Scales ==
 
== Using Different Scales ==
In the above example, two different scales were used for the data sets - a refined scale for the line and a rougher scale for the data points themselves.  In other cases, it is the ''line'' that will need the rougher scale.  As an example, assume you want MATLAB to numerically find the minimum of the function <math>y=3x^2+11x-2</math> using the built-in <code>min</code> command.  To get the most precise answer possible, you will want to give MATLAB a very large number of points - say, 1 million.  That code might look like:
+
In the above example, two different scales were used for the data sets - a refined scale for the line and a rougher scale for the data points themselves.  In other cases, it is the ''line'' that will need the rougher scale.  As an example, assume you want Python to numerically find the minimum of the function <math>y=3x^2+11x-2</math> using the built-in <code>min</code> command.  To get the most precise answer possible, you will want to give Python a very large number of points - say, 1 million.  That code might look like:
<source lang="matlab">
+
<syntaxhighlight lang="Python">
P = [3 11 -2]
+
fun = lambda x: 3*x**2 + 11*x - 2
xVals = linspace(-3, 3, 1e6);
+
xVals = np.linspace(-3.0, 3.0, int(1e6))
yVals = polyval(P, xVals);
+
yVals = fun(xVals)
 
yMin = min(yVals)
 
yMin = min(yVals)
xMin = xVals(find(yVals==yMin))
+
xMin = xVals[np.where(yVals==yMin)]
</source>
+
print(xMin, yMin)
Since the domain is 6 and there are 1e6 points, the spacing between points is approximately 6e-06.  The ''x''-coordinate of the answer then should be very close to the actual answer.  In fact, MATLAB determines that the minimum value is -1.208333333332658e+01 and its ''x''-coordinate is -1.833334833334833e+00 - very close to finding the minimum of ''y''=-1.208333333333333e+01 at  ''x''=-1.833333333333333e+00
+
</syntaxhighlight >
 +
Since the domain is 6 and there are 1e6 points, the spacing between points is approximately 6e-06.  The ''x''-coordinate of the answer then should be very close to the actual answer.  In fact, Python determines that the minimum value is -1.208333333332658e+01 and its ''x''-coordinate is -1.833334833334833e+00 - very close to finding the minimum of ''y''=-1.208333333333333e+01 at  ''x''=-1.833333333333333e+00
  
 
The problem, however, is that if you want to graph this, one million points is somewhat absurd.  Unless you are planning to zoom in on the graph, there are around 1000x as many points as necessary/realistic/useful for a plot like this.  Instead, you should either recalculate the equation using a smaller set of independent data or plot only some of the data.  Code for the former might be:
 
The problem, however, is that if you want to graph this, one million points is somewhat absurd.  Unless you are planning to zoom in on the graph, there are around 1000x as many points as necessary/realistic/useful for a plot like this.  Instead, you should either recalculate the equation using a smaller set of independent data or plot only some of the data.  Code for the former might be:
<source lang="matlab">
+
<syntaxhighlight lang="python">
xPlot = linspace(-3, 3, 100);
+
xPlot = np.linspace(-3, 3, 100)
yPlot = polyval(P, xPlot);
+
yPlot = fun(xPlot)
plot(xPlot, yPlot, 'k-');
+
fig = plt.figure(num=1, clear=True)
xlabel('x');
+
ax = fig.add_subplot(1, 1, 1)
ylabel('y');
+
ax.plot(xPlot, yPlot, 'k-')
title('y=3x^2+11x-2 (mrg)');
+
ax.set(xlabel = 'x', ylabel = 'y', title = '$y=3x^2+11x-2$')
grid on
+
ax.grid(True)
print -deps PolyGraph
+
</syntaxhighlight >
</source>
 
 
while code for the latter might be:
 
while code for the latter might be:
<source lang="matlab">
+
<syntaxhighlight lang="python">
plot(xVals(1:1000:end), yVals(1:1000:end), 'k-');
+
fig.plot(xVals[::1000], yVals[::1000], 'k-')
xlabel('x');
+
ax.set(xlabel = 'x', ylabel = 'y', title = '$y=3x^2+11x-2$')
ylabel('y');
+
ax.grid(True)
title('y=3x^2+11x-2 (mrg)');
+
</syntaxhighlight >
grid on
 
print -deps PolyGraph
 
</source>
 
  
 
The advantage of the first version above is that your domain definitely remains the same - the <code>xPlot</code> variable spans the same [-3, 3] as the <code>xVals</code> set even though it has three orders of magnitude fewer values.  The disadvantage is that you will have to re-perform all the calculations on this new set.
 
The advantage of the first version above is that your domain definitely remains the same - the <code>xPlot</code> variable spans the same [-3, 3] as the <code>xVals</code> set even though it has three orders of magnitude fewer values.  The disadvantage is that you will have to re-perform all the calculations on this new set.
  
The advantage of the ''second'' version, then, is that you are using prior data and thus do not have to recalculate anything.  The disadvantage is, you might miss out on the tail end of the data.  In the above example, going from 1 to 1e6 by 1e3 means the last value copied over will be 999001, meaning the maximum value used on the ''x''-axis is <code>xVals(999001)</code> or approximately 2.994 instead of the 3 in the original.  If that is a major concern, you should make sure the end of your refined data scale can be "reached" if your incremented indices start with 1 and have some increment.  As an example, choosing to have 1000001 points in the original instead of 1000000 means going from 1 by 1e3 to 10000001 will actually end at the 10000001st, and last, point.
+
The advantage of the ''second'' version, then, is that you are using prior data and thus do not have to recalculate anything.  The disadvantage is, you might miss out on the tail end of the data.  In the above example, slicing from the initial value to the end by taking every 1000 means the last value copied over will be 999001st (in index 990000).  This means the maximum value used on the ''x''-axis is <code>xVals[999000]</code> or approximately 2.994 instead of the 3 in the original.  If that is a major concern, you should make sure the end of your refined data scale can be "reached" if your incremented indices start with 1 and have some increment.  As an example, choosing to have 1000001 points in the original instead of 1000000 means going from 0 by 1e3 to the end will actually end at the 1000000th, and last, index.
  
 
== Putting Text on a Plot ==
 
== Putting Text on a Plot ==
The <code>text(x, y, MyText)</code>  
+
The <code>ax.text(x, y, MyText)</code>  
 
command will plot the text string <code>MyText</code> at  
 
command will plot the text string <code>MyText</code> at  
 
the (''x'',''y'') coordinate. There are several important aspects to the <code>text</code>  
 
the (''x'',''y'') coordinate. There are several important aspects to the <code>text</code>  
Line 466: Line 489:
  
 
First, the text command will ''not'' make the figure change its axes  
 
First, the text command will ''not'' make the figure change its axes  
to fit the data automatically. Clear the figure using <code>clf</code> then  try typing:
+
to fit the data automatically. Try typing:
<source lang="matlab">
+
<syntaxhighlight lang="python">
text([1; 2; 3], [1; 2; 3], ['a'; 'b'; 'c']);
+
fig = plt.figure(num=1, clear=True)
</source>
+
ax = fig.add_subplot(1, 1, 1)
and you will get the graph:
+
ax.text(2, 2, 'Hi')
<center>
+
</syntaxhighlight >
[[Image:Text1.png|400px]]
+
The figure will try to show the word Hi in the top right but it will not be in the axis limits.
</center>
+
You need to change the axis limits by using the <code>axis</code> command.  In this case,
Note that the letter <code>a</code> is just visible at the top right corner
+
<syntaxhighlight lang="python">
but <code>b</code> and <code>c</code>are totally missing. You need to manually
+
ax.axis((0, 3, 1, 4))
changes the axes by using the <code>axis</code> command.  In this case,
+
ax.text(2, 3, 'Hi 2')
<source lang="matlab">
+
</syntaxhighlight >
axis([0 4 -1 5])
+
will make the ''x'' axis go from 0 to 3 and the ''y''
</source>
+
axis go from 1 to 4, more than enough space to say "Hi 2" at (2,3)!
will make the ''x'' axis go from 0 to 4 and the ''y''
 
axis go from -1 to 5, more than enough space to see all three letters:
 
<center>
 
[[Image:Text2.png|400px]]
 
</center>
 
  
 
Next, note that  
 
Next, note that  
text placed on plots stays until the next plotting command is
+
text placed on plots will stay until the axes are cleared, so you can use multiple <code>ax.text</code> commands to get several
issued, so you can use multiple <code>text</code> commands to get several
+
labels on one plot.  You can also use the '''fig''' object to place text - in this case, the coordinates for the ''x'' and ''y'' values will be numbers between 0 and 1 where 0,0 is the bottom left of the figure and 1,1 is the top right.
labels on one plot.  Also, the arguments can be column vectors which
 
allows multiple labels to be added at once (such as in the above
 
example).  Note that <code>row vectors</code> do not work.  Clear the figure
 
using <code>clf</code> then try the following:
 
<source lang="matlab">
 
text([1 2 3], [1 2 3], ['a' 'b' 'c']);
 
axis([0 4 -1 5]);
 
</source>
 
This gives you:
 
<center>
 
[[Image:Text3.png|400px]]
 
</center>
 
Because you gave rows of labels instead of columns, <code>text</code> puts the
 
letters <code>abc</code> at each data point.  In this case, you can solve the problem by transposing the third argument.
 
 
 
Also, the text must be...text.  If you try to label using numbers you
 
will get an error.  Try
 
<source lang="matlab">
 
text([1; 2; 3], [1; 2; 3], [1; 2; 3]);
 
axis([0 4 -1 5]);
 
</source>
 
Fortunately, MATLAB has a command called <code>num2str</code> that will
 
convert numbers to strings.  Clear the figure and try
 
<source lang="matlab">
 
text([1; 2; 3], [1; 2; 3], num2str([1; 2; 3]));
 
axis([0 4 -1 5]);
 
</source>
 
and you will get
 
<center>
 
[[Image:Text4.png|400px]]
 
</center>
 
  
Finally, if you put multiple pieces of text on at once, they must all be
+
== Updates / Comments ==
the same size.  You can ensure that they are by using the <code>char</code> command. Clear the figure and try:
+
===Clearing Figures ===
<source lang="matlab">
+
The <code>clear</code> kwarg in <code>plt.figure()</code> or <code>plt.subplots()</code> does not always seem to work with older versions of matplotlib.  Sometimes, future calls to <code>plt.subplots()</code> and <code>fig.tight_layout()</code> will cause the active area of the plot to become smaller and also will cause the axes to be redrawn without removing the old axesIf you have that problem, you will need to replace
text([1; 2; 3], [1; 2; 3], char('one', 'two', 'three'))
+
<syntaxhighlight  lang=python>
axis([0 4 -1 5]);
+
fig = plt.figure(num=1, clear=True)
</source>
+
</syntaxhighlight>
The <code>char</code> command will automatically pad the shorter words with spaces, and the <code>text</code> command will give you:
+
with
<center>
+
<syntaxhighlight lang=python>
[[Image:Text5.png|400px]]
+
fig = plt.figure(num=1)
</center>
+
fig.clf()
 
+
</syntaxhighlight>
== Setting Limits and Tick Locations ==
+
to explicitly run the <code>clf</code> method on the new (or old) figure.
[[File:PalmFig050204plot.png|thumb|300px]]You can set the specific limits and tick locations for a graph such that they will not change when you resize the figure window and so that they print exactly as you see on the screenTo do this, you will use the <code>set</code> command with some specific arguments. The following example is modified from Palm, Chapter 5.2, on page 231 to create the specific figure as shown in the book:
 
 
 
<source lang=matlab>
 
% Code based on code from:
 
% Palm, Introduction to MATLAB for Engineers
 
% Chapter 5.2, p. 231
 
 
 
%% Initialize workspace and figure
 
clear, figure(1), clf
 
 
 
%% Original code (mostly)
 
x = -1:0.01:1;
 
y1 = 3+exp(-x).*sin(6*x);
 
y2 = 4+exp(-x).*cos(6*x);
 
plot((0.1+0.9i).^(0:0.01:10))
 
hold on
 
plot(y1, y2)
 
hold off
 
 
 
%% Add text and adjust limits and ticks
 
%  Add text at specific locations, versus gtext
 
text(2.2, 5.7, 'y2 versus y1')
 
text(1.1, 0.3, 'Imag(z) versus Real(z)')
 
%  Get current axis (gca) and change limits and ticks
 
set(gca, 'XLim', [-1 6],  'YLim', [-1 7],...
 
        'XTick', -1:1:6, 'YTick', -1:1:7)
 
 
 
%% Print figure   
 
print -deps PalmFig050204plot</source>
 
 
 
[[File:PalmFig050204plot2.png|thumb|300px]]
 
You can also specify what goes at each tick mark - either by giving a vector of values or a cell array of strings. Changing the <code>set</code> command as given below produces the graph at right:
 
<source lang=matlab>
 
set(gca, 'XLim', [-1 6],  'YLim', [-1 7],...
 
        'XTick', -1:1:6, 'YTick', -1:1:7,...
 
        'XTickLabel',...
 
        {'eight', 'things', 'must', 'go', 'in', 'here', 'to', 'work'}, ...
 
        'YTickLabel', ...
 
        roundn(linspace(pi, 2*pi, 9), -2))
 
</source>
 
where the <code>round(x,n)</code> command rounds <math>x</math> to the nearest <math>10^n</math>.
 
-->
 
  
 
== Questions ==
 
== Questions ==

Latest revision as of 13:52, 17 October 2022

This page will primarily go over some examples of different ways to plot data sets. It assumes the lines

import math as m
import numpy as np
import matplotlib.pyplot as plt

are in the program.

Introduction

For a good tutorial on using the matplotlib.pyplot module - especially the object-oriented approach, see Python Plotting With Matplotlib (Guide) by Brad Solomon as recommended by Dr. Mark Palmeri, Duke BME.

Note that commands given as plt.COMMAND() may sometimes apply to either a figure or a set of axes or both. In November of 2019, plot infrastructure on this page was formalized to independently create the figure and one or more handles to axes. The primary reason for this is that the formal method is required in order to create 3D plots and so practicing it with 2D plots will prepare programmers for all cases.

Notes

In Fall of 2022, it looks like figures may not update correctly unless (a) saved, (b) resized, (c) explicitly told to update, or (d) interactive plotting is turned on. That is:

  • Saving the figure with
fig.savefig("FILE.EXT")
  • Resizing the window using the mouse
  • Explicitly updating with
fig.canvas.draw()
  • Turning interactive plotting on with
plt.ion()

This is with Spyder 5.2.2.

Simple Examples

If you are here to see some basic ways to get plots to work, this is the section for you! The main steps for creating most (relatively simple) plots are:

  • Import appropriate modules
  • Create figure and axis handles
  • Make plot
  • Add title, labels, etc.
  • Save the plot

Note also that depending on your development environment, graphics may show up in their own window or in the console. These examples generally assume you are having graphics show up in their own window - it makes it much easier to make alterations to graphs after their initial creation. If you are using Spyder, the way to get graphs to show up in their own window is:

  • Within the Tools menu select Preferences
  • From the Preferences list on the left select IPython console
  • In the right window, click the Graphics tab
  • In the second section (Graphics backend), select Automatic
  • Click the OK button at the bottom right of the window.

Plotting a few points

Here is a complete example that will generate and save a single figure window with a single set of axes within it:

Example1 PunPlot.png
# Imports
import matplotlib.pyplot as plt
# Data 
x = [1, 2, 4]
y = [2, 1, 5]
# Set up figure and axis
fig = plt.figure(num=1, clear=True)
ax = fig.add_subplot(1, 1, 1)
# Plot using red circles
ax.plot(x, y, 'ro')
# Set labels and turn grid on
ax.set(xlabel='x', ylabel='y', title='Three Points')
ax.grid(True)
# Use space most effectively
fig.tight_layout()
# Save as a PNG graphics file
fig.savefig('Example1.png')

Note that the plt.figure(num=1, clear=True) line does not work correctly in some versions of matplotlib - the way you will detect the problem is if you re-run the code and there either looks to be two different plots on top of each other or your current plots takes up less of the screen and the text gets dark. If that happens to you, look at the fix in the #Clearing_Figures section below.

Plotting a couple lines

This example uses the numpy module to create an array and apply trig and basic math to that array. It also shows how to use the label kwarg in concert with the legend method to produce a legend. Finally, notice in the ax.set() method that there is an r in front of the ylabel text. If you want to use basic \(\LaTeX\) commands in labels, titles, or text, you need to pass them in raw strings. Finally, the markevery command allwys you to specify how often to put a data marker when plotting a data set.

Example2 PunPlot.png
# Imports
import matplotlib.pyplot as plt
import numpy as np
# Data 
t = np.linspace(0, 2*np.pi, 101)
x1 = 1 + 2 * np.cos(t)
x2 = 2 + 1 * np.sin(t)
# Set up figure and axis
fig = plt.figure(num=1, clear=True)
ax = fig.add_subplot(1, 1, 1)
# Plot using red circles
ax.plot(t, x1, 'gs-', label='First Line', markevery=10)
ax.plot(t, x2, 'bd:', label='Second Line', markevery=20)
# Set labels and turn grid on
ax.set(xlabel='Time $t$, sec', ylabel=r'Value $\xi$, m', title='Two Lines')
ax.grid(True)
ax.legend(loc='best')
# Use space most effectively
fig.tight_layout()
# Save as a PNG file
fig.savefig('Example2.png')

The plt.plot() Function

The plt.plot() function is used to plot sets of data on a 2-D grid. What follows comes from matplotlib.pyplot's help function (some paragraphs have been snipped out). The line styles, symbols, and colors are formatted as a clearer table.

plot(*args, **kwargs)
    Plot lines and/or markers to the
    :class:`~matplotlib.axes.Axes`.  *args* is a variable length
    argument, allowing for multiple *x*, *y* pairs with an
    optional format string.  For example, each of the following is
    legal::
    
        plot(x, y)        # plot x and y using default line style and color
        plot(x, y, 'bo')  # plot x and y using blue circle markers
        plot(y)           # plot y using x as index array 0..N-1
        plot(y, 'r+')     # ditto, but with red plusses
    
    If *x* and/or *y* is 2-dimensional, then the corresponding columns
    will be plotted.
    
    If used with labeled data, make sure that the color spec is not
    included as an element in data, as otherwise the last case
    ``plot("v","r", data={"v":..., "r":...)``
    can be interpreted as the first case which would do ``plot(v, r)``
    using the default line style and color.
    
    If not used with labeled data (i.e., without a data argument),
    an arbitrary number of *x*, *y*, *fmt* groups can be specified, as in::
    
        a.plot(x1, y1, 'g^', x2, y2, 'g-')
    
    Return value is a list of lines that were added.
    
    By default, each line is assigned a different style specified by a
    'style cycle'.  To change this behavior, you can edit the
    axes.prop_cycle rcParam.
    
    The following format string characters are accepted to control
    the line style or marker (and) the following color abbreviations are supported:
Line code Meaning
- solid
: dotted
-. dash-dot
-- dashed
Symbol code Meaning Symbol code Meaning
. point s square
, pixel p pentagon
o circle * star
v triangle (down) h hexagon 1
^ triangle (up) H hexagon 2
< triangle (left) + plus
> triangle (right) x x-mark
1 tri (down) D diamond
2 tri (up) d thin diamond
3 tri (left) vline
4 tri (right) _ hline
Color code Meaning
b blue
g green
r red
c cyan
m magenta
y yellow
k black
w white
    
    In addition, you can specify colors in many weird and
    wonderful ways, including full names (``'green'``), hex
    strings (``'#008000'``), RGB or RGBA tuples (``(0,1,0,1)``) or
    grayscale intensities as a string (``'0.8'``).  Of these, the
    string specifications can be used in place of a ``fmt`` group,
    but the tuple forms can be used only as ``kwargs``.
    
    Line styles and colors are combined in a single format string, as in
    ``'bo'`` for blue circles.
    
    The *kwargs* can be used to set line properties (any property that has
    a ``set_*`` method).  You can use this to set a line label (for auto
    legends), linewidth, anitialising, marker face color, etc.  Here is an
    example::
    
        plot([1,2,3], [1,2,3], 'go-', label='line 1', linewidth=2)
        plot([1,2,3], [1,4,9], 'rs',  label='line 2')
        axis([0, 4, 0, 10])
        legend()
    
    If you make multiple lines with one plot command, the kwargs
    apply to all those lines, e.g.::
    
        plot(x1, y1, x2, y2, antialiased=False)
    
    Neither line will be antialiased.
    
    You do not need to use format strings, which are just
    abbreviations.  All of the line properties can be controlled
    by keyword arguments.  For example, you can set the color,
    marker, linestyle, and markercolor with::
    
        plot(x, y, color='green', linestyle='dashed', marker='o',
             markerfacecolor='blue', markersize=12).

The plt.savefig() Function

The plt.savefig() function is used to save a plot to a file. The type of file is determined by the extension of the file name. For example,

plt.savefig('blah.png')

will save the file to a portable network graphics file, while

plt.savefig('blah.pdf')

will save it to a PDF. From the help file for the command, "Most backends support png, pdf, ps, eps and svg." If you have created a figure handle, you should use the safefig on that handle - that is, use

fig.savefig('blah.png')

instead of using plt.

Example

Example showing a figure split into 2x3 subplots with subplot 3 holding an empty axis and subplot 4 holding a graph of the unit step function.

The following Python code demonstrates how to fill the fourth window of a 2x3 plot figure and save the figure as a PNG file; the resulting figure is in the thumbnail at right.

# Import required modules
import numpy as np
import matplotlib.pyplot as plt

# Create a 100 numbers between -1 and 1
x = np.linspace(-1, 1, 100); 

# This formula for usf is one of several ways to define the unit step function
def usf(t):
    return (t>=0)*1.0; 

# Create a figure with two rows of three figures
fig = plt.figure(num=1, clear=True)
ax = fig.subplots(2, 3)

# Plot the values of the function usf(x) against the vector x in bottom left
ax[1][0].plot(x, usf(x)) 

# Change the axes so the function is not covered by the subplot box
ax[1][0].axis([-1, 1, -1, 2])

# Set labels and title
ax[1][0].set(title='Unit Step Function', xlabel='x', ylabel='u(x)')

# Clear the rest other than bottom left and top right
fig.delaxes(ax[0][0])
fig.delaxes(ax[0][1])
fig.delaxes(ax[1][1])
fig.delaxes(ax[1][2])

# Issue tight layout to fix label overlaps
fig.tight_layout()

# Send the current figure to a file named usfplot.png
fig.savefig('usfplot.png')

Example Redux

Example showing a figure split into 2x3 subplots with subplot 3 holding an empty axis and subplot 4 holding a graph of the unit step function.

The following Python code demonstrates how to fill the fourth window of a 2x3 plot figure and save the figure as a PNG file; the resulting figure is in the thumbnail at right. In this case, instead of creating 6 subplots and deleting four of them, the code below only adds the subplots we want.

# Import required modules
import numpy as np
import matplotlib.pyplot as plt

# Create a 100 numbers between -1 and 1
x = np.linspace(-1, 1, 100); 

# This formula for usf is one of several ways to define the unit step function
def usf(t):
    return (t>=0)*1.0; 

# Create a figure 
fig = plt.figure(num=1, clear=True)

# Create axes for panels 3 and 4 in a 2x3 configuration
ax3 = fig.add_subplot(2, 3, 3)
ax4 = fig.add_subplot(2, 3, 4)

# Plot the values of the function usf(x) against the vector x in bottom left
ax4.plot(x, usf(x)) 

# Change the axes so the function is not covered by the subplot box
ax4.axis([-1, 1, -1, 2])

# Give the current subplot a title
ax4.set_title('Unit Step Function')

# Set the x-label
ax4.set_xlabel('x')

# Set the y-label
ax4.set_ylabel('u(x)')

# Issue tight layout to fix label overlaps
fig.tight_layout()

# Send the current figure to a file named usfplot.png
fig.savefig('usfplot.png')

Setting Up Subplots

See Python:Plotting/Subplots for more on setting up subplots.

Python Settings

For this course, you will generally want to have your graphics set to automatic; to make this change in Spyder:

  • Open the preferences window
    • On Windows, go to the Tools menu near the top right of the window and select Preferences
    • On MACs, go to the python menu near the top left of the window and select Preferences
  • In the Preferences window at the left select IPython console
  • In the right half of the Preferences window, select the Graphics tab
  • In the Backend pulldown, select Automatic
  • Click OK in the Preferences window

General Plotting Tips

You must make sure that your data sets are presented properly. Here are some guidelines:

  • Include axis labels that have, when appropriate, units. You should also include a description of the variable, or the variable itself, or both. For example, on p. 285 of Chapra[1], there is a plot of force versus velocity. Appropriate x axis labels would include any of the following:
ax.set(xlabel='$v$, m/s')
ax.set(xlabel='$v$ (m/s)')
ax.set(xlabel='Velocity, m/s')
ax.set(xlabel='Velocity (m/s)')
ax.set(xlabel='Velocity ($v$), m/s')
ax.set(xlabel='Velocity ($v$, m/s)')
Note that you can use basic $$\LaTeX$$ commands (Greek letters, superscripts, etc) but if you do, you need to make sure the kwarg gets the raw version of the string; specifically, if you want to have the Greek letter $$\theta$$ and you try
ax.set(xlabel='$\theta(t)$, rad')
you will end up getting "heta(t), rad" since the "\t" is processed as a tab character. If you use:
ax.set(xlabel=r'$\theta(t)$, rad')
you will get the correct result.
  • You should be consistent within a single graph as far as which version of axis labels you use, and you should pick which format you believe conveys the information most efficiently and effectively.
  • Make sure your titles make sense. Typically, the title will read "DEP. VAR vs. INDEP. VAR for DESCRIPTION" where DEP. VAR is your dependent variable (typically on the y axis), INDEP. VAR is your independent variable (typically on the x axis), and DESCRIPTION is something that will differentiation this particular plot from others that might have the same variables (for example a data, or an experiment number).
  • Data points should be represented by symbols and model equations should be represented by lines. Be sure to use a legend to identify each item plotted if there are multiple data sets on the same graph.
  • Typically, you will want to set the axis limits such that no data point is on the figure boundary. Certainly you do not want a line to be plotted on top of a figure boundary. After you make a plot, if there is a data point on an edge, look at the current axes and go out a little bit. Just make sure if you end up fundamentally changing your code that you adjust the axes accordingly.

Using Different Line Styles

Most of the time, you will be plotting three or fewer different lines on a single window, and they can thus be distinguished from one another in Python by using different line styles: Note the legend command argument. The default location is upper right but that may land on the data. Telling it to use the best location will have Python look for the most-blank part of the plot. There are many other options for location - try help(plt.legend) and look at the section about loc in the Other Parameters.

Using Different Point Styles

Sometimes there will be more data sets on a graph than there are line styles in Python. In cases like this, you may think to use symbols at the points. The problem with that becomes clear if you have a large number of data points - you do not want to try to jam hundreds of symbols onto a curve. The left graph in the figure below shows what a disaster that could be. Instead, you can plot a line with all your data but then tell Python to plot points only every so often. This is done with the markevery kwarg. The right side of the figure shows the result of this operation.

Using Different Scales

In the above example, two different scales were used for the data sets - a refined scale for the line and a rougher scale for the data points themselves. In other cases, it is the line that will need the rougher scale. As an example, assume you want Python to numerically find the minimum of the function \(y=3x^2+11x-2\) using the built-in min command. To get the most precise answer possible, you will want to give Python a very large number of points - say, 1 million. That code might look like:

fun = lambda x: 3*x**2 + 11*x - 2
xVals = np.linspace(-3.0, 3.0, int(1e6))
yVals = fun(xVals)
yMin = min(yVals)
xMin = xVals[np.where(yVals==yMin)]
print(xMin, yMin)

Since the domain is 6 and there are 1e6 points, the spacing between points is approximately 6e-06. The x-coordinate of the answer then should be very close to the actual answer. In fact, Python determines that the minimum value is -1.208333333332658e+01 and its x-coordinate is -1.833334833334833e+00 - very close to finding the minimum of y=-1.208333333333333e+01 at x=-1.833333333333333e+00

The problem, however, is that if you want to graph this, one million points is somewhat absurd. Unless you are planning to zoom in on the graph, there are around 1000x as many points as necessary/realistic/useful for a plot like this. Instead, you should either recalculate the equation using a smaller set of independent data or plot only some of the data. Code for the former might be:

xPlot = np.linspace(-3, 3, 100)
yPlot = fun(xPlot)
fig = plt.figure(num=1, clear=True)
ax = fig.add_subplot(1, 1, 1)
ax.plot(xPlot, yPlot, 'k-')
ax.set(xlabel = 'x', ylabel = 'y', title = '$y=3x^2+11x-2$')
ax.grid(True)

while code for the latter might be:

fig.plot(xVals[::1000], yVals[::1000], 'k-')
ax.set(xlabel = 'x', ylabel = 'y', title = '$y=3x^2+11x-2$')
ax.grid(True)

The advantage of the first version above is that your domain definitely remains the same - the xPlot variable spans the same [-3, 3] as the xVals set even though it has three orders of magnitude fewer values. The disadvantage is that you will have to re-perform all the calculations on this new set.

The advantage of the second version, then, is that you are using prior data and thus do not have to recalculate anything. The disadvantage is, you might miss out on the tail end of the data. In the above example, slicing from the initial value to the end by taking every 1000 means the last value copied over will be 999001st (in index 990000). This means the maximum value used on the x-axis is xVals[999000] or approximately 2.994 instead of the 3 in the original. If that is a major concern, you should make sure the end of your refined data scale can be "reached" if your incremented indices start with 1 and have some increment. As an example, choosing to have 1000001 points in the original instead of 1000000 means going from 0 by 1e3 to the end will actually end at the 1000000th, and last, index.

Putting Text on a Plot

The ax.text(x, y, MyText) command will plot the text string MyText at the (x,y) coordinate. There are several important aspects to the text command you must remember to use it properly.

First, the text command will not make the figure change its axes to fit the data automatically. Try typing:

fig = plt.figure(num=1, clear=True)
ax = fig.add_subplot(1, 1, 1)
ax.text(2, 2, 'Hi')

The figure will try to show the word Hi in the top right but it will not be in the axis limits. You need to change the axis limits by using the axis command. In this case,

ax.axis((0, 3, 1, 4))
ax.text(2, 3, 'Hi 2')

will make the x axis go from 0 to 3 and the y axis go from 1 to 4, more than enough space to say "Hi 2" at (2,3)!

Next, note that text placed on plots will stay until the axes are cleared, so you can use multiple ax.text commands to get several labels on one plot. You can also use the fig object to place text - in this case, the coordinates for the x and y values will be numbers between 0 and 1 where 0,0 is the bottom left of the figure and 1,1 is the top right.

Updates / Comments

Clearing Figures

The clear kwarg in plt.figure() or plt.subplots() does not always seem to work with older versions of matplotlib. Sometimes, future calls to plt.subplots() and fig.tight_layout() will cause the active area of the plot to become smaller and also will cause the axes to be redrawn without removing the old axes. If you have that problem, you will need to replace

fig = plt.figure(num=1, clear=True)

with

fig = plt.figure(num=1)
fig.clf()

to explicitly run the clf method on the new (or old) figure.

Questions

Post your questions by editing the discussion page of this article. Edit the page, then scroll to the bottom and add a question by putting in the characters *{{Q}}, followed by your question and finally your signature (with four tildes, i.e. ~~~~). Using the {{Q}} will automatically put the page in the category of pages with questions - other editors hoping to help out can then go to that category page to see where the questions are. See the page for Template:Q for details and examples.

External Links

References