Python for Data Analysis
  • Intro
  • Python
    • Basics
    • Variables
    • Data Structure
      • Strings
      • Numbers
      • Lists
    • Functions
  • NumPy
    • Basics
    • Indexing and Slicing
    • Element-wise Operations
    • Math/Statistics Computation
  • Pandas
    • Basics
    • Create a DataFrame
    • Inspect a DataFrame
    • Select in DataFrame
    • Modify a DataFrames
    • Aggregate
  • Matplotlib
    • Basics
  • Jupyter Notebook
  • FAQ
Powered by GitBook
On this page
  • Basic Line Plot
  • Linestyles
  • Axis and Labels
  • Labeling the Axes
  • Subplots

Was this helpful?

  1. Matplotlib

Basics

PreviousAggregateNextFAQ

Last updated 5 years ago

Was this helpful?

The Python library Matplotlib contains the pyplot module, which provides users with an interface for graphing data. Pyplot contains over 100 functions, from acorr to yticks. You must import the module, and plt is the standard variable name used.

from matplotlib import pyplot as plt

Basic Line Plot

Line graphs are helpful for visualizing how a variable changes over time.

Using Matplotlib methods, the following code will create a simple line graph using .plot() and display it using .show() :

x_values = [0, 1, 2, 3, 4]
y_values = [0, 1, 4, 9, 16]
plt.plot(x_values, y_values)
plt.show()
  • x_values is a variable holding a list of x-values for each point on our line graph

  • y_values is a variable holding a list of y-values for each point on our line graph

  • plt is the name we have given to the Matplotlib module we have imported at the top of the code

  • plt.plot(x_values, y_values) will create the line graph

  • plt.show() will actually display the graph

Our graph would look like this:

We can also have multiple line plots displayed on the same set of axes.

# Days of the week:
days = [0, 1, 2, 3, 4, 5, 6]
# Your Money:
money_spent = [10, 12, 12, 10, 14, 22, 24]
# Your Friend's Money:
money_spent_2 = [11, 14, 15, 15, 22, 21, 12]
# Plot your money:
plt.plot(days, money_spent)
# Plot your friend's money:
plt.plot(days, money_spent_2)
# Display the result:
plt.show()

We then get two lines on the same plot:

By default, the first line is always blue, and the second line is always orange. In the next exercise, we’ll learn how to customize these lines ourselves.

Linestyles

plt.plot(days, money_spent, color='green')
plt.plot(days, money_spent_2, color='#AAAAAA')

We can also make a line dotted or dashed using the keyword linestyle.

# Dashed:
plt.plot(x_values, y_values, linestyle='--')
# Dotted:
plt.plot(x_values, y_values, linestyle=':')
# No line:
plt.plot(x_values, y_values, linestyle='')

We can also add a marker using the keyword marker:

# A circle:
plt.plot(x_values, y_values, marker='o')
# A square:
plt.plot(x_values, y_values, marker='s')
# A star:
plt.plot(x_values, y_values, marker='*')

Axis and Labels

Sometimes, it can be helpful to zoom in or out of the plot, especially if there is some detail we want to address. To zoom, we can use plt.axis(). We use plt.axis() by feeding it a list as input. This list should contain:

  1. The minimum x-value displayed

  2. The maximum x-value displayed

  3. The minimum y-value displayed

  4. The maximum y-value displayed

For example, if we want to display a plot from x=0 to x=3 and from y=2 to y=5, we would call plt.axis([0, 3, 2, 5]).

x = [0, 1, 2, 3, 4]
y = [0, 1, 4, 9, 16]
plt.plot(x, y)
plt.axis([0, 3, 2, 5])
plt.show()

Labeling the Axes

Eventually, we will want to show these plots to other people to convince them of important trends in our data. When we do that, we’ll want to make our plots look as professional as possible.

The first step towards a professional-looking plot is adding labels to the x-axis and y-axis, and giving the plot a title.

We can label the x- and y- axes by using plt.xlabel() and plt.ylabel(). The plot title can be set by using plt.title().

hours = [9, 10, 11, 12, 13, 14, 
        15, 16, 17, 18, 19, 20]
happiness = [9.8, 9.9, 9.2, 8.6, 8.3, 9.0, 
             8.7, 9.1, 7.0, 6.4, 6.9, 7.5]
plt.plot(hours, happiness)
plt.xlabel('Time of day')
plt.ylabel('Happiness Rating (out of 10)')
plt.title('My Self-Reported Happiness While Awake')
plt.show()

Subplots

Sometimes, we want to display two lines side-by-side, rather than in the same set of x- and y-axes. When we have multiple axes in the same picture, we call each set of axes a subplot. The picture or object that contains all of the subplots is called a figure.

We can create subplots using .subplot().

The command plt.subplot() needs three arguments to be passed into it:

  • The number of rows of subplots

  • The number of columns of subplots

  • The index of the subplot we want to create

For instance, the command plt.subplot(2, 3, 4) would create “Subplot 4” from the figure above.

Any plt.plot() that comes after plt.subplot() will create a line plot in the specified subplot. For instance:

# Data sets
x = [1, 2, 3, 4]
y = [1, 2, 3, 4]

# First Subplot
plt.subplot(1, 2, 1)
plt.plot(x, y, color='green')
plt.title('First Subplot')

# Second Subplot
plt.subplot(1, 2, 2)
plt.plot(x, y, color='steelblue')
plt.title('Second Subplot')

# Display both subplots
plt.show()

This would result in a figure with the two plots arranged like this:

money_spent_2

We can specify a different color for a line by using the keyword color with either an or a :

money_colors
axis_zoom
axis_labels

We can have many different subplots in the same figure, and we can lay them out in many different ways. We can think of our layouts as having rows and columns of subplots. For instance, the following figure has six subplots split into 2 rows and 3 columns:

subplots
HTML color name
HEX code