{"id":34503,"date":"2022-09-28T18:24:53","date_gmt":"2022-09-28T18:24:53","guid":{"rendered":"https:\/\/www.askpython.com\/?p=34503"},"modified":"2022-09-28T18:24:55","modified_gmt":"2022-09-28T18:24:55","slug":"3d-plot-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/matplotlib\/3d-plot-in-python","title":{"rendered":"3D Plot in Python: A Quick Guide"},"content":{"rendered":"\n<p>We are going to learn several methods for plotting 3D plots in Python with their appropriate uses. <strong>We are going to work on our Google Colab notebook.<\/strong> Let&#8217;s get into it. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Required Methods for Plotting <\/h2>\n\n\n\n<p>Before getting started with our examples, Let&#8217;s understand the methods as well. We are going to use them further.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">numpy.linespace() <\/h3>\n\n\n\n<p>This method is used to plot values in our required axis. The syntax for <code>numpy.linespace()<\/code> is as follows.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.linspace(start, stop, num = 50, endpoint = True, retstep = False, dtype = None)\n<\/pre><\/div>\n\n\n<p>The Parameters for this method are :<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>start:<\/strong> Beginning of our axes coordinate. <\/li><li><strong>stop: <\/strong>Ending of our axes coordinate.  <\/li><li><strong>num:<\/strong> No. of dot samples to be plotted <\/li><li><strong>restep: <\/strong>If True, return (samples, step). By default restep = False. (Optional)<\/li><li><strong>dtype: <\/strong>type of output array. (Optional)<\/li><\/ul>\n\n\n\n<p>We can better understand by following an example as follow.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nimport pylab as p\n \n# taking Start = 0, End = 2 and num=15 as parameters \nx1 = np.linspace(0, 2, 15, endpoint = True)\ny1 = np.zeros(15) \n \n#taking x axis from -0.2 to 2.1 in our graph\np.xlim(-0.2, 2.1)\n\n#plotting graph\np.plot(x1, y1, &quot;*&quot;)\n<\/pre><\/div>\n\n\n<p>The code snippet will give the result as follows.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"647\" height=\"356\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/Capture-1.png\" alt=\"numpy.linespace() method output\" class=\"wp-image-34512\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/Capture-1.png 647w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/Capture-1-300x165.png 300w\" sizes=\"auto, (max-width: 647px) 100vw, 647px\" \/><figcaption><code>numpy.linespace()<\/code> method output<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">numpy.mgrid<\/h3>\n\n\n\n<p>An\u00a0instance of <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/python-numpy-arrays\" data-type=\"post\" data-id=\"1070\">NumPy library<\/a> that returns a multi-dimensional <code>mesh grid<\/code>. A <code>mesh grid<\/code> is a 2d array with similar values. This method calls the mesh grid method to initialize dense multidimensional arrays. The dimensions and number of the output arrays are equal to the number of indexing dimensions.<\/p>\n\n\n\n<p>The syntax for numpy.mgrid is as follows.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.mgrid = &lt;numpy.lib.index_tricks.nd_grid object&gt;\n<\/pre><\/div>\n\n\n<p>We can better understand the following examples in our code snippet.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; import numpy as np\n&gt;&gt;&gt; new = np.mgrid&#x5B;0:6, 0:4]\n&gt;&gt;&gt; print(new)\n&#x5B;&#x5B;&#x5B;0 0 0 0]\n  &#x5B;1 1 1 1]\n  &#x5B;2 2 2 2]\n  &#x5B;3 3 3 3]\n  &#x5B;4 4 4 4]\n  &#x5B;5 5 5 5]]\n\n &#x5B;&#x5B;0 1 2 3]\n  &#x5B;0 1 2 3]\n  &#x5B;0 1 2 3]\n  &#x5B;0 1 2 3]\n  &#x5B;0 1 2 3]\n  &#x5B;0 1 2 3]]]\n&gt;&gt;&gt; new2 = np.mgrid&#x5B;0:3, 0:5]\n&gt;&gt;&gt; print(new2)\n&#x5B;&#x5B;&#x5B;0 0 0 0 0]\n  &#x5B;1 1 1 1 1]\n  &#x5B;2 2 2 2 2]]\n\n &#x5B;&#x5B;0 1 2 3 4]\n  &#x5B;0 1 2 3 4]\n  &#x5B;0 1 2 3 4]]]\n<\/pre><\/div>\n\n\n<p><strong>Let&#8217;s start plotting our 3D models using different methods. <\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Plotting a 3D model using .plot3D() method<\/h2>\n\n\n\n<p>Plotting our 1st 3D model in Python, we are going to create a Solenoid using python in a 3D graph. Let&#8217;s have a look at our code snippet below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#importing required modules for 3D plotting\nfrom mpl_toolkits import mplot3d\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n#creating our 3D space using projection=3D parameter\nax = plt.axes(projection=&#039;3d&#039;)\n\n#using linespace() method to assign plotting range of z-axis\nzline = np.linspace(10, 200, 1000)\n#taking x-intercept and y-intercept values as sin(zvalue) and cos(zvalue) respectively for different zvalue as radian.  \nxline = np.sin(zline)\nyline = np.cos(zline)\n\n#using plot3D() method by passing 3 axes values and plotting colour as &quot;green&quot; \nax.plot3D(xline, yline, zline, &#039;green&#039;)\n<\/pre><\/div>\n\n\n<p>By following the above code snippet, we can get our output as follows.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"349\" height=\"231\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/example-1-output.png\" alt=\"Example 1 Output\" class=\"wp-image-34505\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/example-1-output.png 349w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/example-1-output-300x199.png 300w\" sizes=\"auto, (max-width: 349px) 100vw, 349px\" \/><figcaption>Example 1 Output<\/figcaption><\/figure>\n\n\n\n<p>You can try the above code snippet by changing the parameter values. You can get various other results and could better understand as well. Let&#8217;s look at another example of a 3D model.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Plotting a 3D model using .scatter3D() method<\/h2>\n\n\n\n<p>Plotting our 2nd 3D model. We are going to create a scattered dotted Solenoid using python in a 3D graph. Let&#8217;s have a look at our code snippet below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#importing modules and creating a 3D as previous example \nfrom mpl_toolkits import mplot3d\nimport numpy as np\nimport matplotlib.pyplot as plt\nax = plt.axes(projection=&#039;3d&#039;)\n\na = 500\n# Data for three-dimensional scattered points\nzdata = 50 * np.random.random(a)\nxdata = np.sin(zdata) + 0.0 * np.random.random(a)\nydata = np.cos(zdata) + 0.0 * np.random.random(a)\n\n#using the scatter3D() method by passing 3 co-ordinates \nax.scatter3D(xdata, ydata, zdata);\n<\/pre><\/div>\n\n\n<p>By following the above code snippet, we can get our output as follows.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"349\" height=\"231\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/example-2-output.png\" alt=\"Example 2 Output\" class=\"wp-image-34506\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/example-2-output.png 349w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/example-2-output-300x199.png 300w\" sizes=\"auto, (max-width: 349px) 100vw, 349px\" \/><figcaption>Example 2 Output<\/figcaption><\/figure>\n\n\n\n<p>By changing values on our same code snippet in parameters, We can expect different plots as follows.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Data for three-dimensional scattered points\na = 400\nzdata = 20 * np.random.random(a)\nxdata = np.sin(zdata) + 0.3 * np.random.random(a)\nydata = np.cos(zdata) + 0.1 * np.random.random(a)\nax.scatter3D(xdata, ydata, zdata);\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/example-2-output-after-changing-parameter-values.png\" alt=\"Example 2 Output After Changing Parameter Values\" class=\"wp-image-34507\" width=\"301\" height=\"199\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/example-2-output-after-changing-parameter-values.png 349w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/example-2-output-after-changing-parameter-values-300x199.png 300w\" sizes=\"auto, (max-width: 301px) 100vw, 301px\" \/><figcaption>Example 2 Output After Changing Parameter Values<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Plotting a surface from a list of tuples<\/h2>\n\n\n\n<p>In this method, we are going to plot the surface for some tuples (Consider tuples as coordinates). <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nfrom matplotlib import pyplot as plt\n\nax = plt.axes(projection=&#039;3d&#039;)\n\n# List of tuples\ntuple_list = &#x5B;(1, 0, 0), (0, 1, 0), (0, 0, 1)]\n\n# Data points from the list of tuples\nx, y, z = zip(tuple_list)\nx, y = np.meshgrid(x, y)\nZ = x ** 2 + y ** 2\n\n# Surface plotting using .plot_surface() method\nax.plot_surface(x, y, Z)\n\nplt.show()\n<\/pre><\/div>\n\n\n<p>By following the above code snippet, we can get our output as follows.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"496\" height=\"244\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/download-1.png\" alt=\"Surface By Tuples\" class=\"wp-image-34513\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/download-1.png 496w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/download-1-300x148.png 300w\" sizes=\"auto, (max-width: 496px) 100vw, 496px\" \/><figcaption>Surface By Tuples<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Plotting a 3D model using .plot_surface() method<\/h2>\n\n\n\n<p>In this method, We are going to plot points on the surface of a sphere in Python using <strong>plot_surface()<\/strong>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nax = plt.axes(projection=&#039;3d&#039;)\n\ngetting the values for spherical points \nu, v = np.mgrid&#x5B;0:2 * np.pi:50j, 0:np.pi:50j]\nx = np.cos(u) * np.sin(v)\ny = np.sin(u) * np.sin(v)\nz = np.cos(v)\n\n#Plotting the surface using plot_surface() method\nax.plot_surface(x, y, z)\nplt.show()\n<\/pre><\/div>\n\n\n<p>By following the above code snippet, We will get the output as below.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/plot_surface-method.png\" alt=\"Plot Surface Method\" class=\"wp-image-34509\" width=\"496\" height=\"244\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/plot_surface-method.png 496w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/plot_surface-method-300x148.png 300w\" sizes=\"auto, (max-width: 496px) 100vw, 496px\" \/><figcaption>Plot Surface Method<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>In this article, we covered how to plot 3D models using Python. We plotted a solenoid, a sphere, and a normal plane. You can try the same code snippets with different values as parameters to get different outputs. We can learn more as well.\u00a0Hope You must have enjoyed it. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>We are going to learn several methods for plotting 3D plots in Python with their appropriate uses. We are going to work on our Google Colab notebook. Let&#8217;s get into it. Required Methods for Plotting Before getting started with our examples, Let&#8217;s understand the methods as well. We are going to use them further. numpy.linespace() [&hellip;]<\/p>\n","protected":false},"author":47,"featured_media":34510,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[95],"tags":[],"class_list":["post-34503","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-matplotlib"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/34503","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/47"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=34503"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/34503\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/34510"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=34503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=34503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=34503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}