Advanced and scientific Python

Logo

Materials to brush up your Python skills

View the Project on GitHub xoolive/pyclass

Matplotlib

This session is based on a Python files. When you open this file with Visual Studio Code, you will notice pseudo-cells prefixed as follows:

vscode ipython

Clicking on Run Cell (or pressing Maj+Enter) runs an IPython kernel appearing (usually) on the right side of your screen.

This approach is comfortable to preliminary projects:

Good practice    Consider this option before jumping to Jupyter notebooks when you start a new project. Notebooks are a natural choice for education materials, but they lack rigor in terms of software engineering and management.

The file is located in the python/ folder:

More exercices for Matplotlib

  1. Trace the graph of function $t \mapsto e^{-t} \cdot \cos(2\,\pi\,t)$ for $t\in[0,5]$

  2. Consider the polar coordinate curve defined by $r = \sin(5\,\theta)$, compute the x- and y- coordinates using Numpy functions, then plot the curve.

  3. Start from the following code, and edit it through the followings steps to reach the final design:

    
    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
    c, s = np.cos(x), np.sin(x)
    
    fig, ax = plt.subplots()
    (cos_plot,) = ax.plot(x, c)
    (sin_plot,) = ax.plot(x, s)
    
    
    • Remove the top and right spines

    • Set x-tick values for multiples of $\pi$

    • Set x labels with the $\pi$ symbol

    • Move the spines so that they cross at 0, 0

    • Adjust further the y-axis

    • Annotate some points

    • Try to imagine some trick to get the following rendering

  4. Take lessons from Exercice 3 to produce the following plot:

↑ Home | Next »