0% found this document useful (0 votes)
4 views

Matplotlib

Uploaded by

Tea
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Matplotlib

Uploaded by

Tea
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

9/10/24, 5:53 PM Matplotlib.

ipynb - Colab

Creating graphs with Matplotlib

Matplotlib is a Python package, which means that it's a collection of modules with related functionality.

We will plot a simple graph with just three points: (1,2), (2,4) and (3,6).

First item in each point is x-coordinate

Second item in each point is y-coordinate

x_numbers = [1, 2, 3]
y_numbers = [2, 4, 6]

We import the plot() and show() functions from the pylab module, which is part of the matplotlib package.

plot() function : plot(x-axis, y-axis)

The plot() function only creates the graph. To display it, we call the show() function

from pylab import plot, show


plot(x_numbers, y_numbers)
show()

Marking Points on Your Graph

plot(x-axis, y-axis, marker = 'symbol')

Add marker = 'symbol' in plot() function to mark the points that are supplied for plotting

symbols

'o': Circle
's': Square
'^': Triangle up
'v': Triangle down
'*': Star
'+': Plus
'x': Cross

from pylab import plot, show


plot(x_numbers, y_numbers, marker = "v")
show()

https://colab.research.google.com/drive/1hwe4sQUav7l5Ai_eb3WM8xVmwY5Wj05_#scrollTo=y--b9Txwajlq&printMode=true 1/12
9/10/24, 5:54 PM Matplotlib.ipynb - Colab

By default, we can see a line connecting the points. To remove this line or to see the plot of our actual data, we pass the following.

from pylab import plot, show


plot(x_numbers, y_numbers, 'o')
show()

To increase size of marker, we use markersize=value

from pylab import plot, show


plot(x_numbers, y_numbers, 'o', markersize = 15)
show()

https://colab.research.google.com/drive/1hwe4sQUav7l5Ai_eb3WM8xVmwY5Wj05_#scrollTo=y--b9Txwajlq&printMode=true 2/12
9/10/24, 5:54 PM Matplotlib.ipynb - Colab

To change marker color and marker edge color, we pass, respectively,

markerfacecolor='color name' and markeredgecolor='color name'

from pylab import plot, show


plot(x_numbers, y_numbers, 'o', markersize = 15,markerfacecolor="red", markeredgecolor="green")
show()

Plotting function f(x)=x^2

from pylab import plot, show


def f(x):
return x**2
xaxis = [-2,-1,0,1,2]
yaxis = [f(-2),f(-1),f(0),f(1),f(2)]
plot(xaxis, yaxis)
show()

https://colab.research.google.com/drive/1hwe4sQUav7l5Ai_eb3WM8xVmwY5Wj05_#scrollTo=y--b9Txwajlq&printMode=true 3/12
9/10/24, 5:54 PM Matplotlib.ipynb - Colab

Showing points that are used to plot the function f(x)=x^2

from pylab import plot, show


def f(x):
return x**2
xaxis = [-2,-1,0,1,2]
yaxis = [f(-2),f(-1),f(0),f(1),f(2)]
plot(xaxis, yaxis, marker = "o")
show()

Showing only points (no lines join the points) that are used to plot the function f(x)=x^2

from pylab import plot, show


def f(x):
return x**2
xaxis = [-2,-1,0,1,2]
yaxis = [f(-2),f(-1),f(0),f(1),f(2)]
plot(xaxis, yaxis, "o")
show()

https://colab.research.google.com/drive/1hwe4sQUav7l5Ai_eb3WM8xVmwY5Wj05_#scrollTo=y--b9Txwajlq&printMode=true 4/12
9/10/24, 5:54 PM Matplotlib.ipynb - Colab

Using for() loop to plot the function f(x)=x^2

from pylab import plot, show


def f(x):
return x**2
xaxis = []
yaxis = []
for i in range(-2,3,1):
xaxis.append(i)
yaxis.append(f(i))
plot(xaxis, yaxis, "o")
show()

Using while() loop to plot the function f(x)=x^2. We use while() loop to include more points within the interval [-2,2]

from pylab import plot, show


def f(x):
return x**2
i=-2
xaxis = []
yaxis = []
while i<= 2:
xaxis.append(i)
yaxis.append(f(i))
i=i+0.1
plot(xaxis, yaxis, "o")
show()

https://colab.research.google.com/drive/1hwe4sQUav7l5Ai_eb3WM8xVmwY5Wj05_#scrollTo=y--b9Txwajlq&printMode=true 5/12
9/10/24, 5:54 PM Matplotlib.ipynb - Colab

More points will give accurate graph

from pylab import plot, show


def f(x):
return x**2
i=-2
xaxis = []
yaxis = []
while i<= 2:
xaxis.append(i)
yaxis.append(f(i))
i=i+0.1
plot(xaxis, yaxis, marker = "o")
show()

Change the linewidth of the plot. The command is linewidth = value. You can set value to thinner or thicker the line.

from pylab import plot, show


def f(x):
return x**2
i=-2
xaxis = []
yaxis = []
while i<= 2:
xaxis.append(i)
yaxis.append(f(i))
i=i+0.1
plot(xaxis, yaxis, linewidth=0.5) # thinner the line
show()

https://colab.research.google.com/drive/1hwe4sQUav7l5Ai_eb3WM8xVmwY5Wj05_#scrollTo=y--b9Txwajlq&printMode=true 6/12
9/10/24, 5:54 PM Matplotlib.ipynb - Colab

from pylab import plot, show


def f(x):
return x**2
i=-2
xaxis = []
yaxis = []
while i<= 2:
xaxis.append(i)
yaxis.append(f(i))
i=i+0.1
plot(xaxis, yaxis, linewidth=4) # thicker the line
show()

Adding color to the graph.

from pylab import plot, show


def f(x):
return x**2
i=-2
xaxis = []
yaxis = []
while i<= 2:
xaxis.append(i)
yaxis.append(f(i))
i=i+0.1
plot(xaxis, yaxis, marker = "o",color="red")
show()

https://colab.research.google.com/drive/1hwe4sQUav7l5Ai_eb3WM8xVmwY5Wj05_#scrollTo=y--b9Txwajlq&printMode=true 7/12
9/10/24, 5:54 PM Matplotlib.ipynb - Colab

Changing color of marker

from pylab import plot, show


def f(x):
return x**2
i=-2
xaxis = []
yaxis = []
while i<= 2:
xaxis.append(i)
yaxis.append(f(i))
i=i+0.1
plot(xaxis, yaxis, marker = "o",color="red")
plot(xaxis, yaxis, "o",color="green")
show()

Other way to change the color of the marker

from pylab import plot, show

def f(x):
return x**2

i = -2
xaxis = []
yaxis = []

while i <= 2:
xaxis.append(i)
yaxis.append(f(i))
i += 0.1

# Plot the graph with a specified color for the line, marker, and marker edge
plot(xaxis, yaxis, color="blue", marker="o", markerfacecolor="red", markeredgecolor="black")
https://colab.research.google.com/drive/1hwe4sQUav7l5Ai_eb3WM8xVmwY5Wj05_#scrollTo=y--b9Txwajlq&printMode=true 8/12
9/10/24, 5:54 PM Matplotlib.ipynb - Colab
plot(xaxis, yaxis, color blue , marker o , markerfacecolor red , markeredgecolor black )

show()

Labelling the graph

from pylab import plot, show, xlabel, ylabel, legend


def f(x):
return x**2
i=-2
xaxis = []
yaxis = []
while i<= 2:
xaxis.append(i)
yaxis.append(f(i))
i=i+0.1
plot(xaxis, yaxis, marker = "o",label = "Graph of f(x)=x^2")
xlabel("x-axis")
ylabel("y-axis")
legend()
show()

To add title in the graph, we pass the follwoing command to import all necessary commands.

from matplotlib import *

Or one can pass

from matplotlib.pyplot import title

from matplotlib import *


from pylab import plot, show, xlabel, ylabel, legend
def f(x):
return x**2
https://colab.research.google.com/drive/1hwe4sQUav7l5Ai_eb3WM8xVmwY5Wj05_#scrollTo=y--b9Txwajlq&printMode=true 9/12
9/10/24, 5:54 PM Matplotlib.ipynb - Colab
return x**2
i=-2
xaxis = []
yaxis = []
while i<= 2:
xaxis.append(i)
yaxis.append(f(i))
i=i+0.1
plot(xaxis, yaxis, marker = "o",label = "Graph of f(x)=x^2")
xlabel("x-axis")
ylabel("y-axis")
legend()
show()

Changing x-axis and y-axis limits. For this we pass the commmands xlim() and ylim(). We also pass the following command that helps to
import the commands:

from matplotlib.pyplot import title, xlim, ylim

or we can pass

from matplotlib import *

from pylab import plot, show, xlabel, ylabel, legend


from matplotlib.pyplot import title, xlim, ylim # Import xlim, ylim, and title from matplotlib.pyplot

def f(x):
return x**2

i = -2
xaxis = []
yaxis = []

while i <= 2:
xaxis.append(i)
yaxis.append(f(i))
i += 0.1

plot(xaxis, yaxis, marker="o")


xlabel("x-axis")
ylabel("y-axis")
title("Plot of square function")

# Set the limits for the x-axis and y-axis


xlim(-3, 3) # Set x-axis limits from -3 to 3
ylim(-1, 5) # Set y-axis limits from -1 to 5

legend(["Graph of f(x)=x^2"])
show()

https://colab.research.google.com/drive/1hwe4sQUav7l5Ai_eb3WM8xVmwY5Wj05_#scrollTo=y--b9Txwajlq&printMode=true 10/12
9/10/24, 5:54 PM Matplotlib.ipynb - Colab

Change the place of the legend.

upper right : Place the legend in the upper-right corner.

upper left : Place the legend in the upper-left corner.

lower left : Place the legend in the lower-left corner.

lower right : Place the legend in the lower-right corner.

best : Automatically choose the best location.

(x, y) : Place the legend at a specific location using normalized coordinates, where (0, 0) is the bottom-left and (1, 1) is the top-right.

from pylab import plot, show, xlabel, ylabel, legend


def f(x):
return x**2
i=-2
xaxis = []
yaxis = []
while i<= 2:
xaxis.append(i)
yaxis.append(f(i))
i=i+0.1
plot(xaxis, yaxis, marker = "o",label = "Graph of f(x)=x^2")
xlabel("x-axis")
ylabel("y-axis")
legend(loc="lower left")
show()

from pylab import plot, show, xlabel, ylabel, legend


def f(x):
return x**2
https://colab.research.google.com/drive/1hwe4sQUav7l5Ai_eb3WM8xVmwY5Wj05_#scrollTo=y--b9Txwajlq&printMode=true 11/12
9/10/24, 5:54 PM Matplotlib.ipynb - Colab
i=-2
xaxis = []
yaxis = []
while i<= 2:
xaxis.append(i)
yaxis.append(f(i))
i=i+0.1
plot(xaxis, yaxis, marker = "o",label = "Graph of f(x)=x^2")
xlabel("x-axis")
ylabel("y-axis")
legend(loc=(0.5,0.5))
show()

from pylab import plot, show, xlabel, ylabel, legend


def f(x):
return x**2

def g(x):
return x**3
i=-2
xaxis = []
f_yaxis = []
g_yaxis = []
while i<= 2:
xaxis.append(i)
f_yaxis.append(f(i))
g_yaxis.append(g(i))
i=i+0.1
plot(xaxis, f_yaxis, marker = "o",label = "Graph of f(x)=x^2")
plot(xaxis, g_yaxis, marker = "*",label = "Graph of g(x)=x^3")
xlabel("x-axis")
ylabel("y-axis")
legend()
show()

https://colab.research.google.com/drive/1hwe4sQUav7l5Ai_eb3WM8xVmwY5Wj05_#scrollTo=y--b9Txwajlq&printMode=true 12/12

You might also like