Learn to Code via Tutorials on Repl.it!

← Back to all posts
harpertutorial - Plotting Graphs
h
HarperframeInc

Plotting Graphs...

You do that in math, science, and in python too!
First, we need to import matplotlib


NOTE
matplotlib is a looooong name. Use as mpl instead.
I will be referring to it as mpl.


So, what's next?
Let's start your first simple graph.

So we imported mpl. What's next?
We can create a line graph with:

Doing this will give us:

Do you like it?
Mess around with it, do whatever you want!

Labeling

Labels, Labels, Labels.
Your X axis, your Y axis.
You can label your X & Y axis with:

Here's an example the Pyplot Guide gives:

Saving and Showing.

You can show with the:

Or you want to download it,

Bonus

Eww, I hate that blue line. Why is it there?
Well,

Gives you...
Drumroll Please....

Wow!
The blue line is because all graphs default to "-b"
How about the red circles? Its "ro"
How about all colors and shapes?
Here's what the guide has to say.

Colors
The following color abbreviations are supported:
'b' blue
'g' green
'r' red
'c' cyan
'm' magenta
'y' yellow
'k' black
'w' white
Markers
'.' point marker
',' pixel marker
'o' circle marker
'v' triangledown marker
'^' triangle_up marker
'<' triangle_left marker
'>' triangle_right marker
'1' tri_down marker
'2' tri_up marker
'3' tri_left marker
'4' tri_right marker
's' square marker
'p' pentagon marker
'*' star marker
'h' hexagon1 marker
'H' hexagon2 marker
'+' plus marker
'x' x marker
'D' diamond marker
'd' thin_diamond marker
'|' vline marker
'
' hline marker
Line Styles
'-' solid line style
'--' dashed line style
'-.' dash-dot line style
':' dotted line style
Please don't ask me questions about this, because I literally have no clue on shapes and stuff, I can help with anything else. Here's the guide on the plot() function: https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html#matplotlib.pyplot.plot

Liked this tutorial? Was it anyway helpful to you? If so, please upvote this tutorial. I might be adding on more soon.

The Guide: https://matplotlib.org/tutorials/introductory/pyplot.html#sphx-glr-tutorials-introductory-pyplot-py
Use the code below for reference

Voters
MahatiSriramul1
Johnge
BalachandarGovi
awodeay1
DarrFranco
AnthonyRindone
NIKOLASGONZALES
LiCar2994
Mosrod
timmy_i_chen
Comments
hotnewtop
UVIANAnD

These examples aren't working for me, and I have tried similar tutorials that also don't output anything after hitting "Run". What could be wrong?
The very first example does nothing. It also does nothing if you use "mpl.show()", and you get an error if you try "mpl.save()".

HarperframeInc

Hi @UVIANAnD! This tutorial is outdated. I'm sorry it couldn't help you, however you can try checking the matplotlib website

JohnHenckel

I get error no such function mpl.save()

timmy_i_chen

Awesome - thanks for making this!

HarperframeInc

@timmy_i_chen No Problem!