  # 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. 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 = 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` )   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 = (adsbygoogle = window.adsbygoogle || []).push({}); Books for developers Smart Grid using Big Data Analytics: A Random Matrix Theory Approach Data is “unreasonably effective”. Nobel laureate Eugene Wigner referred to the unreasonable effectiveness of mathematics in the natural sciences. What is big data? Its sizes are in the order of te... 10/07/2020 Designing Data Visualizations Since you are interested in learning more about designing data visualizations (by virtue of the fact that you’re reading this book), then chances are good that you have been the reader of other peop... 10/07/2020 Big Data and the Internet of Things The genesis of this book began in 2012. Hadoop was being explored in mainstream organizations, and we believed that information architecture was about to be transformed. For many years, business intel... 10/07/2020 Python for Programmers Python for Programmers: with Big Data and Artificial Intelligence Case Studies This book, written for programmers with a high-level experience in another language, uses how-to instructions to teach... 08/08/2021 Get Solution for free from DataCamp guru © 2021 Python.Engineering Best Python tutorials books for beginners and professionals Python.Engineering is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to amazon.com Computations Development Cryptography For dummies Machine Learning Big Data Loops Counters NumPy NLP PHP Regular Expressions File Handling Arrays String Variables Knowledge Database X Submit new EBook \$(document).ready(function () { \$(".modal_galery").owlCarousel({ items: 1, itemsCustom: false, itemsDesktop: [1300, 1], itemsDesktopSmall: [960, 1], itemsTablet: [768, 1], itemsTabletSmall: false, itemsMobile: [479, 1], singleItem: false, itemsScaleUp: false, pagination: false, navigation: true, rewindNav: true, autoPlay: true, stopOnHover: true, navigationText: [ "<img class='img_no_nav_mob' src='/wp-content/themes/nimani/image/prevCopy.png'>", "<img class='img_no_nav_mob' src='/wp-content/themes/nimani/image/nextCopy.png'>" ], }); \$(".tel_mask").mask("+9(999) 999-99-99"); }) ```