/* jquery */ /* jquery accordion style*/ /* jquery init */

Python Math - Function Plotting

Python Coding

Python benefits from an impressive set of graphing packages. However, many of the most popular ones tend to be focussed on the data visualisation side of things rather than mathematics.

When it comes to plotting math functions consider an exploraion of the functionality provided by the matplotlib package. This mature package works runs on a wide range of operating systems and has some math-centric advantages.

For instance, matplotlib includes the pyplot interface, namely matplotlib.pyplot, which is a collection of functions that make matplotlib work like MATLAB.

It also plays nicely with other math packages, including NumPy. The code below uses both matplotlib.pyplot and numpy to plot sine and cos curves:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)

plt.plot(x, np.sin(x))
plt.plot(x, np.cos(x))

Adding graph axis labels and a title is also sraightforward with pyplot, as shown below:

plt.xlabel('x values')
plt.ylabel('sin(x) and cos(x)')
plt.title('Plot of sin and cos')
plt.legend(['sin(x)', 'cos(x)'])
plt.show()

Importantly matplotlib has extensive collection of tutorial documentation pages covering the pyplot interface in addition to many other math-aligned applications, all with fully explained code examples.

In addition to the official documention a quick search of the web will unveil a plethora of matplotlib plotting code examples and links to diverse selection of real-world applications.

More Python Math Coding

No comments: