Python Matplotlib Tutorial – Python Plotting For Beginners

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

In our previous tutorial, Python Data Cleansing. Today, we’ll play around with Python Matplotlib Tutorial and Python Plot. Moreover, we will discuss Pyplot, Keyword String, and Categorical Variables of Python Plotting. At last, we will cover Line properties and some Python Matplotlib example.
So, let’s start Python Matplotlib Tutorial.

What is Python Matplotlib?

Working across platforms, when you want to conceive publication quality figures in hardcopy formats and interactive environments, you use matplotlib. This is a library for 2-dimensional plotting with Python.
Some plots it will let us build are:

  • Plots
  • Histograms
  • Error charts
  • Power spectra
  • Bar charts
  • Scatter Plots

Some features of Python Plot supports-

  • Font properties
  • Axes properties
  • Line styles

Follow this link to know about Python Packages 

Python Matplotlib Tutorial – Prerequisites

For our Python Matplotlib tutorial, we will need two Python libraries.

a. Python Matplotlib

We’ve already discussed this in section 2. To install it, you can use the following command-

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
C:\Users\lifei>pip install matplotlib
C:\Users\lifei>pip install matplotlib
C:\Users\lifei>pip install matplotlib

b. Pandas

Pandas is another Python library we will use here. It lets us manipulate and analyze data using data structures and operations on numerical tables and time series.

Python Matplotlib Tutorial - Python Plotting

Matplotlib Tutorial – Python Plotting

You can install it with the following command-

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
C:\Users\lifei>pip install pandas
C:\Users\lifei>pip install pandas
C:\Users\lifei>pip install pandas

Python Matplotlib Tutorial – Pyplot

It has a pyplot interface. This holds command-like functions that let us alter a figure.

a. plot()

You can use the plot() method to create a plot of points on the graph.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> import matplotlib.pyplot as plt
>>> plt.plot([2,3,4,5])
>>> import matplotlib.pyplot as plt >>> plt.plot([2,3,4,5])
>>> import matplotlib.pyplot as plt
>>> plt.plot([2,3,4,5])

[<matplotlib.lines.Line2D object at 0x00FD5650>]

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.xlabel('Actual birth weight')
>>> plt.xlabel('Actual birth weight')
>>> plt.xlabel('Actual birth weight')

Text(0.5,0,’Actual birth weight’)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.ylabel('Estimated birth weight')
>>> plt.ylabel('Estimated birth weight')
>>> plt.ylabel('Estimated birth weight')

Text(0,0.5,’Estimated birth weight’)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.show()
>>> plt.show()
>>> plt.show()

Let’s explore Python Property – The Problem and Solution

Python Matplotlib Tutorial - Python Plotting

Python Matplotlib Tutorial – Python Plotting

Here, the methods xlabel() and ylabel() let us set labels for the x and y-axes respectively. It takes values from our list of parameters for the y-axis; for the x-axis, it uses 0.0, 1.0, 2.0, and 3.0 for the four points.
We can give it more than one list of parameters-

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.plot([2,3,4,5],[3,8,10,12])
>>> plt.plot([2,3,4,5],[3,8,10,12])
>>> plt.plot([2,3,4,5],[3,8,10,12])

[<matplotlib.lines.Line2D object at 0x0153EFF0>] 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.show()
>>> plt.show()
>>> plt.show()
Python Matplotlib Tutorial - Python Plotting

Python Matplotlib Tutorial – Python Plotting

This takes the first list for x-axis and the second for the y-axis.

b. Formatting your Python Plot

A third argument will let you choose the color and the line type of the plot in Python Programming Language. The default format string gives us a solid blue line, as we’ve seen in the examples so far. This is ‘b-‘. You see, these strings are from MATLAB.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.plot([2,3,4,5],[3,8,10,12],'gs')
>>> plt.plot([2,3,4,5],[3,8,10,12],'gs')
>>> plt.plot([2,3,4,5],[3,8,10,12],'gs')

[<matplotlib.lines.Line2D object at 0x01AD1050>]

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.axis([0,7,0,21])
>>> plt.axis([0,7,0,21])
>>> plt.axis([0,7,0,21])

[0, 7, 0, 21]

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.show()
>>> plt.show()
>>> plt.show()
Python Matplotlib Tutorial - Python Plotting

Python Matplotlib Tutorial – Python Plotting

