matplotlib-2-1
matplotlib-2-1
.ة إلى ال س ال اني ال ﻹضافة خ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.plot(x, y)
plt.grid()
plt.show()
fig, ax = plt.subplots()
fig.suptitle("sub")
ax.plot(x, y)
ax.set_title('A single plot')
plt.show()
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:.
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.
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()
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()
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.
xlabel("string") It is used to set the label for the x-axis as specified by the
string.
xtricks(index, categorical It is used to set or get the current tick locations labels of the
variables) x-axis.
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.
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.