Book Image

matplotlib Plotting Cookbook

By : Alexandre Devert
Book Image

matplotlib Plotting Cookbook

By: Alexandre Devert

Overview of this book

Table of Contents (15 chapters)
matplotlib Plotting Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Making a user-controllable plot


Out of the box, without requiring any additional packages, matplotlib offers primitives to add controllers on a figure so that a user can interact with it. In this recipe, we are going to see how to plot a famous parametric curve: the SuperShape curve. This curve is controlled by six parameters: A, B, M, N1, N2, and N3. These parameters determine the shape of the curve. They can be set interactively by the user by moving the cursor on the figure.

How to do it...

The following code will display a curve using pyplot.plot(), which at this point should be simple. However, we now use user interface elements (more commonly called widgets), that is, sliders. This can be done with the following steps:

  1. We start with the necessary import directives as follows:

    import numpy as np
    from matplotlib import pyplot as plt
    from matplotlib.widgets import Slider
  2. The SuperShape curve is defined by the following function:

    def supershape_radius(phi, a, b, m, n1, n2, n3):
      theta = .25...