{"id":16966,"date":"2022-04-26T09:30:57","date_gmt":"2022-04-26T09:30:57","guid":{"rendered":"https:\/\/linuxways.net\/?p=16966"},"modified":"2022-04-26T10:59:37","modified_gmt":"2022-04-26T10:59:37","slug":"how-to-plot-a-function-in-python","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/scripting\/how-to-plot-a-function-in-python\/","title":{"rendered":"How to Plot a Function in Python"},"content":{"rendered":"<p>The matplotlib.pyplot module&#8217;s plot() method offers a single interface for constructing many sorts of graphs. This function accepts any two arguments, in our case, x and y, and generates a visual representation of the relevant points in x and y. Following these procedures, we may plot a function:<\/p>\n<ol>\n<li>First of all, we import the pyplot function from the matplotlib module.<\/li>\n<li>Then we define values for the x and y variables, which are dependent on the type of function to be plotted.<\/li>\n<li>Then we use the plt.plot() function to draw the figure.<\/li>\n<li>Lastly, we use the plt.show() function to display the plot.<\/li>\n<\/ol>\n<p>Let\u2019s consider the following examples for plotting functions.<\/p>\n<h2><strong>Example 1: Identity function<\/strong><\/h2>\n<pre>From matplotlib, import pyplot as plt.\r\n\r\nx = [0, 1, 2, 3, 4, 5]\r\n\r\ny=x\r\n\r\nplt.plot(x, y)\r\n\r\nplt.show()<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"308\" height=\"112\" class=\"wp-image-16967\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-330.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-330.png 308w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-330-300x109.png 300w\" sizes=\"auto, (max-width: 308px) 100vw, 308px\" \/><br \/>\nFigure 1: Identity Function<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"570\" height=\"430\" class=\"wp-image-16968\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-331.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-331.png 570w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-331-300x226.png 300w\" sizes=\"auto, (max-width: 570px) 100vw, 570px\" \/><\/p>\n<p>In an identical function, values of x and y are the same, so we get a straight line plot as shown in figure 2.<\/p>\n<h2><strong>Example 2: Sine function<\/strong><\/h2>\n<pre>from matplotlib import pyplot as plt\r\n\r\nimport numpy as np\r\n\r\nx = np.linspace(0, 10, 50)\r\n\r\nplt.plot(x, np.sin(x))\r\n\r\nplt.show()<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"305\" height=\"131\" class=\"wp-image-16969\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-332.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-332.png 305w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-332-300x129.png 300w\" sizes=\"auto, (max-width: 305px) 100vw, 305px\" \/><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"594\" height=\"439\" class=\"wp-image-16970\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-333.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-333.png 594w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-333-300x222.png 300w\" sizes=\"auto, (max-width: 594px) 100vw, 594px\" \/><\/strong><\/p>\n<p>In example 2, we are using the NumPy module. As x is generated using the np.linespace function, which returns evenly spaced numbers between 0 and 1 over a specified interval. Here, y is also the sine function of the NumPy module. The plot for the sine function is shown in figure 4.<\/p>\n<h2><strong>Example 3: Quadratic function<\/strong><\/h2>\n<pre>from matplotlib import pyplot as plt\r\n\r\nimport numpy as np\r\n\r\nx =np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])\r\n\r\na = 3\r\n\r\nb = 8\r\n\r\nc = 9\r\n\r\ny = a * (x ** 2) + b * x + c\r\n\r\nprint('Values of x: ', x)\r\n\r\nprint('Values of y: ', y)\r\n\r\nplt.plot(x, y)\r\n\r\nplt.title(\"Quadratic Function\")\r\n\r\nplt.xlabel(\"Values of x\")\r\n\r\nplt.ylabel(\"Values of y\")\r\n\r\nplt.show()<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"393\" height=\"333\" class=\"wp-image-16971\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-334.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-334.png 393w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-334-300x254.png 300w\" sizes=\"auto, (max-width: 393px) 100vw, 393px\" \/><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"537\" height=\"47\" class=\"wp-image-16972\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-335.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-335.png 537w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-335-300x26.png 300w\" sizes=\"auto, (max-width: 537px) 100vw, 537px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"589\" height=\"453\" class=\"wp-image-16973\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-336.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-336.png 589w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-336-300x231.png 300w\" sizes=\"auto, (max-width: 589px) 100vw, 589px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>In example 3, we have an x variable list which is converted to a matrix using the array function of the numpy module. The value of y is calculated by inserting values of variables and constants into a quadratic equation. The output of the quadratic function is shown in figure 6. A plot is drawn for quadratic function as shown in figure 7.<\/p>\n<h2><strong>Example 4: Cubic Function<\/strong><\/h2>\n<pre>from matplotlib import pyplot as plt\r\n\r\nimport numpy as np\r\n\r\nx =np.array([-4, -3, -2, -1, 0, 1, 2, 3, 4])\r\n\r\na = 2\r\n\r\nb = 4\r\n\r\nc = 6\r\n\r\nd = 8\r\n\r\ny = a * (x ** 3) + b * (x ** 2) + c * x + d\r\n\r\nprint('Values of x: ', x)\r\n\r\nprint('Values of y: ', y)\r\n\r\nplt.plot(x, y)\r\n\r\nplt.xlabel(\"Values of x\")\r\n\r\nplt.ylabel(\"Values of y\")\r\n\r\nplt.title(\"Cubic Function\")\r\n\r\nplt.show()<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"448\" height=\"333\" class=\"wp-image-16974\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-337.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-337.png 448w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-337-300x223.png 300w\" sizes=\"auto, (max-width: 448px) 100vw, 448px\" \/><br \/>\n<strong>Output:<\/strong><\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"427\" height=\"55\" class=\"wp-image-16975\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-338.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-338.png 427w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-338-300x39.png 300w\" sizes=\"auto, (max-width: 427px) 100vw, 427px\" \/><\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"593\" height=\"462\" class=\"wp-image-16976\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-339.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-339.png 593w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-339-300x234.png 300w\" sizes=\"auto, (max-width: 593px) 100vw, 593px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>In example 4, y represents the cubic equation. Values for cubic functions are calculated and displayed in figure 9. A cubic function plot is drawn in figure 10.<\/p>","protected":false},"excerpt":{"rendered":"<p>The matplotlib.pyplot module&#8217;s plot() method offers a single interface for constructing many sorts of graphs. This function accepts any two arguments, in our case, x and y, and&hellip;<\/p>","protected":false},"author":1,"featured_media":17009,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[168],"tags":[992,10],"class_list":["post-16966","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-plot-function","tag-python"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/16966","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=16966"}],"version-history":[{"count":1,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/16966\/revisions"}],"predecessor-version":[{"id":17050,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/16966\/revisions\/17050"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/17009"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=16966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=16966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=16966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}