DEV Community

Cover image for Matplotlib: Plotting in Matplotlib
MdMusfikurRahmanSifar
MdMusfikurRahmanSifar

Posted on

Matplotlib: Plotting in Matplotlib

Matplotlib is the "grandfather" library of data visualization with Python. It is an excellent 2D and 3D graphics library for generating scientific figures.

Basics of matplotlib basically means-

  • How to plot
  • How to edit the plot

Our main focus in this will be the plotting part. Some editing will be along the way.

We will discuss-
- Plotting in two ways
- Subplot in two ways

First we need to-

import matplotlib.pyplot as plt
%matplotlib inline
Enter fullscreen mode Exit fullscreen mode

%matplotlib inline is used in jupyter. It allows us to plot the graph without extra command. Otherwise on other editors to see the plot we will need to use plt.show()

Plotting in two ways:

Functional method:

01
02

Object Oriented Method:

03
This method actually gives us control over the figure's axes...It is really helpful. Play with it to know it!

Subplots in two ways:

Functional method:

04
05

OO method:

06
07
We can actually see the power of this method in this example.
08
09
10
Here .set_xlabel() .set_ylabel() .set_title() are used for editing. There's a lot that we can edit in matplotlib....go ahead and find out them. I am mentioning some-

  • You can use the label="label text" keyword argument when plots or other objects are added to the figure, and then using the legend method without arguments to add the legend to the figure:
    11

  • To change the line width, we can use the linewidth or lw keyword argument. The line style can be selected using the linestyle or ls keyword arguments:

12
13
There is more to matplotlib. Let's keep exploring....

Top comments (0)