Formatting your Python PlotThe axis() method lets us set the viewport for the axes in terms of xmin, xmax, ymin, and ymax. The format string ‘gs’ here gives us green squares. Similarly, we can plot green, red, and blue orbs, squares, and triangles too. Let’s take another example.
Read about CGI Programming in Python with Functions and Modules

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> import numpy as np
>>> t=np.arange(0,5,0.2)
>>> plt.plot(t,t,'r--',t,t**3,'b^',t,t**2,'gs')
>>> import numpy as np >>> t=np.arange(0,5,0.2) >>> plt.plot(t,t,'r--',t,t**3,'b^',t,t**2,'gs')
>>> import numpy as np
>>> t=np.arange(0,5,0.2)
>>> plt.plot(t,t,'r--',t,t**3,'b^',t,t**2,'gs')

[<matplotlib.lines.Line2D object at 0x0A66B670>, <matplotlib.lines.Line2D object at 0x0A66B7B0>, <matplotlib.lines.Line2D object at 0x0A66BA90>] 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.show()
>>> plt.show()
>>> plt.show()
Python Matplotlib Tutorial - Python Plotting

Matplotlib Tutorial – Python Plotting

Python Matplotlib Keyword Strings

Using a data keyword argument, we can produce Python plots with strings that relate to some variables. We can access these variables with strings.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> data={'a':np.arange(50),
'c':np.random.randint(0,50,50),
'd':np.random.randn(50)}
>>> data['b']=data['a']+10*np.random.randn(50)
>>> data['d']=np.abs(data['d'])*100
>>> plt.scatter('a','b',c='c',s='d',data=data)
>>> data={'a':np.arange(50), 'c':np.random.randint(0,50,50), 'd':np.random.randn(50)} >>> data['b']=data['a']+10*np.random.randn(50) >>> data['d']=np.abs(data['d'])*100 >>> plt.scatter('a','b',c='c',s='d',data=data)
>>> data={'a':np.arange(50),
'c':np.random.randint(0,50,50),
'd':np.random.randn(50)}
>>> data['b']=data['a']+10*np.random.randn(50)
>>> data['d']=np.abs(data['d'])*100
>>> plt.scatter('a','b',c='c',s='d',data=data)

<matplotlib.collections.PathCollection object at 0x0A7E8AD0>

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.show()
>>> plt.show()
>>> plt.show()
Python Matplotlib Tutorial - Python Plotting

Matplotlib Tutorial -Keyword Strings

Categorical Variables to Python Plotting

We can pass categorical variables to Python plotting functions.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> names=["Dingos","Wild Cats","Tigers"]
>>> values=[1,11,111]
>>> plt.figure(1,figsize=(9,3))
>>> names=["Dingos","Wild Cats","Tigers"] >>> values=[1,11,111] >>> plt.figure(1,figsize=(9,3))
>>> names=["Dingos","Wild Cats","Tigers"]
>>> values=[1,11,111]
>>> plt.figure(1,figsize=(9,3))

<Figure size 900×300 with 0 Axes>

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.subplot(131)
>>> plt.subplot(131)
>>> plt.subplot(131)

<matplotlib.axes._subplots.AxesSubplot object at 0x00FD5B30>

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.bar(names,values)
>>> plt.bar(names,values)
>>> plt.bar(names,values)

<BarContainer object of 3 artists>

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.subplot(132)
>>> plt.subplot(132)
>>> plt.subplot(132)

<matplotlib.axes._subplots.AxesSubplot object at 0x01889610>

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.scatter(names,values)
>>> plt.scatter(names,values)
>>> plt.scatter(names,values)

<matplotlib.collections.PathCollection object at 0x0A191AB0>

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.subplot(133)
>>> plt.subplot(133)
>>> plt.subplot(133)

<matplotlib.axes._subplots.AxesSubplot object at 0x0A171110>

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.plot(names,values)
>>> plt.plot(names,values)
>>> plt.plot(names,values)

[<matplotlib.lines.Line2D object at 0x0A1ADE30>]

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.suptitle('Varsity')
>>> plt.suptitle('Varsity')
>>> plt.suptitle('Varsity')

Text(0.5,0.98,’Varsity’)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.show()
>>> plt.show()
>>> plt.show()
Python Matplotlib Tutorial - Python Plotting

Python Matplotlib Tutorial – Python Plotting

Python Matplotlib Tutorial – Categorical Variables to Python Plotting
Do you know How Python Send Email Via SMTP | SMTP Server

Some Line Properties of Matplotlib

Using some keyword arguments, we can alter how a graph looks. Let’s look at some.

Python Matplotlib Tutorial - Python Plotting For Beginners

Python Matplotlib Tutorial – Line Properties

a. Linewidth

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.plot([1,2,3],[2,4,9],linewidth=4.0)
>>> plt.plot([1,2,3],[2,4,9],linewidth=4.0)
>>> plt.plot([1,2,3],[2,4,9],linewidth=4.0)

