{"id":1940,"date":"2019-08-07T12:00:00","date_gmt":"2019-08-07T10:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/plotting-tutorial-in-python-with-matplolib-pyplot-part-1\/"},"modified":"2026-07-05T03:24:12","modified_gmt":"2026-07-05T01:24:12","slug":"plotting-tutorial-in-python-with-matplolib-pyplot-part-1","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/plotting-tutorial-in-python-with-matplolib-pyplot-part-1\/","title":{"rendered":"Plotting Tutorial in Python with Matplolib.pyplot \u2013 Part 1"},"content":{"rendered":"<p>In this practical lesson, I would teach you how to plot in Python. You will learn about plotting in Python with Matplotlib.pyplot.<\/p>\n<p>So, we&#8217;ll cover the following topics. <a href=\"https:\/\/youtu.be\/gWieyVShHHk\" target=\"_blank\" rel=\"noopener\">Watch the video lesson<\/a><\/p>\n<ol>\n<li><a href=\"#t1\">Introduction<\/a><\/li>\n<li><a href=\"#t2\">Basic Plotting<\/a><\/li>\n<li><a href=\"#t3\">Properties of a Line<\/a><\/li>\n<li><a href=\"#t4\">Formatting Your Plot<\/a><\/li>\n<li><a href=\"#t5\">Pyplot Functions<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/plotting-tutorial-in-python-with-matplolib-pyplot-part-2\/\">Plotting Tutorial in Python with Matplolib.pyplot &#8211; Part 2<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t1\">1. Introduction<\/strong><\/h5>\n<p><strong><a href=\"http:\/\/matplotlib.org\/\" target=\"_blank\" rel=\"noopener\">Matplotlib<\/a><\/strong>\u00a0is a Python module for 2D plotting and the\u00a0<strong>matplotlib.pyplot<\/strong>\u00a0sub-module contains many plotting functions to create various kinds of plots. So we begin\u00a0 by importing\u00a0matplotlib.pyplot\u00a0and using\u00a0%matplotib\u00a0Jupyter magic\u00a0to display plots in the notebook.<\/p>\n<p>Before you can plot, you need to import the neccessary modules. You do that using the code below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># you need to import numpy and pyplot<\/span>\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">numpy<\/span> <span style=\"color: #008800; font-weight: bold;\">as<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">np<\/span>\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">matplotlib.pyplot<\/span> <span style=\"color: #008800; font-weight: bold;\">as<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">plt<\/span>\n<span style=\"color: #333333;\">%<\/span>matplotlib inline\n<\/pre>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t2\">2. Basic Plotting<\/strong><\/h5>\n<p>Let&#8217;s look at how to create a basic 2D plot. The procedure is:<\/p>\n<ul>\n<li>Create a sequence that represents x\u00a0values.<\/li>\n<li>Create a sequence the represents the y\u00a0values.<\/li>\n<li>Use\u00a0<strong>plt.plot(x,y,[fmt],**kwargs<\/strong>). [fmt]\u00a0is an (optional) format string while **kwargs\u00a0are also (optional) keyword arguments the specifies the line properties of the plot.<\/li>\n<li>Use\u00a0<strong>pyplot\u00a0<\/strong>functions to add features to the plot such as gridlines,\u00a0 title, legend, etc.<\/li>\n<li>Use\u00a0<strong>plt.show()<\/strong>\u00a0to display the resulting figure.<\/li>\n<\/ul>\n<p><strong>Example i<\/strong>, the code below, produces the line plot shown<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">x <span style=\"color: #333333;\">=<\/span> [<span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">6<\/span>, <span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">5<\/span>, <span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">2<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">3<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">4<\/span>]\ny <span style=\"color: #333333;\">=<\/span> [<span style=\"color: #0000dd; font-weight: bold;\">1<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">2<\/span>, <span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>, <span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">4<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">3<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">2<\/span>]\n\nplt<span style=\"color: #333333;\">.<\/span>plot(x,y)\nplt<span style=\"color: #333333;\">.<\/span>show()\n<\/pre>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1077 size-full\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Plot-1.jpg\" alt=\"\" width=\"550\" height=\"374\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example ii<\/strong>, a second plot. But notice the\u00a0 that the edges of the curve are sharp edges.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">x <span style=\"color: #333333;\">=<\/span> [<span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">3<\/span>, <span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">2<\/span>, <span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">2<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">3<\/span>]\ny <span style=\"color: #333333;\">=<\/span>[<span style=\"color: #0000dd; font-weight: bold;\">5<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">4<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">1<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">1<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">4<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">5<\/span>]\n\nplt<span style=\"color: #333333;\">.<\/span>plot(x, y)\nplt<span style=\"color: #333333;\">.<\/span>show()\n<\/pre>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1078 size-full\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Plot-2.jpg\" alt=\"\" width=\"537\" height=\"373\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example iii<\/strong>, now we would plot a smooth curve by using a larger number of points. The code and curve are shown below<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># Smooth curve is produced by using large number of data points<\/span>\nx <span style=\"color: #333333;\">=<\/span> np<span style=\"color: #333333;\">.<\/span>linspace(<span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">3<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">3<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">200<\/span>)\ny <span style=\"color: #333333;\">=<\/span> x<span style=\"color: #333333;\">**<\/span><span style=\"color: #0000dd; font-weight: bold;\">2<\/span>\n\nplt<span style=\"color: #333333;\">.<\/span>plot(x, y)\nplt<span style=\"color: #333333;\">.<\/span>show()\n<\/pre>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1079 size-full\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Plot3.jpg\" alt=\"\" width=\"552\" height=\"374\" \/><\/p>\n<p>Now, you know how to create basic plots, let&#8217;s now see how to format our plot.<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t3\">3. Properties of a Line<\/strong><\/h5>\n<p>Some of the properties of the line you can set includes, color, alpha(transparency), style, width, markers etc. These properties can be set by giving additional attributes to the plot method. The table below shows that attributes you can use to format your plot<\/p>\n<table style=\"height: 459px;\" width=\"940\">\n<thead>\n<tr style=\"background-color: #f7f6f3;\">\n<td><strong>Property<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>alpha (transparency)<\/td>\n<td>transparency (0.0 transparent through 1.0 opaque)<\/td>\n<\/tr>\n<tr>\n<td>color\u00a0(or\u00a0c)<\/td>\n<td>any matplotlib color<\/td>\n<\/tr>\n<tr>\n<td>label<\/td>\n<td>text appearing in legend<\/td>\n<\/tr>\n<tr>\n<td>linestyle\u00a0(or\u00a0ls)<\/td>\n<td>solid,\u00a0dashed,\u00a0dashdot,\u00a0dotted<\/td>\n<\/tr>\n<tr>\n<td>linewidth\u00a0(or\u00a0lw)<\/td>\n<td>set width of the line<\/td>\n<\/tr>\n<tr>\n<td>marker<\/td>\n<td>set marker style<\/td>\n<\/tr>\n<tr>\n<td>markeredgecolor\u00a0(or\u00a0mec)<\/td>\n<td>any matplotlib color<\/td>\n<\/tr>\n<tr>\n<td>markerfacecolor\u00a0(or\u00a0mfc)<\/td>\n<td>any matplotlib color<\/td>\n<\/tr>\n<tr>\n<td>markersize\u00a0(or\u00a0ms)<\/td>\n<td>size of the marker<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Table 1: Properties of a Plot\u00a0 Line<\/p>\n<h5><\/h5>\n<p>We would apply what we learnt to plot a curve. The curve would be the damped cosine wave curve based on this function<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1081 \" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Damped-cosine-wave.jpg\" alt=\"Damped cosine wave\" width=\"355\" height=\"50\" \/><\/p>\n<p>You should learn how to write math functions in Python format. For example the equation in Python will be:<br \/>\n<em><strong>y = np.exp(-x**2) * np.cos(2 * np.pi * x)<\/strong><\/em><\/p>\n<p>I guess I&#8217;ll cover this in another tutorial.<\/p>\n<p>The complete code is given below. Notice how we have used the attributes specified in Table 1 to set the properties of the curve.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">#generate a sequence of 40 values from -2 to 2<\/span>\nx <span style=\"color: #333333;\">=<\/span> np<span style=\"color: #333333;\">.<\/span>linspace(<span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">2<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">2<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">40<\/span>)\ny <span style=\"color: #333333;\">=<\/span> np<span style=\"color: #333333;\">.<\/span>exp(<span style=\"color: #333333;\">-<\/span>x<span style=\"color: #333333;\">**<\/span><span style=\"color: #0000dd; font-weight: bold;\">2<\/span>)<span style=\"color: #333333;\">*<\/span> np<span style=\"color: #333333;\">.<\/span>cos(<span style=\"color: #0000dd; font-weight: bold;\">2<\/span><span style=\"color: #333333;\">*<\/span>math<span style=\"color: #333333;\">.<\/span>pi<span style=\"color: #333333;\">*<\/span>x) <span style=\"color: #888888;\">#damped cosine wave<\/span>\n\nplt<span style=\"color: #333333;\">.<\/span>plot(x,y, \n         alpha<span style=\"color: #333333;\">=<\/span><span style=\"color: #6600ee; font-weight: bold;\">0.4<\/span>, \n         label<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">'Damped Cosine Wave'<\/span>, \n         color<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">'red'<\/span>, linestyle<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">'dashed'<\/span>, \n         linewidth<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">2<\/span>, \n         marker<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">'*'<\/span>, \n         markersize<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">7<\/span>, \n         markerfacecolor<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">'red'<\/span>,\n         markeredgecolor<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">'blue'<\/span>)\nplt<span style=\"color: #333333;\">.<\/span>ylim([<span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">2<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">2<\/span>])\nplt<span style=\"color: #333333;\">.<\/span>legend()\nplt<span style=\"color: #333333;\">.<\/span>show()\n<\/pre>\n<p>&nbsp;<\/p>\n<p>If you run the code, the resulting curve will be<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1082 size-full\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Decaying-Cosine-Curve.jpg\" alt=\"Decaying Cosine Curve\" width=\"556\" height=\"382\" \/><\/p>\n<p>I recommend, you change of some of the attributes in the code and see how it affects the curve<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t4\">4. Formatting Your Plot<\/strong><\/h5>\n<p>We can you a format string to shortcut to add attributes like color, marker, and linestyle to a line plot. For instance, assuming we want to plot the<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1084 \" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Arctan-Function.jpg\" alt=\"Arctan Function\" width=\"340\" height=\"104\" \/><\/p>\n<p>The equivalent Python code is given below<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">x <span style=\"color: #333333;\">=<\/span> np<span style=\"color: #333333;\">.<\/span>linspace(<span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">5<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">5<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">40<\/span>)\ny <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">\/<\/span>(<span style=\"color: #0000dd; font-weight: bold;\">1<\/span> <span style=\"color: #333333;\">+<\/span> x<span style=\"color: #333333;\">**<\/span><span style=\"color: #0000dd; font-weight: bold;\">2<\/span>)\nplt<span style=\"color: #333333;\">.<\/span>plot(x, y, \n         color<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">'red'<\/span>, \n         marker<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">'s'<\/span>,\n         markerfacecolor<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">'blue'<\/span>)\nplt<span style=\"color: #333333;\">.<\/span>show()\n<\/pre>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1085 size-full\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Plot-4.jpg\" alt=\"\" width=\"563\" height=\"375\" \/><\/p>\n<p>The code below also achieves the same thing<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">x <span style=\"color: #333333;\">=<\/span> np<span style=\"color: #333333;\">.<\/span>linspace(<span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">5<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">5<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">40<\/span>)\ny <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">\/<\/span>(<span style=\"color: #0000dd; font-weight: bold;\">1<\/span> <span style=\"color: #333333;\">+<\/span> x<span style=\"color: #333333;\">**<\/span><span style=\"color: #0000dd; font-weight: bold;\">2<\/span>)\nplt<span style=\"color: #333333;\">.<\/span>plot(x, y, \n         <span style=\"background-color: #fff0f0;\">'rs--'<\/span>,\n        markerfacecolor<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">'blue'<\/span>)\nplt<span style=\"color: #333333;\">.<\/span>show()\n<\/pre>\n<p>&nbsp;<\/p>\n<p>In the code above, &#8216;rs&#8212;&#8216; means:<\/p>\n<ul>\n<li>r stands for a red line<\/li>\n<li>s stands for a square marker<\/li>\n<li>&#8212; stands for a dashed line<\/li>\n<\/ul>\n<p>The tables below give the the shortcuts for other attributes<\/p>\n<p><strong>Shortcuts for colors<\/strong><\/p>\n<table width=\"404\">\n<thead>\n<tr style=\"background-color: #f7f6f3;\">\n<td><strong>Character<\/strong><\/td>\n<td><strong>Color<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>b<\/td>\n<td>blue<\/td>\n<\/tr>\n<tr>\n<td>g<\/td>\n<td>green<\/td>\n<\/tr>\n<tr>\n<td>r<\/td>\n<td>red<\/td>\n<\/tr>\n<tr>\n<td>c<\/td>\n<td>cyan<\/td>\n<\/tr>\n<tr>\n<td>m<\/td>\n<td>magenta<\/td>\n<\/tr>\n<tr>\n<td>y<\/td>\n<td>yellow<\/td>\n<\/tr>\n<tr>\n<td>k<\/td>\n<td>black<\/td>\n<\/tr>\n<tr>\n<td>w<\/td>\n<td>white<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p><strong>Shortcuts for markers<\/strong><\/p>\n<table width=\"345\">\n<thead>\n<tr style=\"background-color: #f7f6f3;\">\n<td>Character<\/td>\n<td>Marker<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>.<\/td>\n<td>point<\/td>\n<\/tr>\n<tr>\n<td>o<\/td>\n<td>circle<\/td>\n<\/tr>\n<tr>\n<td>v<\/td>\n<td>triangle down<\/td>\n<\/tr>\n<tr>\n<td>^<\/td>\n<td>triangle up<\/td>\n<\/tr>\n<tr>\n<td>s<\/td>\n<td>square<\/td>\n<\/tr>\n<tr>\n<td>p<\/td>\n<td>pentagon<\/td>\n<\/tr>\n<tr>\n<td>*<\/td>\n<td>star<\/td>\n<\/tr>\n<tr>\n<td>+<\/td>\n<td>plus<\/td>\n<\/tr>\n<tr>\n<td>x<\/td>\n<td>x<\/td>\n<\/tr>\n<tr>\n<td>D<\/td>\n<td>diamond<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p><strong>Shortcuts for linestyles<\/strong><\/p>\n<table width=\"419\">\n<thead>\n<tr style=\"background-color: #f7f6f3;\">\n<td>Character<\/td>\n<td>Line Style<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>&#8211;<\/td>\n<td>solid line style<\/td>\n<\/tr>\n<tr>\n<td>&#8212;<\/td>\n<td>dashed line style<\/td>\n<\/tr>\n<tr>\n<td>-.<\/td>\n<td>dash-dot line style<\/td>\n<\/tr>\n<tr>\n<td>:<\/td>\n<td>dotted line style<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5><\/h5>\n<h5><strong id=\"t5\">5. Pyplot Functions<\/strong><\/h5>\n<p>There are a number of pyplot\u00a0functions available for us to customize our figures. For example, the table below shows some of them:<\/p>\n<table width=\"828\">\n<thead>\n<tr style=\"background-color: #f7f6f3;\">\n<td><strong>Fucntion<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>plt.xlim<\/td>\n<td>sets the x\u00a0limits<\/td>\n<\/tr>\n<tr>\n<td>plt.ylim<\/td>\n<td>sets the\u00a0 y\u00a0limits<\/td>\n<\/tr>\n<tr>\n<td>plt.grid<\/td>\n<td>adds the grid lines<\/td>\n<\/tr>\n<tr>\n<td>plt.title<\/td>\n<td>adds a title<\/td>\n<\/tr>\n<tr>\n<td>plt.xlabel<\/td>\n<td>adds a label to the horizontal axis<\/td>\n<\/tr>\n<tr>\n<td>plt.ylabel<\/td>\n<td>adds a label to the vertical axis<\/td>\n<\/tr>\n<tr>\n<td>plt.axis<\/td>\n<td>sets axis properties (equal,\u00a0off,\u00a0scaled, etc.)<\/td>\n<\/tr>\n<tr>\n<td>plt.xticks<\/td>\n<td>sets tick locations on the horizontal axis<\/td>\n<\/tr>\n<tr>\n<td>plt.yticks<\/td>\n<td>sets tick locations on the vertical axis<\/td>\n<\/tr>\n<tr>\n<td>plt.legend<\/td>\n<td>displays legend for several lines in the same figure<\/td>\n<\/tr>\n<tr>\n<td>plt.savefig<\/td>\n<td>save figure (as .png, .pdf, etc.) to working directory<\/td>\n<\/tr>\n<tr>\n<td>plt.figure<\/td>\n<td>creates a new figure and set its properties<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Let&#8217;s now apply some of these <strong>pyplot<\/strong> functions to create a plot.<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t6\">6. Example 1 &#8211; Plotting the Taylor&#8217;s Polynomial<\/strong><\/h5>\n<p>Now, we are going to used some the pyplot functions to plot the Taylor&#8217;s Polynomial.<\/p>\n<p>This is also called Taylor&#8217;s Series and <b>it<\/b>\u00a0is a way to represent\u00a0 a\u00a0<a title=\"Function (mathematics)\" href=\"https:\/\/en.wikipedia.org\/wiki\/Function_(mathematics)\" target=\"_blank\" rel=\"noopener\">function<\/a>\u00a0as an\u00a0<a title=\"Series (mathematics)\" href=\"https:\/\/en.wikipedia.org\/wiki\/Series_(mathematics)\" target=\"_blank\" rel=\"noopener\">infinite sum<\/a>\u00a0of terms that are computed from the values of the function&#8217;s\u00a0<a title=\"Derivative\" href=\"https:\/\/en.wikipedia.org\/wiki\/Derivative\" target=\"_blank\" rel=\"noopener\">derivatives<\/a>\u00a0at a single point.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">##################### EXAMPLE 1 - TAYLOR'S POLYNOMIAL #######################<\/span>\n\n<span style=\"color: #888888;\"># Generate a sequence of 50 values from -6 to 6<\/span>\nx <span style=\"color: #333333;\">=<\/span> np<span style=\"color: #333333;\">.<\/span>linspace(<span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">6<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">6<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">50<\/span>) \n\n<span style=\"color: #888888;\"># Plot y = cos(x)<\/span>\ny <span style=\"color: #333333;\">=<\/span> np<span style=\"color: #333333;\">.<\/span>cos(x)\nplt<span style=\"color: #333333;\">.<\/span>plot(x, y, <span style=\"background-color: #fff0f0;\">'b'<\/span>, label<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">'cos(x) '<\/span>, lw<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">3<\/span>) <span style=\"color: #888888;\">#black line<\/span>\n\n<span style=\"color: #888888;\"># Plot order 2 of Taylor's polynomial<\/span>\ny2 <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span> <span style=\"color: #333333;\">-<\/span> x<span style=\"color: #333333;\">**<\/span><span style=\"color: #0000dd; font-weight: bold;\">2<\/span><span style=\"color: #333333;\">\/<\/span><span style=\"color: #0000dd; font-weight: bold;\">2<\/span>\nplt<span style=\"color: #333333;\">.<\/span>plot(x, y2, <span style=\"background-color: #fff0f0;\">'r-.'<\/span>, label<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">'Degree 2'<\/span>, lw<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">3<\/span>) <span style=\"color: #888888;\">#red line<\/span>\n\n<span style=\"color: #888888;\"># Plot order 4 of Taylor's polynomial<\/span>\ny4 <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span> <span style=\"color: #333333;\">-<\/span> x<span style=\"color: #333333;\">**<\/span><span style=\"color: #0000dd; font-weight: bold;\">2<\/span><span style=\"color: #333333;\">\/<\/span><span style=\"color: #0000dd; font-weight: bold;\">2<\/span> <span style=\"color: #333333;\">+<\/span> x<span style=\"color: #333333;\">**<\/span><span style=\"color: #0000dd; font-weight: bold;\">4<\/span><span style=\"color: #333333;\">\/<\/span><span style=\"color: #0000dd; font-weight: bold;\">24<\/span>\nplt<span style=\"color: #333333;\">.<\/span>plot(x, y4, <span style=\"background-color: #fff0f0;\">'g:'<\/span>, label <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">'Degree 4'<\/span>, lw<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">3<\/span>) <span style=\"color: #888888;\">#green line<\/span>\n\n<span style=\"color: #888888;\">#Add some features to the figure<\/span>\nplt<span style=\"color: #333333;\">.<\/span>legend()\nplt<span style=\"color: #333333;\">.<\/span>grid(<span style=\"color: #007020;\">True<\/span>, linestyle<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">':'<\/span>)\nplt<span style=\"color: #333333;\">.<\/span>xlim(<span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">6<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">6<\/span>)\nplt<span style=\"color: #333333;\">.<\/span>ylim(<span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">4<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">4<\/span>)\nplt<span style=\"color: #333333;\">.<\/span>title(<span style=\"background-color: #fff0f0;\">'Taylors Polynomial of cos(x) at x=0'<\/span>)\nplt<span style=\"color: #333333;\">.<\/span>xlabel(<span style=\"background-color: #fff0f0;\">'x values'<\/span>)\nplt<span style=\"color: #333333;\">.<\/span>ylabel(<span style=\"background-color: #fff0f0;\">'y value'<\/span>)\nplt<span style=\"color: #333333;\">.<\/span>show()\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The output of the code is the graph shown below:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1086 size-full\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Taylors-Polynomial.jpg\" alt=\"Taylors Polynomial\" width=\"612\" height=\"425\" \/><\/p>\n<p>Now that you have completed Part 1 of\u00a0Plotting Tutorial in Python with Matplolib.pyplot, thumbs up to you! <a href=\"https:\/\/kindsonthegenius.com\/tempsite\/plotting-tutorial-in-python-with-matplolib-pyplot-part-2\/\">You can now proceed to Part 2.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this practical lesson, I would teach you how to plot in Python. You will learn about plotting in Python with Matplotlib.pyplot. So, we&#8217;ll cover &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[7],"tags":[],"class_list":["post-1940","post","type-post","status-publish","format-standard","hentry","category-python-tutorials"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1940","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1940"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1940\/revisions"}],"predecessor-version":[{"id":2108,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1940\/revisions\/2108"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}