Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
SciPy Recipes

You're reading from   SciPy Recipes A cookbook with over 110 proven recipes for performing mathematical and scientific computations

Arrow left icon
Product type Paperback
Published in Dec 2017
Publisher Packt
ISBN-13 9781788291460
Length 386 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (3):
Arrow left icon
V Kishore Ayyadevara V Kishore Ayyadevara
Author Profile Icon V Kishore Ayyadevara
V Kishore Ayyadevara
Ruben Oliva Ramos Ruben Oliva Ramos
Author Profile Icon Ruben Oliva Ramos
Ruben Oliva Ramos
Luiz Felipe Martins Luiz Felipe Martins
Author Profile Icon Luiz Felipe Martins
Luiz Felipe Martins
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Getting to Know the Tools FREE CHAPTER 2. Getting Started with NumPy 3. Using Matplotlib to Create Graphs 4. Data Wrangling with pandas 5. Matrices and Linear Algebra 6. Solving Equations and Optimization 7. Constants and Special Functions 8. Calculus, Interpolation, and Differential Equations 9. Statistics and Probability 10. Advanced Computations with SciPy

Installing packages with pip

The main tool to set up packages in a standalone installation of Python 3 is the pip3 command. This command will fetch a package from the Python Package Index (PyPI), a standard public repository located at https://pypi.python.org/pypi.

PyPI is somewhat hard to navigate due to the large number of packages from different application areas that are available. A list restricted to packages related to SciPy is available at: https://www.scipy.org/topical-software.html.

The existence of a uniform package manager makes it is straightforward to add and remove packages to a Python installation.

Python has a standard package distribution system, documented at the site https://packaging.python.org.
The procedures outlined in this site are the preferred methods to distribute Python software. This is recommended even in the case of software intended for internal use in an institution.
If you are using Anaconda, the preferred method for installing packages is to use conda, as explained in the previous section. Only use pip3 if the package is not available in Anaconda.

How to do it...

As an example, let's say we want to install the Bokeh package, located at http://bokeh.pydata.org/.

Bokeh is a visualization library that targets modern browsers for presentation. It produces high-quality plots in a variety of formats for inclusion in web pages. To install Bokeh in macOS or Linux, all you need to do is to run the following command in a Terminal window:

pip3 install bokeh

To install on Windows, run the following command at the Command Prompt:

pip install bokeh

This will remotely access PyPI, fetch Bokeh and all its dependencies, build the package, and make it accessible in the current Python 3 installation. To test the package, use a text editor to enter the following code in the bokeh_test.py file:

import numpy as np
from bokeh.plotting import figure, show

xvalues = np.linspace(-np.pi, np.pi, 50)
y1values = np.sin(xvalues)
y2values = np.cos(xvalues)

p = figure(
tools='pan,box_zoom,reset',
title='Trigonometric functions',
x_axis_label='x',
y_axis_label='y'
)

p.line(xvalues, y1values,
legend='y=sin(x)', line_color='blue')
p.circle(xvalues, y2values,
legend='y=cos(x)', fill_color='green',
line_color='green', size=3)

show(p)

print('Plot generated. Open file bokeh_test.html in your browser.')

This code uses the np.sin() and np.cos() NumPy functions to generate data points for the trigonometric functions sin(x) and cos(x). After that, it constructs a Bokeh figure object and adds two plots to the figure with the p.line() and p.circle() functions. Finally, the figure is displayed with a call to show(p).

Save the file in a convenient folder and open a Terminal window from the same folder. Run the following statement at the command line:

python3 bokeh_test.py

Running the script will create an output file named bokeh_test.html, with an HTML code that generates the figure described in the Python script. If you have a default browser defined for your system, Bokeh will automatically open the HTML file in the browser. Otherwise, you will need to open the file in the browser manually.

Notice that the graph displays control buttons at the top right. By clicking on the Pan button, the user can drag the graphed region with the mouse. With the Box Zoom tool, it is possible to draw a box on the graph, and the plot will be zoomed in on that box. Finally, Reset brings the figure back to its original state.

Let's say that Bokeh does not satisfy your needs and you do not want to keep it in your Python installation. The package can be easily uninstalled by issuing the following command in the Terminal, for Linux and macOS:

pip3 uninstall bokeh

On Windows, use the following command:

pip uninstall bokeh

The uninstaller will list the changes to be made and ask for confirmation. If you answer yes, the package and its dependencies will be removed from the system.

The pip3 package manager is a wonderful and well designed tool. However, it is a complex tool, since it has to carefully keep track of the changes and dependencies in the Python installation. Furthermore, it depends on developers to structure distributed packages with the correct dependencies. If all the reader wants is to test a package, I recommend the use of a virtual environment, as explained in the next section.
You have been reading a chapter from
SciPy Recipes
Published in: Dec 2017
Publisher: Packt
ISBN-13: 9781788291460
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at R$50/month. Cancel anytime