Search icon
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Matplotlib 3.0 Cookbook

You're reading from  Matplotlib 3.0 Cookbook

Product type Book
Published in Oct 2018
Publisher Packt
ISBN-13 9781789135718
Pages 676 pages
Edition 1st Edition
Languages
Authors (2):
Srinivasa Rao Poladi Srinivasa Rao Poladi
Profile icon Srinivasa Rao Poladi
Nikhil Borkar Nikhil Borkar
Profile icon Nikhil Borkar
View More author details

Table of Contents (17) Chapters

Preface 1. Anatomy of Matplotlib 2. Getting Started with Basic Plots 3. Plotting Multiple Charts, Subplots, and Figures 4. Developing Visualizations for Publishing Quality 5. Plotting with Object-Oriented API 6. Plotting with Advanced Features 7. Embedding Text and Expressions 8. Saving the Figure in Different Formats 9. Developing Interactive Plots 10. Embedding Plots in a Graphical User Interface 11. Plotting 3D Graphs Using the mplot3d Toolkit 12. Using the axisartist Toolkit 13. Using the axes_grid1 Toolkit 14. Plotting Geographical Maps Using Cartopy Toolkit 15. Exploratory Data Analysis Using the Seaborn Toolkit 16. Other Books You May Enjoy

Working in interactive mode

Matplotlib can be used in an interactive or non-interactive modes. In the interactive mode, the graph display gets updated after each statement. In the non-interactive mode, the graph does not get displayed until explicitly asked to do so.

Getting ready

You need working installations of Python, NumPy, and Matplotlib packages.

Using the following commands, interactive mode can be set on or off, and also checked for current mode at any point in time:

  • matplotlib.pyplot.ion() to set the interactive mode ON
  • matplotlib.pyplot.ioff() to switch OFF the interactive mode
  • matplotlib.is_interactive() to check whether the interactive mode is ON (True) or OFF (False)

How to do it...

Let's see how simple it is to work in interactive mode:

  1. Set the screen output as the backend:
%matplotlib inline
  1. Import the matplotlib and pyplot libraries. It is common practice in Python to import libraries with crisp synonyms. Note plt is the synonym for the matplotlib.pyplot package:
import matplotlib as mpl
import matplotlib.pyplot as plt
  1. Set the interactive mode to ON:
plt.ion()
  1. Check the status of interactive mode:
mpl.is_interactive()
  1. You should get the output as True.
  2. Plot a line graph:
plt.plot([1.5, 3.0])

You should see the following graph as the output:

  1. Now add the axis labels and a title to the graph with the help of the following code:
# Add labels and title
plt.title("Interactive Plot") #Prints the title on top of graph
plt.xlabel("X-axis") # Prints X axis label as "X-axis"
plt.ylabel("Y-axis") # Prints Y axis label as "Y-axis"

After executing the preceding three statements, your graph should look as follows:

How it works...

So, this is how the explanation goes:

  • plt.plot([1.5, 3.0]) plots a line graph connecting two points (0, 1.5) and (1.0, 3.0).
  • The plot command expects two arguments (Python list, NumPy array or pandas DataFrame) for the x and y axis respectively.
  • If only one argument is passed, it takes it as y axis co-ordinates and for x axis co-ordinates it takes the length of the argument provided.
  • In this example, we are passing only one list of two points, which will be taken as y axis coordinates.
  • For the x axis, it takes the default values in the range of 0 to 1, since the length of the list [1.5, 3.0] is 2.
  • If we had three coordinates in the list for y, then for x, it would take the range of 0 to 2.
  • You should see the graph like the one shown in step 6.
  • plt.title("Interactive Plot"), prints the title on top of the graph as Interactive Plot.
  • plt.xlabel("X-axis"), prints the x axis label as X-axis.
  • plt.ylabel("Y-axis"), prints the y axis label as Y-axis.
  • After executing preceding three statements, you should see the graph as shown in step 7.

If you are using Python shell, after executing each of the code statements, you should see the graph getting updated with title first, then the x axis label, and finally the y axis label.

If you are using Jupyter Notebook, you can see the output only after all the statements in a given cell are executed, so you have to put each of these three statements in separate cells and execute one after the other, to see the graph getting updated after each code statement.

In older versions of Matplotlib or certain backends (such as macosx), the graph may not be updated immediately. In such cases, you need to call plt.draw() explicitly at the end, so that the graph gets displayed.

There's more...

You can add one more line graph to the same plot and go on until you complete your interactive session:

  1. Plot a line graph:
plt.plot([1.5, 3.0])
  1. Add labels and title:
plt.title("Interactive Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
  1. Add one more line graph:
plt.plot([3.5, 2.5])

The following graph is the output obtained after executing the code:

Hence, we have now worked in interactive mode.

You have been reading a chapter from
Matplotlib 3.0 Cookbook
Published in: Oct 2018 Publisher: Packt ISBN-13: 9781789135718
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}