[<matplotlib.lines.Line2D object at 0x010D05D0>] 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.show()
>>> plt.show()
>>> plt.show()
Python Matplotlib Tutorial - Python Plotting

Python Matplotlib Tutorial – Linewidth

b. Alpha

We can use the alpha channel to create prettier plots by softening colors.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.plot([1,2,3],[2,4,9],alpha=5.5)
>>> plt.plot([1,2,3],[2,4,9],alpha=5.5)
>>> plt.plot([1,2,3],[2,4,9],alpha=5.5)

[<matplotlib.lines.Line2D object at 0x00BC9310>] 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.show()
>>> plt.show()
>>> plt.show()
Python Matplotlib Tutorial - Python Plotting

Python Matplotlib Tutorial – Alpha

c. Antialiased

If you’ll look closely, the lines look quite smooth. But we can turn antialiasing off- this will show us the aliasing in the lines.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.plot([1,2,3],[2,4,9],antialiased=True)
>>> plt.plot([1,2,3],[2,4,9],antialiased=True)
>>> plt.plot([1,2,3],[2,4,9],antialiased=True)

[<matplotlib.lines.Line2D object at 0x087278B0>] 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.show()
>>> plt.show()
>>> plt.show()
Python Matplotlib Tutorial - Python Plotting

Python Matplotlib Tutorial – Antialiased

Let’s read about Image Processing with SciPy and NumPy in Python

d. Color or c

You can set the color of the plot with this parameter.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.plot([1,2,3],[2,4,9],color='Chartreuse')
>>> plt.plot([1,2,3],[2,4,9],color='Chartreuse')
>>> plt.plot([1,2,3],[2,4,9],color='Chartreuse')

[<matplotlib.lines.Line2D object at 0x0AE98D70>] 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.show()
>>> plt.show()
>>> plt.show()
Python Matplotlib Tutorial - Python Plotting

Matplotlib Tutorial – Color

e. Dashes

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.plot([1,2,3],[2,4,9],dashes=[1,2,4,4])
>>> plt.plot([1,2,3],[2,4,9],dashes=[1,2,4,4])
>>> plt.plot([1,2,3],[2,4,9],dashes=[1,2,4,4])

[<matplotlib.lines.Line2D object at 0x099ED290>] 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.show()
>>> plt.show()
>>> plt.show()
Python Matplotlib Tutorial - Python Plotting

Python Dashes

f. Linestyle or ls

You can choose the style of line you want for your plot.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.plot([1,2,3],[2,4,9],linestyle='steps')
>>> plt.plot([1,2,3],[2,4,9],linestyle='steps')
>>> plt.plot([1,2,3],[2,4,9],linestyle='steps')

[<matplotlib.lines.Line2D object at 0x00B816D0>]

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.show()
>>> plt.show()
>>> plt.show()
Python Matplotlib Tutorial - Python Plotting

Python Matplotlib Tutorial – Linestyle or ls

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.plot([1,2,3],[2,4,9],linestyle=':')
>>> plt.plot([1,2,3],[2,4,9],linestyle=':')
>>> plt.plot([1,2,3],[2,4,9],linestyle=':')

[<matplotlib.lines.Line2D object at 0x0AC892D0>] 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.show()
>>> plt.show()
>>> plt.show()
Python Matplotlib Tutorial - Python Plotting

Matplotlib Tutorial – Python Linestyle or ls

Let’s explore Python Database Access – Python 3 MySQL

g. Marker

A marker will let you select what symbol you want to display at breakpoints and bends.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.plot([1,2,3],[2,4,9],marker='+')
>>> plt.plot([1,2,3],[2,4,9],marker='+')
>>> plt.plot([1,2,3],[2,4,9],marker='+')

[<matplotlib.lines.Line2D object at 0x01018790>]

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.show()
>>> plt.show()
>>> plt.show()
Python Matplotlib Tutorial - Python Plotting

Python Marker

h. Markeredgecolor

You can also decide on a color for your markers’ edges.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.plot([1,2,3],[2,4,9],marker='+',markeredgecolor='brown')
>>> plt.plot([1,2,3],[2,4,9],marker='+',markeredgecolor='brown')
>>> plt.plot([1,2,3],[2,4,9],marker='+',markeredgecolor='brown')

[<matplotlib.lines.Line2D object at 0x01280110>] 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.show()
>>> plt.show()
>>> plt.show()
Python Matplotlib Tutorial - Python Plotting

Matplotlib Tutorial – Markeredgecolor

i. Markeredgewidth

This lets us decide how thick we want our markers to be.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.plot([1,2,3],[2,4,9],marker='+',markeredgewidth=0.4)
>>> plt.plot([1,2,3],[2,4,9],marker='+',markeredgewidth=0.4)
>>> plt.plot([1,2,3],[2,4,9],marker='+',markeredgewidth=0.4)

