0% found this document useful (0 votes)
2 views31 pages

Chapter 4

The document provides an introduction to using Matplotlib for data visualization in Python, focusing on various plotting functions such as line plots, scatterplots, and histograms. It covers how to customize plots with colors, linestyles, labels, and legends, as well as techniques for analyzing data distributions through histograms. The content is structured as a tutorial for financial analysis, encouraging practice with the provided examples.

Uploaded by

Emily Camila
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views31 pages

Chapter 4

The document provides an introduction to using Matplotlib for data visualization in Python, focusing on various plotting functions such as line plots, scatterplots, and histograms. It covers how to customize plots with colors, linestyles, labels, and legends, as well as techniques for analyzing data distributions through histograms. The content is structured as a tutorial for financial analysis, encouraging practice with the provided examples.

Uploaded by

Emily Camila
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

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.

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

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

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

INTRODUCTION TO PYTHON FOR FINANCE


Colors and linestyles
color linestyle
'green' green '-' solid line
'red' red '--' dashed line
'cyan' cyan '-.' dashed dot line
'blue' blue ':' 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

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

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

INTRODUCTION TO PYTHON FOR FINANCE


Let's practice!
INTRODUCTION TO PYTHON FOR FINANCE
Histograms
INTRODUCTION TO PYTHON FOR FINANCE

Adina Howe
Instructor
Why histograms for financial analysis?

INTRODUCTION TO PYTHON FOR FINANCE


Histograms and Data
Is your data skewed?
Is your data centered around the average?

Do you have any abnormal data points (outliers) in your data?

INTRODUCTION TO PYTHON FOR FINANCE


Histograms and matplotlib.pyplot
import matplotlib.pyplot as plt
plt.hist(x=prices, bins=3)
plt.show()

INTRODUCTION TO PYTHON FOR FINANCE


Changing the number of bins
import matplotlib.pyplot as plt
plt.hist(prices, bins=6)
plt.show()

INTRODUCTION TO PYTHON FOR FINANCE


Normalizing histogram data
import matplotlib.pyplot as plt
plt.hist(prices, bins=6, normed=1)
plt.show()

INTRODUCTION TO PYTHON FOR FINANCE


Layering histograms on a plot
import matplotlib.pyplot as plt
plt.hist(x=prices, bins=6, normed=1)
plt.hist(x=prices_new, bins=6, normed=1)
plt.show()

INTRODUCTION TO PYTHON FOR FINANCE


Histogram result

INTRODUCTION TO PYTHON FOR FINANCE


Alpha: Changing transparency of histograms
import matplotlib.pyplot as plt
plt.hist(x=prices, bins=6, normed=1, alpha=0.5)
plt.hist(x=prices_new, bins=6, normed=1, alpha=0.5)
plt.show()

INTRODUCTION TO PYTHON FOR FINANCE


Histogram result

INTRODUCTION TO PYTHON FOR FINANCE


Adding a legend
import matplotlib.pyplot as plt
plt.hist(x=prices, bins=6, normed=1, alpha=0.5, label="Prices 1")
plt.hist(x=prices_new, bins=6, normed=1, alpha=0.5, label="Prices New")
plt.legend()
plt.show()

INTRODUCTION TO PYTHON FOR FINANCE


Histogram result

INTRODUCTION TO PYTHON FOR FINANCE


Let's practice!
INTRODUCTION TO PYTHON FOR FINANCE

You might also like