Visualization in Python

Introduction to Python for Finance

Adina Howe

Instructor

Matplotlib: A visualization package

See more of the Matplotlib gallery by clicking this link.

matplotlib

Introduction to Python for Finance

matplotlib.pyplot - diverse plotting functions

import matplotlib.pyplot as plt
Introduction to Python for Finance

matplotlib.pyplot - diverse plotting functions

  • plt.plot()

    • takes arguments that describe the data to be plotted
  • plt.show()

    • displays plot to screen
Introduction to Python for Finance

Plotting with pyplot

import matplotlib.pyplot as plt
plt.plot(months, prices)
plt.show()
Introduction to Python for Finance

Plot result

Simple plot2

Introduction to Python for Finance

Red solid line

import matplotlib.pyplot as plt
plt.plot(months, prices, color = 'red')
plt.show()
Introduction to Python for Finance

Plot result

redline

Introduction to Python for Finance

Dashed line

import matplotlib.pyplot as plt
plt.plot(months, prices, color = 'red', linestyle = '--')
plt.show()
Introduction to Python for Finance

Plot result

dash

Introduction to Python for Finance

Colors and linestyles

color
'green' green
'red' red
'cyan' cyan
'blue' blue
linestyle
'-' solid line
'--' dashed line
'-.' dashed dot line
':' dotted

More documentation on colors and lines can be found here.

Introduction to Python for Finance

Adding Labels and Titles

import matplotlib.pyplot as plt
plt.plot(months, prices, color = 'red', linestyle = '--')

# Add labels
plt.xlabel('Months')
plt.ylabel('Consumer Price Indexes, $')
plt.title('Average Monthly Consumer Price Indexes')

# Show plot
plt.show()
Introduction to Python for Finance

Plot result

labeled_plot

Introduction to Python for Finance

Adding additional lines

import matplotlib.pyplot as plt
plt.plot(months, prices, color = 'red', linestyle = '--')

# adding an additional line
plt.plot(months, prices_new, color = 'green', linestyle = '--') 

plt.xlabel('Months')
plt.ylabel('Consumer Price Indexes, $')
plt.title('Average Monthly Consumer Price Indexes')
plt.show()
Introduction to Python for Finance

Plot result

twolines

Introduction to Python for Finance

Scatterplots

import matplotlib.pyplot as plt
plt.scatter(x = months, y = prices, color = 'red')
plt.show()
Introduction to Python for Finance

Scatterplot result

scatterplot

Introduction to Python for Finance

Let's practice!

Introduction to Python for Finance

Preparing Video For Download...