Matplotlibgig
Matplotlibgig
In this
tutorial, we will discuss how to create line plots, bar plots, and scatter plots in
Matplotlib using stock market data in 2022. These are the foundational plots that will
allow you to start understanding, visualizing, and telling stories about data. Data
visualization is an essential skill for all data analysts and Matplotlib is one of the most
popular libraries for creating visualizations.
This tutorial expects some basic prior knowledge in NumPy arrays and pandas
dataframes. When we use those libraries, we will quickly explain what we are doing.
The main focus of this tutorial is Matplotlib, which works on top of these data
structures to create visualizations.
Matplotlib is very flexible and customizable for creating plots. It does require a lot of
code to make more basic plots with little customizations. When working in a setting
where exploratory data analysis is the main goal, requiring many quickly drawn plots
without as much emphasis on aesthetics, the library seaborn is a great option as it
builds on top of Matplotlib to create visualizations more quickly. Please see
our Python Seaborn Tutorial For Beginners instead if exploratory data analysis or
quick and easy graph creation is your main priority.
Matplotlib Examples
By the end of this tutorial, you will be able to make great-looking visualizations in
Matplotlib. We will focus on creating line plots, bar plots, and scatter plots. We will also
focus on how to make customization decisions, such as the use of color, how to label
plots, and how to organize them in a clear way to tell a compelling story.
The Dataset
Matplotlib is designed to work with NumPy arrays and pandas dataframes. The library
makes it easy to make graphs from tabular data. For this tutorial, we will use the Dow
Jones Industrial Average (DJIA) index’s historical prices from 2022-01-01 to 2022-12-
31 (found here). You can set the date range on the page and then click the “download a
spreadsheet” button.
We will load in the csv file, named HistoricalPrices.csv using the pandas library and view the
first rows using the .head() method.
import pandas as pd
djia_data = pd.read_csv('HistoricalPrices.csv')
djia_data.head()
OpenAI
We see the data include 4 columns, a Date, Open, High, Low, and Close. The latter 4 are
related to the price of the index during the trading day. Below is a brief explanation of
each variable.