Creating Graphs with Matplotlib

45 minutes
  • 4 Learning Objectives

About this Hands-on Lab

Matplotlib is a popular and powerful library for plotting out information using Python. Since Python often works with datasets, and visually representing data can help demonstrate points within data, it makes sense that there is a useful Python library for just that. In this hands-on lab, we’ll utilize `matplotlib` to create graphs to display various pieces of information about Target stores opened in the United States.

_Warning_: This is a lab designed as part of a professional-level course and is difficult. The lab asks you to accomplish something using methods and functionality of the `matplotlib` library that might not have been covered in lessons.

To feel comfortable completing this lab, you’ll want to know how to do the following:

– Use Matplotlib. Watch the lessons in the “Using Matplotlib” section from this course.
– Be comfortable reading the [matplotlib documentation](https://matplotlib.org/) to find new functions and methods to use to accomplish your goal.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Install matplotlib

Please Note: Accessing VS Code Server in the browser will require a password. The password is system generated and will either be 24bdf4febea54a8579305316 or can be located on the host itself by logging in via SSH and viewing the config.yaml file.

  cat ~/.config/code-server/config.yaml

Within VS Code Server, you will have the opportunity to use the Jupyter Notebook service to render graphs. It will ask you for a password on first use. There is no password; simply place your cursor in the prompt box and press Enter/Return.

Before we can use matplotlib to generate graphs, we’ll need to make sure that it is installed.

Create a Bar Graph Showing Number of Stores by State

From within the target_graphs.py file, create a bar chart to display the number of Target stores in each state. It will be useful to use itertools.groupby to collect the information based on the state. It will likely be necessary to change the font size of the axis ticks so that the state abbreviations don’t overlap; this can be done with set_yticklabel and set_xticklabel (depending on how the graph is oriented.)

Create a Line Graph Showing New Stores Opened Each Year

From within the target_graphs.py file, create a line graph that plots the number of Target stores that were opened each year. A line graph works well for charting this because we can visualize the information without adding a new tick label to the axis for years.

Create a Line Graph Showing the Total Number of Target Stores Over Time

From within the target_graphs.py file, create a line graph that plots the total number of Target stores that are open over time. We’ll need to calculate the total for each year by using the total from the previous year and adding the number of new stores opened in a given year.

Additional Resources

We have a dataset containing information about the Target stores that have been opened in the United States. To get a better idea of where the stores are distributed and how they came to be over time, we've decided to create three charts and will do a good job of visualizing this information. Here are the three charts that we'd like to create:

  1. Number of Stores by State - A bar chart that displays the number of Target stores in each state.
  2. New Stores Opened by Year - A line plot that displays the number of Target stores opened each year. This could be done with a bar chart but there are too many years and it would be hard to display the information cleanly.
  3. Stores Opened Over Time - A line plot displaying the number of total Target stores over time.

To make it easier to get started, we've already written a target_graphs.py file that extracts the dataset into a list of dictionaries called targets and shows the shape of each item in the list. The development server has a Jupyter Notebook server running and can preview plots from within scripts if they are annotated using the #%% comment to run them as a cell. Alternatively, the plots can also be written out to files, downloaded (through scp), and viewed locally.

Logging In

There are a couple of ways to get in and work with the code. One is to use the credentials provided in the hands-on lab overview page, log in with SSH, and use a text editor in the terminal.

The other is using VS Code Server in the browser. If you'd like to go this route, then you will need to navigate to the public IP address of the workstation server (provided in the hands-on lab overview page) on port 9090 (example: http://PUBLIC_IP:9090).

Please Note: Accessing VS Code Server in the browser will require a password. The password is system generated and will either be 24bdf4febea54a8579305316 or it can be located on the host itself by logging in via SSH and viewing the config.yaml file.

  cat ~/.config/code-server/config.yaml

Within VS Code Server, you will have the opportunity to use the Jupyter Notebook service to render graphs. It will ask you for a password on first use. There is no password; simply place your cursor in the prompt box and press Enter/Return.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Get Started
Who’s going to be learning?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


$2,495.00

Checkout
Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!