Add Grid Lines to a Plot
.ة إلى ال س ال اني ال ﻹضافة خgrid() ام ال الة ا اس ، Pyplot ام اس
import numpy as np
import [Link] as plt
x = [Link]([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = [Link]([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])
[Link]("Sports Watch Data")
[Link]("Average Pulse")
[Link]("Calorie Burnage")
[Link](x, y)
[Link]()
[Link]()
.ع ضها ة ال ي س ال ي خ لgrid() في ال الةaxis ام ال ارم اس
ة ال اق ل ة فق ال وذل لع ض خaxis = 'x'
ة اﻷف ة فق ال وذل لع ض خaxis = 'y'
و ون ت ي ال ضع اﻻف اضي ع ض ش ة اﻷف ة وال اق ل ة
Creating multiple subplots using [Link]
في ت مع ت ف، ة اس عاء واح ات الف وش ة م الfigure ش ًﻼ[Link] ي ئ
ات الف د ة ك ة إن اء ال
1- subplots() without arguments returns a Figure and a single Axes.
import [Link] as plt
import numpy as np
x = [Link](0, 2 * [Link], 400)
y = [Link](x ** 2)
fig, ax = [Link]()
[Link]("sub")
[Link](x, y)
ax.set_title('A single plot')
[Link]()
Stacking subplots in one direction
fig, axs = [Link](2)
[Link]('Vertically stacked subplots')
axs[0].plot(x, y)
axs[1].plot(x, -y)
To obtain side-by-side subplots, pass parameters 1, 2 for one row and two
columns.
fig, (ax1, ax2) = [Link](1, 2)
[Link]('Horizontally stacked subplots')
[Link](x, y)
[Link](x, -y)
Stacking subplots in two directions
When stacking in two directions, the returned axs is a 2D NumPy array.
If you have to set parameters for each subplot it's handy to iterate over all
subplots in a 2D grid using for ax in [Link]:.
fig, axs = [Link](2, 2)
axs[0, 0].plot(x, y)
axs[0, 0].set_title('Axis [0, 0]')
axs[0, 1].plot(x, y, 'tab:orange')
axs[0, 1].set_title('Axis [0, 1]')
axs[1, 0].plot(x, -y, 'tab:green')
axs[1, 0].set_title('Axis [1, 0]')
axs[1, 1].plot(x, -y, 'tab:red')
axs[1, 1].set_title('Axis [1, 1]')
for ax in [Link]:
[Link](xlabel='x-label', ylabel='y-label')
# Hide x labels and tick labels for top plots and y ticks for right plots.
for ax in [Link]:
ax.label_outer()
:قة ثان ة
fig, ((ax1, ax2), (ax3, ax4)) = [Link](2, 2)
[Link]('Sharing x per column, y per row')
[Link](x, y)
[Link](x, y**2, 'tab:orange')
[Link](x, -y, 'tab:green')
[Link](x, -y**2, 'tab:red')
for ax in fig.get_axes():
ax.label_outer()
Sharing axes
By default, each Axes is scaled individually. Thus, if the ranges are different the
tick values of the subplots do not align.
fig, (ax1, ax2) = [Link](2)
[Link]('Axes values are scaled individually by default')
[Link](x, y)
[Link](x + 1, -y)
You can use sharex or sharey to align the horizontal or vertical axis.
fig, (ax1, ax2) = [Link](2, sharex=True)
[Link]('Aligning x-axis using sharex')
[Link](x, y)
[Link](x + 1, -y)
Setting sharex or sharey to True enables global sharing across the whole grid,
i.e. also the y-axes of vertically stacked subplots have the same scale when
using sharey=True.
fig, axs = [Link](3, sharex=True, sharey=True)
[Link]('Sharing both axes')
axs[0].plot(x, y ** 2)
axs[1].plot(x, 0.3 * y, 'o')
axs[2].plot(x, y, '+')
3D graph plot:
ات ثﻼث ة ث ت إضافةأدوات ال. ات ث ائ ة اﻷ عاد فق في ال ا ةMatplotlib ت ت
. اﻷ عاد
ا في ذل ت، mplot3d عة أدوات اس اد م ات ثﻼث ة اﻷ عاد ع إن اء ال
:ال ئ يMatplotlib
from mpl_toolkits import mplot3d
إلى أ م إج اءات إن اءprojection='3d' ت إن اء م اور ثﻼث ة اﻷ عاد ع
: ال اور العاد ة
:م ال
from mpltoolkits import mplot3d
import numpy as np
import [Link] as plt
fig = [Link]()
ax = [Link](projection='3d')
:2م ال
from mpl_toolkits import mplot3d
import numpy as np
import [Link] as plt
height = [Link]([100,110,87,85,65,80,96,75,42,59,54,63,95,71,86])
weight = [Link]([105,123,84,85,78,95,69,42,87,91,63,83,75,41,80])
scatter(height,weight)
fig = [Link]()
ax = [Link](projection='3d')
# This is used to plot 3D scatter
ax.scatter3D(height,weight)
[Link]("3D Scatter Plot")
[Link]("Height")
[Link]("Weight")
[Link]("3D Scatter Plot")
[Link]("Height")
[Link]("Weight")
[Link]()
Note: We can use the plot3D () to plot simple 3D line graph.
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import [Link] as plt
[Link]['[Link]'] = 10
fig = [Link]()
ax = [Link](projection='3d')
theta1 = [Link](-4 * [Link], 4 * [Link], 100)
z = [Link](-2, 2, 100)
r = z**2 + 1
x = r * [Link](theta1)
y = r * [Link](theta1)
ax.plot3D(x, y, z, label='parametric curve', color = 'red')
[Link]()
[Link]()
Important functions of Matplotlib
Functions Description
plot(x-axis value, y-axis- It is used to plot a simple line graph with x-axis value
values) against the y-axis values. show() It is used to display the
graph.
title("string") It is used to set the title of the plotted graph as specified by
the string.
xlabel("string") It is used to set the label for the x-axis as specified by the
string.
ylabel("string") It is used to set the label for y-axis as specified by the
string.
figure() It is used to control a figure level attributes.
subplots(nrows,ncol,index) It is used to add a subplot to recent figure.
subtitle("string") It adds a common title to the plotted graph specified by the
string.
subplots(nrows,ncols,figsize) It provides the simple way to create subplot, in a single call
and returns a tuple of a figure and number of axes.
set_title("string") It is an axes level method which is used to set the title of
the subplots.
bar(categorical variables, values, It is used to create a vertical bar graph.
color)
barh(categorical variables, It is used to create horizontal bar graphs.
values, color)
legend(loc) It is used to make a legend of the graph.
xtricks(index, categorical It is used to set or get the current tick locations labels of the
variables) x-axis.
pie(value, categorical variables) It is used to create a pie chart.
hist(value, number of bins) It is used to create a histogram.
xlim(start value, end value) It is used to set the limit of values of the x-axis.
ylim(start value, end value) It is used to set the limit of values of the y-axis.
scatter(x-axis values, y-axis It is used to plots a scatter plot with x-axis value against the
values) y-axis values.
axes() It is used to add axes to the recent figure.
set_xlabel("string") It is an axes level method which is used to set the x-label
of the plot specified as a string.
set_ylabel("string") It is used to set the y-label of the plot specified as a string.
scatter3D(x-axis values, y-axis It is used to plot a three-dimension scatter plot with x- axis
values) value against the y-axis.
plot3D(x-axis values, y-axis It is used to plots a three-dimension line graph with x- axis
values) values against y-axis values.