Matplotlib 3.0 Cookbook
上QQ阅读APP看书,第一时间看更新

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: