Search this site
Embedded Files
AIMD GPDS Courses
  • Home
  • Courses
  • Contact
AIMD GPDS Courses
  • Home
  • Courses
  • Contact
  • More
    • Home
    • Courses
    • Contact

日本語  ❯

Lesson 4    ❮    Lesson List    ❮    Top Page

❯  4.1  Intro to matplotlib

4.2  Plotting with pandas

4.3  Styles of Visualization

4.4  Advanced Filtering

4.5  Multiple Plots with Seaborn

⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
EXPECTED COMPLETION TIME
❲▹❳  Video   8m 18s
☷  Interactive readings   5m

Making a simple plot with matplotlib

With matplotlib, we use the following import convention:

import matplotlib.pyplot as plt


To make  a plot, we can simply add plt.plot() follow by the x and y axis.

Making multiple plot

The subplots method of matplotlib can be used to make multiple plot.

This method returns two variables for the figure and the axis, usually named fig and ax.

Common options in subplots:

nrows, ncols
Number of rows/columns of the subplot grid.

sharex, sharey
Controls sharing of properties among x (sharex) or y (sharey) axes.

Adding colors, markers , and line Styles.

Matplotlib's main plot function accepts arrays of x and y coordinates and optionally a string abbreviation indicating color and line style.  For example, to plot x versus y with green dashes, you would execute:

ax.plot(x, y, 'g--')

The same plot could also have been expressed more explicitly as:

ax.plot(x, y, linestyle='--', color='g')

Line styles help differentiate the graphs. Here are the character representing the linestyle:

  • '-' : Solid Line
  • '--' : Dashed Line
  • '-.' : Dash-dot Line
  • ':' : Dotted Line

Color is another way to differentiate line graphs. The following list shows the color 

  • 'b' Blue
  • 'g' Green
  • 'r' Red
  • 'c'  Cyan
  • 'm'  Magenta
  • 'k'  Black
  • 'w'  White

Markers add a special symbol to each data point in a line graph. The following list shows some  of the markers:

  • '.' Point
  • ',' Pixel
  • 'o' Circle
  • 's' Square
  • '*' Star
  • '+' Plus
  • 'x' X

Setting the ticks, labels, and title.

Sometimes we want to format the number of ticks and the labels.

To change the axis ticks, we can use set_xticks, set_yticks.

For changing the name of the ticks, we can use set_xticklabels, set_yticklabels. 

Lastly, using the method suptitle on the fig and set_title on the ax, we can give the title to our figures.

©2023. All rights reserved.  Samy Baladram,
Graduate Program in Data Science - GSIS - Tohoku University
Google Sites
Report abuse
Page details
Page updated
Google Sites
Report abuse