Get Started

Importing matplotlib and pyplot

Pyplot is a collection of functions in the popular visualization package Matplotlib. Its functions manipulate elements of a figure, such as creating a figure, creating a plotting area, plotting lines, adding plot labels, etc. Let's use the plot() function from pyplot to create a dashed line graph showing the growth of a company's stock. Remember, you can change the color of the line by adding the argument color and the linestyle by adding the argument linestyle.

Two lists, days (representing the days since the company became public), and prices (representing the price of the stock corresponding to that day) are available in your workspace.

This is a part of the course

“Introduction to Python for Finance”

View Course

Exercise instructions

  • Selectively import the pyplot module of matplotlib as plt.
  • Plot days on the x-axis and prices on the y-axis as a red colored dashed line.
  • Display the plot with the show() function.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Import matplotlib.pyplot with the alias plt
import matplotlib.pyplot as ____

# Plot the price of stock over time
plt.plot(____, ____, color="____", linestyle="____")

# Display the plot
plt.____()

This exercise is part of the course

Introduction to Python for Finance

BeginnerSkill Level
4.7+
29 reviews

Build Python skills to elevate your finance career. Learn how to work with lists, arrays and data visualizations to master financial analyses.

In this chapter, you will be introduced to the Matplotlib package for creating line plots, scatter plots, and histograms.

Exercise 1: Visualization in PythonExercise 2: Importing matplotlib and pyplot
Exercise 3: Adding axis labels and titlesExercise 4: Multiple lines on the same plotExercise 5: ScatterplotsExercise 6: HistogramsExercise 7: What are applications of histograms in finance?Exercise 8: Is data normally distributed?Exercise 9: Comparing two histogramsExercise 10: Adding a legend

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free