How to do it…

Enter the following code in your text editor:

import numpy as np
import matplotlib
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt

f = lambda x: 1/((x-3)**2+.01) + 1/((x-9)**2+.04) - 6
xvalues = np.linspace(2, 10, 300)
yvalues = f(xvalues)

plt.plot(xvalues, yvalues)
plt.xlabel('$x$')
plt.ylabel('$f(x)$')
plt.title('The "humps" function')

plt.show()

print('Done plotting.')

Save the script to disk with the name backend_test.py and run it with the following command line:

python3 backend_test.py

Running the script will open a window displaying the plot shown in the following screenshot:

This example uses the Qt5Agg backend, which requires PyQt5. If you are using Anaconda or followed the installation instructions in Chapter 1, Getting to Know the Tools, you already have PyQt5. Otherwise, to make this example work, you will need to install it by running pip install pyqt5.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset