Matplotlib
Matplotlib
ipynb - Colab
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).
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.
The plot() function only creates the graph. To display it, we call the show() function
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
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.
https://colab.research.google.com/drive/1hwe4sQUav7l5Ai_eb3WM8xVmwY5Wj05_#scrollTo=y--b9Txwajlq&printMode=true 2/12
9/10/24, 5:54 PM Matplotlib.ipynb - Colab
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 only points (no lines join the points) that are used to plot the function f(x)=x^2
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 while() loop to plot the function f(x)=x^2. We use while() loop to include more points within the interval [-2,2]
https://colab.research.google.com/drive/1hwe4sQUav7l5Ai_eb3WM8xVmwY5Wj05_#scrollTo=y--b9Txwajlq&printMode=true 5/12
9/10/24, 5:54 PM Matplotlib.ipynb - Colab
Change the linewidth of the plot. The command is linewidth = value. You can set value to thinner or thicker the line.
https://colab.research.google.com/drive/1hwe4sQUav7l5Ai_eb3WM8xVmwY5Wj05_#scrollTo=y--b9Txwajlq&printMode=true 6/12
9/10/24, 5:54 PM Matplotlib.ipynb - Colab
https://colab.research.google.com/drive/1hwe4sQUav7l5Ai_eb3WM8xVmwY5Wj05_#scrollTo=y--b9Txwajlq&printMode=true 7/12
9/10/24, 5:54 PM Matplotlib.ipynb - Colab
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()
To add title in the graph, we pass the follwoing command to import all necessary commands.
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:
or we can pass
def f(x):
return x**2
i = -2
xaxis = []
yaxis = []
while i <= 2:
xaxis.append(i)
yaxis.append(f(i))
i += 0.1
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
(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.
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