Plotting in Python | Set 2

File handling | NumPy | Python Methods and Functions

# 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 = fig.add_subplot ( 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 ()

Output:

Let`s walk through this program step by step:

  •  plt.style.use (`fivethirtyeight`) 

    Graph styles can be customized by setting the various styles available or by setting your own. You can read more about this function here

  •  fig = plt.figure ( ) 

    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.

  •  plt1 = fig.add_subplot (221) plt2 = fig.add_subplot ( 222) plt3 = fig.add_subplot (223) plt4 = fig.add_subplot (224) 

    Here we use the fig.add_subplot method to determine the subplots and their positions. The prototype of the function looks like this:

     add_subplot (nrows, ncols, plot_number) 

    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:

  •  x, y = create_plot (`linear`) plt1.plot (x, y, color =` r`) plt1.set_title (`$ y_1 = x $`) 

    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.
    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) 

    This is another utility method that creates space between parcels.

  •  plt. show () 

    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 ( 0 , 5 , 0.01 )

  

# set Y-axis values ​​

if ptype = = `sin` :

# sinusoid

y = np.sin ( 2 * np.pi * x)

  elif ptype = = ` exp` :

  # negative exponential function

y = np.exp ( - x)

elif ptype = = `hybrid` :

  # decaying sine wave

  y = (np.sin ( 2 * np.pi * x) ) * (np.exp ( - x))

 

return (x, y)

 
# setting the style to use

plt.style.use ( `ggplot` )

  
# definition of subplots and their positions th

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 )

 
# plotting points on each site

x, y = create_plot ( `sin` )

plt1.plot (x, y, label = `sine wave` , color = `b` )

x, y = create_plot ( `exp` )

plt2.plot (x, y, label = `negative exponential` , color = `r` )

x, y = create_plot ( ` hybrid` )

plt3.plot (x, y, label = `damped sine wave` , color = ` g` )

 
# show legends for each plot
plt1.legend ()
plt2.legend ()
plt3.legend ()

 
# plot show function
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 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.