10.12.2019 Views

Python for Finance

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 2

Similarly, we could save all the functions to a vector (array); see the following code:

>>>import scipy as sp

>>> x=dir(sp)

>>> len(x)

588

>>>

Introduction to matplotlib

Graphs and other visual representations have become more important in explaining

many complex financial concepts, trading strategies, and formulas.

In this section, we discuss the matplotlib module, which is used to create various

types of graphs. In addition, the module will be used intensively in Chapter 10,

Options and Futures, when we discuss the famous Black-Scholes-Merton option model

and various trading strategies. The matplotlib module is designed to produce

publication-quality figures and graphs. The matplotlib module depends on NumPy

and SciPy, which were discussed in the previous sections. To save generated graphs,

there are several output formats available, such as PDF, Postscript, SVG, and PNG.

How to install matplotlib

If Python was installed by using the Anaconda super package, then matplotlib is

preinstalled already. After launching Spyder, type the following line to test. If there

is no error, it means that we have imported/uploaded the module successfully. This

is the beauty of using a super package such as Anaconda:

>>> import matplotlib

To install the matplotlib module or other modules independently, see the Module

dependency – how to install a module section.

Several graphical presentations using

matplotlib

The best way to understand the usage of the matplotlib module is through

examples. The following example could be the simplest one since it has just

three lines of Python code. The objective is to link several points. By default, the

matplotlib module assumes that the x axis starts at zero and moves by one on every

element of the array.

[ 45 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!