Book Image

NumPy Cookbook - Second Edition

By : Ivan Idris
Book Image

NumPy Cookbook - Second Edition

By: Ivan Idris

Overview of this book

<p>NumPy has the ability to give you speed and high productivity. High performance calculations can be done easily with clean and efficient code, and it allows you to execute complex algebraic and mathematical computations in no time.</p> <p>This book will give you a solid foundation in NumPy arrays and universal functions. Starting with the installation and configuration of IPython, you'll learn about advanced indexing and array concepts along with commonly used yet effective functions. You will then cover practical concepts such as image processing, special arrays, and universal functions. You will also learn about plotting with Matplotlib and the related SciPy project with the help of examples. At the end of the book, you will study how to explore atmospheric pressure and its related techniques. By the time you finish this book, you'll be able to write clean and fast code with NumPy.</p>
Table of Contents (19 chapters)
NumPy Cookbook Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Blurring images


We can blur images with a Gaussian filter (http://en.wikipedia.org/wiki/Gaussian_filter). This filter is based on the normal distribution. A corresponding SciPy function requires the standard deviation as a parameter. In this recipe, we will also plot a Polar rose and a spiral http://en.wikipedia.org/wiki/Polar_coordinate_system. These figures are not directly related, but it seemed more fun to combine them here.

How to do it...

We start by initializing the polar plots, after which we will blur the Lena image and plot using polar coordinates:

  1. Initialize the polar plots:

    NFIGURES = 5
    k = np.random.random_integers(1, 5, NFIGURES)
    a = np.random.random_integers(1, 5, NFIGURES)
    colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k']
    
  2. To blur Lena, apply the Gaussian filter with a standard deviation of 4:

    plt.subplot(212)
    blurred = scipy.ndimage.gaussian_filter(lena, sigma=4)
    
    plt.imshow(blurred)
    plt.axis('off')
    
  3. matplotlib has a polar() function, which plots in polar coordinates:

    theta = np.linspace...