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

matplotlib-2-1

The document provides a comprehensive guide on using Matplotlib for plotting, including adding grid lines, creating subplots, and generating 3D plots. It covers essential functions and their descriptions for plotting various types of graphs such as line graphs, bar graphs, and scatter plots. Additionally, it explains how to share axes and customize plot titles and labels.

Uploaded by

ahmadkharfan111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

matplotlib-2-1

The document provides a comprehensive guide on using Matplotlib for plotting, including adding grid lines, creating subplots, and generating 3D plots. It covers essential functions and their descriptions for plotting various types of graphs such as line graphs, bar graphs, and scatter plots. Additionally, it explains how to share axes and customize plot titles and labels.

Uploaded by

ahmadkharfan111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Add Grid Lines to a Plot

.‫ة إلى ال س ال اني‬ ‫ال‬ ‫ ﻹضافة خ‬grid() ‫ام ال الة‬ ‫ا اس‬ ، Pyplot ‫ام‬ ‫اس‬

import numpy as np
import matplotlib.pyplot as plt

x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

plt.title("Sports Watch Data")


plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")

plt.plot(x, y)

plt.grid()

plt.show()

.‫ع ضها‬ ‫ة ال ي س‬ ‫ال‬ ‫ي خ‬ ‫ ل‬grid() ‫ في ال الة‬axis ‫ام ال ارم‬ ‫اس‬

‫ة ال اق ل ة فق‬ ‫ال‬ ‫ وذل لع ض خ‬axis = 'x'

‫ة اﻷف ة فق‬ ‫ال‬ ‫ وذل لع ض خ‬axis = 'y'

‫و ون ت ي ال ضع اﻻف اضي ع ض ش ة اﻷف ة وال اق ل ة‬

Creating multiple subplots using plt.subplots


‫في‬ ‫ت‬ ‫ مع ت ف‬، ‫ة اس عاء واح‬ ‫ات الف‬ ‫وش ة م ال‬figure ‫ ش ًﻼ‬pyplot.subplots ‫ي ئ‬
‫ات الف د ة‬ ‫ك ة إن اء ال‬

1- subplots() without arguments returns a Figure and a single Axes.

import matplotlib.pyplot as plt


import numpy as np

x = np.linspace(0, 2 * np.pi, 400)


y = np.sin(x ** 2)

fig, ax = plt.subplots()
fig.suptitle("sub")

ax.plot(x, y)
ax.set_title('A single plot')
plt.show()

Stacking subplots in one direction


fig, axs = plt.subplots(2)
fig.suptitle('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) = plt.subplots(1, 2)
fig.suptitle('Horizontally stacked subplots')
ax1.plot(x, y)
ax2.plot(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 axs.flat:.

fig, axs = plt.subplots(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 axs.flat:
ax.set(xlabel='x-label', ylabel='y-label')

# Hide x labels and tick labels for top plots and y ticks for right plots.
for ax in axs.flat:
ax.label_outer()

:‫قة ثان ة‬
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
fig.suptitle('Sharing x per column, y per row')
ax1.plot(x, y)
ax2.plot(x, y**2, 'tab:orange')
ax3.plot(x, -y, 'tab:green')
ax4.plot(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) = plt.subplots(2)


fig.suptitle('Axes values are scaled individually by default')
ax1.plot(x, y)
ax2.plot(x + 1, -y)
You can use sharex or sharey to align the horizontal or vertical axis.
fig, (ax1, ax2) = plt.subplots(2, sharex=True)
fig.suptitle('Aligning x-axis using sharex')
ax1.plot(x, y)
ax2.plot(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 = plt.subplots(3, sharex=True, sharey=True)
fig.suptitle('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 matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes(projection='3d')
:2‫م ال‬
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
height = np.array([100,110,87,85,65,80,96,75,42,59,54,63,95,71,86])
weight = np.array([105,123,84,85,78,95,69,42,87,91,63,83,75,41,80])
scatter(height,weight)
fig = plt.figure()
ax = plt.axes(projection='3d')
# This is used to plot 3D scatter
ax.scatter3D(height,weight)
plt.title("3D Scatter Plot")
plt.xlabel("Height")
plt.ylabel("Weight")
plt.title("3D Scatter Plot")
plt.xlabel("Height")
plt.ylabel("Weight")
plt.show()

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 matplotlib.pyplot as plt

mpl.rcParams['legend.fontsize'] = 10
fig = plt.figure()
ax = fig.gca(projection='3d')
theta1 = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta1)
y = r * np.cos(theta1)
ax.plot3D(x, y, z, label='parametric curve', color = 'red')
ax.legend()
plt.show()

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.

You might also like