
上QQ阅读APP看书,第一时间看更新
How to do it...
Let's see how simple it is to work in interactive mode:
- Set the screen output as the backend:
%matplotlib inline
- 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
- Set the interactive mode to ON:
plt.ion()
- Check the status of interactive mode:
mpl.is_interactive()
- You should get the output as True.
- Plot a line graph:
plt.plot([1.5, 3.0])
You should see the following graph as the output:
- 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:
