Output: Let’s walk through this program step by step: Graph styles can be customized by setting the various styles available or by setting your own. You can read more about this function here The picture acts as a top-level container for all chart elements. So we define the figure as a pic, which will contain all of our subplot. Here we use the fig.add_subplot method to determine the subplots and their positions. The prototype of the function looks like this: If a subplot is applied to a figure, the figure will be conditionally divided on the "nrows" * "ncols" sub-axis. The plot_number parameter identifies the plot that the function call should create. & # 39; plot_number & # 39; can range from 1 to a maximum of & # 39; nrows & # 39; * & # 39; ncols & # 39 ;. If the three parameters are less than 10, the function subplot can be called with one int parameter, where hundreds represent "nrows", tens represent "ncols", and ones represent "Plot_number". This means: instead of subparagraph (2, 3, 4) we can write subparagraph (234) . This figure will clarify how the positions are indicated: Next, we plot our points at each site. First, we generate the x and y axis coordinates using the create_plot function, specifying the type of curve we want. This is another utility method that creates space between parcels. Finally, we call the plt.show () method, which will show the current indicator. Method 2 # import required modules
import
matplotlib.pyplot as plt
import
numpy as np
# function to generate coordinates
def
create_plot (ptype) :
# setting the x-axis
x
=
np.arange (
-
10
,
10
,
0.01
)
# set axis values Y
if
ptype
=
=
’linear’
:
y
=
x
elif
ptype
=
=
’quadratic’
:
y
=
x
*
*
2
elif
ptype
=
=
’cubic’
:
y
=
x
*
*
3
elif
ptype
=
=
’quartic’
:
y
=
x
*
*
4
return
(x, y)
# setting the style to use
plt.style.use (
’ fivethirtyeight’
)
# create shape
fig
=
plt.figure ()
# define subplots and their positions on picture
plt1
=
fig.add_subplot (
221
)
plt2
=
222
)
plt3
=
fig.add_subplot (
223
)
plt4
=
fig.add_subplot (
224
)
# drawing points on each site
x, y
=
create_plot (
’linear’
)
plt1.plot (x, y, color
=
’r’
)
plt1.set_title (
’$ y_1 = x $’
)
x, y
=
create_plot (
’quadratic’
)
plt2.plot (x, y, color
=
’b’
)
plt2.set_title (
’$ y_2 = x ^ 2 $ ’
)
x, y
=
create_plot (
’cubic’
)
plt3.plot (x, y, color
=
’g’
)
plt3.set_title (
’$ y_3 = x ^ 3 $’
)
x, y
=
create_plot (
’quartic’
)
plt4.plot (x, y, color
=
’k’
)
plt4.set_title (
’$ y_4 = x ^ 4 $’
)
# adjust the distance between areas
fig.subplots_adjust (hspace
=
.
5
, wspace
=
0.5
)
# plot show function
plt.show ()
plt.style.use (’fivethirtyeight’)
fig = plt.figure ( )
plt1 = fig.add_subplot (221) plt2 = fig.add_subplot ( 222) plt3 = fig.add_subplot (223) plt4 = fig.add_subplot (224)
add_subplot (nrows, ncols, plot_number)
x, y = create_plot (’linear’) plt1.plot (x, y, color =’ r’) plt1.set_title (’$ y_1 = x $’)
Then we plot these points on our plot, using the .plot method. The title of the subplot is set using the set_title method. Using $ at the beginning and end of the heading text ensures that "_" (underscore) reads as index and "^" reads as superscript. fig.subplots_adjust (hspace = .5, wspace = 0.5)
plt. show ()
|
Output:
Let’s go through the important parts of this program:
-
plt1 = plt.subplot2grid ((11,1), (0,0), rowspan = 3, colspan = 1) plt2 = plt.subplot2grid ((11,1), (4,0 ), rowspan = 3, colspan = 1) plt3 = plt.subplot2grid ((11,1), (8,0), rowspan = 3, colspan = 1)
subplot2grid s trong> is similar to "pyplot.subplot", but uses 0-based indexing and allows the subplot to span multiple cells.
Let’s try to understand the arguments of the subplot2grid method:
1. argument 1: grid geometry
2. argument 2: position of the plot in the grid
3. argument 3 : (row of lines) The number of lines covered by the subplot.
4.argument 4: (colspan) The number of columns covered by the subplot.This number will make this concept clearer:
In our example, each subplot spans 3 rows and 1 column with two blank rows (row # 4,8).
-
x, y = create_plot (’sin’) plt1.plot (x, y, label =’ sine wave’, color = ’b’)
Nothing special about this part, as the syntax for plotting points in the auxiliary plot remains unchanged.
-
plt1.legend ()
This will show the caption for the plot in the picture.
-
plt.show ()
Finally, we call the plt.show () function to show the current graph.
Note: After examining the above two examples, we can conclude that it follows and use the subplot () method when the plots are the same size, whereas the subplot2grid () method should be preferred when we want more flexibility regarding the position and size of our child parcels.
3-D drawing
We can plot 3D figures easily in matplotlib. We will now discuss some important and commonly used 3-D graphics.
- Print Dots
from
mpl_toolkits.mplot3d
import
axes3d
import
matplotlib.pyplot as plt
from
matplotlib
import
style
import
numpy as np
# set your own style to use
style.use (
’ggplot’
)
# create a new shape to draw
fig
=
plt.figure ()
# create a new site on our shape
# and set the perspective to 3d
ax1
=
fig.add_subplot (
111
, projection
=
’3d’
)
# define x, y, z coordinates
x
=
np.random.randint (
0
,
10
, size
= cod e>
20
)
y
=
np.random.randint (
0
,
10
, size
=
20
)
z
=
np.random.randint (
0
,
10
, size
=
20
)
# drawing dots on plot
# set axis labels
a x1.set_xlabel (
’x-axis’
)
ax1.set_ylabel (
’ y-axis’
)
ax1.set_zlabel (
’z-axis’
)
# plot show function
plt.show ( )
The output of the above program will provide you with a window that can rotate or enlarge the plot. Here’s a screenshot: (dark points are closer than light ones)
Let’s try to understand some important aspects of this code now .
from mpl_toolkits.mplot3d import axes3d
This is the module required to plot 3D space.
ax1 = fig.add_subplot (111, projection = ’3d’ )
Here we create a plot on our shape and set the projection argument to 3d.
ax1.scatter (x, y, z, c = ’m’, marker =’ o’ )
We now use the .scatter () function to plot points in the XYZ plane.
- Dot lines
# import required modules
from
mpl_toolkits.mplot3d
import
axes3d
import
matplotlib.pyplot as plt
from
matplotlib
import
style
import
numpy as np
# set your own style to use
style.use (
’ggplot’
)
# create a new shape for construction
fig
=
plt.figure ()
# create a new parcel on our shape
ax1
=
fig.add_subplot (
111
, projection
=
’ 3d’
)
# definition of x, y, z coordinates
x
=
np.random.randint (
0
,
10
, size
=
5
)
y
=
np.random.randint (
0
,
10
, siz e
=
5
)
z
=
np.random.randint (
0
,
10
, size
=
5
)
# plotting points on the area
ax1.plot_wireframe (x, y, z)
# tagging
ax1.set_xlabel (
’x-axis’
)
ax1.set_ylabel (
’y-axis’
)
ax1.set_zlabel (
’z-axis’ co de>
)
plt.show ()
A screenshot of the 3D plot of the above program will look like this:
The main difference between this program and the previous one:
ax1.plot_wireframe (x, y, z)
We used the .plot_wireframe () method to draw lines on a given set of 3D points.
- Plotting bars
# import required modules
from
mpl_toolkits.mplot3d
import
axes3d
import
matplotlib.pyplot as plt
from
matplotlib
import
style
import
numpy as np
# set your own style to use
style.use (
’ ggplot’
)
# create a new shape to draw
fig
=
plt.figure ()
# create a new plot on our shape
ax1
=
fig.add_subplot (
111
, projection
=
’3d’
)
undefined spaces ">
# set your own style to use
style.use (
’ggplot’
)
# create a new shape to draw
fig
=
Shop
Latest questions
Wiki