Visualization in Python

Introduzione a Python per la finanza

Adina Howe

Instructor

Matplotlib: A visualization package

See more of the Matplotlib gallery by clicking this link.

matplotlib

Introduzione a Python per la finanza

matplotlib.pyplot - diverse plotting functions

import matplotlib.pyplot as plt
Introduzione a Python per la finanza

matplotlib.pyplot - diverse plotting functions

  • plt.plot()

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

    • displays plot to screen
Introduzione a Python per la finanza

Plotting with pyplot

import matplotlib.pyplot as plt
plt.plot(months, prices)
plt.show()
Introduzione a Python per la finanza

Plot result

Simple plot2

Introduzione a Python per la finanza

Red solid line

import matplotlib.pyplot as plt
plt.plot(months, prices, color = 'red')
plt.show()
Introduzione a Python per la finanza

Plot result

redline

Introduzione a Python per la finanza

Dashed line

import matplotlib.pyplot as plt
plt.plot(months, prices, color = 'red', linestyle = '--')
plt.show()
Introduzione a Python per la finanza

Plot result

dash

Introduzione a Python per la finanza

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.

Introduzione a Python per la finanza

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()
Introduzione a Python per la finanza

Plot result

labeled_plot

Introduzione a Python per la finanza

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()
Introduzione a Python per la finanza

Plot result

twolines

Introduzione a Python per la finanza

Scatterplots

import matplotlib.pyplot as plt
plt.scatter(x = months, y = prices, color = 'red')
plt.show()
Introduzione a Python per la finanza

Scatterplot result

scatterplot

Introduzione a Python per la finanza

Let's practice!

Introduzione a Python per la finanza

Preparing Video For Download...