[<matplotlib.lines.Line2D object at 0x0AE54290>]

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.show()
>>> plt.show()
>>> plt.show()
Python Matplotlib Tutorial - Python Plotting

Matplotlib Tutorial – Markeredgewidth

Let’s Learn Aggregation and Data Wrangling with Python

j. Markerfacecolor and Markersize

This will let you choose which color to fill in your marker and what size to keep it.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.plot([1,2,3],[2,4,9],marker='.',markerfacecolor='orange',markersize=13.0)
>>> plt.plot([1,2,3],[2,4,9],marker='.',markerfacecolor='orange',markersize=13.0)
>>> plt.plot([1,2,3],[2,4,9],marker='.',markerfacecolor='orange',markersize=13.0)

[<matplotlib.lines.Line2D object at 0x0AE9BC10>] 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.show()
>>> plt.show()
>>> plt.show()
Python Matplotlib Tutorial - Python Plotting

Python Matplotlib Tutorial – Markerfacecolor and Markersize

k. Markevery

This parameter lets us decide at what parameters we want to put markers.

Python Matplotlib Tutorial - Python Plotting

Python Markevery

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
plt.plot([1,2,3],[2,4,9],marker='.',markerfacecolor='orange',markersize=13.0,markevery=2)
plt.plot([1,2,3],[2,4,9],marker='.',markerfacecolor='orange',markersize=13.0,markevery=2)
plt.plot([1,2,3],[2,4,9],marker='.',markerfacecolor='orange',markersize=13.0,markevery=2)

[<matplotlib.lines.Line2D object at 0x0103C8B0>]

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.show()
>>> plt.show()
>>> plt.show()
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.plot([1,2,3],
>>> plt.plot([1,2,3],
>>> plt.plot([1,2,3],

[2,4,9],marker=’.’,markerfacecolor=’orange’,markersize=13.0,markevery=3) [<matplotlib.lines.Line2D object at 0x01025F10>]

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.show()
>>> plt.show()
>>> plt.show()
Python Matplotlib Tutorial - Python Plotting

Matplotlib Tutorial – Python Markevery

l. Zorder

This lets us decide which plots will show up in the front and which to send to back.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.plot([1,2,3],[2,4,9],zorder=1,linewidth=4)
>>> plt.plot([1,2,3],[2,4,9],zorder=1,linewidth=4)
>>> plt.plot([1,2,3],[2,4,9],zorder=1,linewidth=4)

[<matplotlib.lines.Line2D object at 0x01255E70>]

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.plot([1,2,6,9],[2,4,9,10],zorder=2,linewidth=4)
>>> plt.plot([1,2,6,9],[2,4,9,10],zorder=2,linewidth=4)
>>> plt.plot([1,2,6,9],[2,4,9,10],zorder=2,linewidth=4)

[<matplotlib.lines.Line2D object at 0x01255770>]

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.show()
>>> plt.show()
>>> plt.show()
Python Matplotlib Tutorial - Python Plotting

Matplotlib Tutorial – Python Plotting

In this graph, in the area enclosed by the points (1,2) and (2,4), both plots cover the same points, but the one in orange is in the front. This is because we give it a higher zorder. This is like z-index in CSS.
Let’s Know about Python Stemming and Lemmatization – NLTK

Showing a Grid in Python Plot

You can use the grid() method to toggle a grid in your plot.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.grid(True)
>>> plt.plot([1,2,6,9],[2,4,9,10],zorder=2,linewidth=4)
>>> plt.grid(True) >>> plt.plot([1,2,6,9],[2,4,9,10],zorder=2,linewidth=4)
>>> plt.grid(True)
>>> plt.plot([1,2,6,9],[2,4,9,10],zorder=2,linewidth=4)

[<matplotlib.lines.Line2D object at 0x00DC02B0>] 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> plt.show()
>>> plt.show()
>>> plt.show()
Python Matplotlib Tutorial - Python Plotting

Showing a Grid in Python Plot

So, this was all about Python Matplotlib Tutorial. Hope you like our explanation.

Conclusion

Hence, we have studied, Matplotlib, which is the Python Libraries used for Python Plot and much more. Libraries like Seaborn and Pandas plotting functions are built on top of Matplotlib. Once you understand the basics, you can use it to build visuals for dashboards, reports, and Jupyter notebooks that clearly communicate your insights.

Furthermore, if you have any information regarding it, feel free to share with us.

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

courses

DataFlair Team

DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.

1 Response

  1. PHILEMON KIRUI says:

    The article is really nice and helpful. I feel so much confident in handling any challenge related to matplotlib.

    Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *