{"id":5411,"date":"2020-10-07T19:27:26","date_gmt":"2020-10-07T13:57:26","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=5411"},"modified":"2020-10-07T19:27:29","modified_gmt":"2020-10-07T13:57:29","slug":"introduction-to-data-visualization-using-matplot","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/introduction-to-data-visualization-using-matplot\/","title":{"rendered":"Introduction to data visualization using matplot"},"content":{"rendered":"\n<p>Matplotlib is one of the most popular Python packages used for data visualization. It is a <a href=\"https:\/\/kivy.org\/\" rel=\"nofollow noopener\" target=\"_blank\">cross-platform library <\/a>for making 2D plots from data in arrays. It provides an object-oriented API that helps in embedding plots in applications using Python GUI toolkits such as PyQt,&nbsp; WxPython Tkinter. It can be used in Python and IPython shells,&nbsp; Jupyter notebook, and web application servers also.&nbsp;<\/p>\n\n\n\n<p><strong>How to install matplot&nbsp;<\/strong><\/p>\n\n\n\n<p>Matplotlib and its dependency packages are available in the form of wheel packages on the standard <a href=\"https:\/\/www.h2kinfosys.com\/blog\/what-is-python-what-are-the-benefits-of-using-python\/\">Python package<\/a> repositories and can be installed on Windows, Linux as well as macOS systems using the pip package manager.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">python -m pip install -U pip &nbsp;\npython -m pip install -U matplotlib&nbsp;<\/pre>\n\n\n\n<p><strong>matplotlib.pyplot <\/strong>is a collection of command style functions that make&nbsp; Matplotlib work like MATLAB. Each pyplot function makes some changes to a figure. For example, a function creates a figure, a plotting area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc.&nbsp;<\/p>\n\n\n\n<p><strong>The following are the list of plots that are available in matplot&nbsp;&nbsp;<\/strong><\/p>\n\n\n\n<p>Plot -It is used to&nbsp; plot lines and\/or markers to the Axes&nbsp;<\/p>\n\n\n\n<p>Scatter Make a scatter plot of x vs y&nbsp;<\/p>\n\n\n\n<p>Bar Make a bar plot&nbsp;<\/p>\n\n\n\n<p>Barh Make a horizontal bar plot&nbsp;<\/p>\n\n\n\n<p>Boxplot Make a box and whisker plot&nbsp;<\/p>\n\n\n\n<p>Hist Plot a histogram&nbsp;<\/p>\n\n\n\n<p>Pie Plot a pie chart&nbsp;<\/p>\n\n\n\n<p>Step Make a step plot&nbsp;<\/p>\n\n\n\n<p>Stem Create a stem plot&nbsp;<\/p>\n\n\n\n<p>Stackplot Draw a stacked area plot<\/p>\n\n\n\n<p><strong>The following are the list of Axis Functions that are available in&nbsp; matplot&nbsp;&nbsp;<\/strong><\/p>\n\n\n\n<p>Axes Add axes to the figure&nbsp;<\/p>\n\n\n\n<p>Text Add text to the axes.&nbsp;<\/p>\n\n\n\n<p>Title Set a title of the current axes&nbsp;<\/p>\n\n\n\n<p>X &#8211; label Set the x-axis label of the current axis&nbsp;<\/p>\n\n\n\n<p>Y &#8211; label Set the y-axis label of the current axis&nbsp;<\/p>\n\n\n\n<p>X &#8211; lim Get or set the x limits of the current axes&nbsp;<\/p>\n\n\n\n<p>Y &#8211; lim Get or set the y limits of the current axes&nbsp;<\/p>\n\n\n\n<p>X &#8211; ticks Get or set the x-limits of the current tick locations and labels. Y &#8211; ticks Get or set the y-limits of the current tick locations and labels.<\/p>\n\n\n\n<p><strong>The following are the list of Figure Functions that are available in&nbsp; matplot&nbsp;&nbsp;<\/strong><\/p>\n\n\n\n<p>Figtext Add text to figure&nbsp;<\/p>\n\n\n\n<p>Figure Creates a new figure<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/deW31yE9WfrDwRFCLGwU-CLk7OARRiiyPZXe_LGJTCUpjDIU5veQ4kCa-qZHKZjJcycJy-N99DQVY6jh-A20hbfHsjAl7jWDCN0Yc-1wYjDkoxRkSrhiXifflti6A1WFsxcW9nrx\" alt=\"Figtext Add text to figure \" title=\"\"><\/figure>\n\n\n\n<p><strong>Sample Plot using matplot<\/strong><strong>&nbsp;&nbsp;<\/strong><\/p>\n\n\n\n<p>Create a new untitled notebook with the <strong>.ipynb <\/strong>extension (stands for &nbsp;the IPython notebook)&nbsp;<\/p>\n\n\n\n<p>We shall now display a simple line plot between 2 arrays&nbsp;To begin with, the Pyplot module from the Matplotlib package is imported, &nbsp;with an alias plt as a matter of convention.<\/p>\n\n\n\n<p><code>import matplotlib.pyplot as plt&nbsp;&nbsp;<\/code><\/p>\n\n\n\n<p>Next we need an array of numbers to plot. Various array functions are &nbsp;defined in the NumPy library which is imported with the np alias.<\/p>\n\n\n\n<p><code>import numpy as np&nbsp;&nbsp;<\/code><\/p>\n\n\n\n<p>Now we will obtain the ndarray objects for x and y&nbsp;<\/p>\n\n\n\n<p><code>x = np.random.randint(5, 50,10)<\/code><\/p>\n\n\n\n<p>## To generate random numbers for plotting<\/p>\n\n\n\n<p><code>y = 2*x<\/code><\/p>\n\n\n\n<p>The values from two arrays are plotted using the plot()<\/p>\n\n\n\n<p><code>function. plt.plot(x,y)&nbsp;&nbsp;<\/code><\/p>\n\n\n\n<p>You can set the plot title, and labels for x and y axes.<\/p>\n\n\n\n<p><code>plt.xlabel(\"X-values\")<br>plt.ylabel(\"Y-values\")<br>plt.title('X vs Y\u2019)&nbsp;&nbsp;<\/code><\/p>\n\n\n\n<p>The Plot viewer window is invoked by the show() function plt.show()&nbsp;<\/p>\n\n\n\n<p>The complete program is as follows &nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np  \nimport matplotlib.pyplot as plt  \nx=np.random.randint(5,50,10)  \ny=2*x  \nplt.xlabel(\"X-values\")  \nplt.ylabel(\"Y-values\")  \nplt.title('X vs Y')  \nplt.plot(x,y) <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><strong>&nbsp;&nbsp;<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/NuT1ecGJOYPsvvlrqjQEstlgMfM-vuiw8cs9W9T6Jr1czz7RmWYeZzaS9GWpe0F-N0pfJRBEPd68DPrqbQffejq0mFkumzyzDrKLa_1a2O1Ga1W5pWxaZPjhfvKZ3-dXA_J70Qjl\" alt=\"Sample Plot using matplot  \" title=\"\"><\/figure>\n\n\n\n<p>To display plot outputs inside the notebook itself and not in the&nbsp; separate viewer , enter the following magic statement<\/p>\n\n\n\n<p><code>%matplotlib inline&nbsp;<\/code><\/p>\n\n\n\n<p><strong>Matplotlib &#8211; PyLab module<\/strong><strong>&nbsp;&nbsp;<\/strong><\/p>\n\n\n\n<p>PyLab is a procedural interface to the Matplotlib object-oriented plotting library. Matplotlib is the whole package; matplotlib.pyplot is a&nbsp; module in Matplotlib, and PyLab is a module that gets installed alongside Matplotlib.&nbsp;<\/p>\n\n\n\n<p>PyLab is a convenience module that imports matplotlib.pyplot (for plotting) and NumPy (for Mathematics and working with arrays) in a &nbsp;single namespace. Although many examples use PyLab, it is no longer recommended.&nbsp;Now we will plot a curve and try to play with the color and markers style<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np  \nimport matplotlib.pyplot as plt  \nx = np.linspace(-3, 3, 30)  \ny = x**2  \nplt.plot(x,y)\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><strong>&nbsp;<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/jgmVfaiivM1_hoVEqg_JWYA7lTxdTS6mbKxipFh5PfnGYTdDw6EkkSE7j9Sx2LgW-c4sagZdnV2GPJf1pG-aTOyMkyylRiqycMha15r0YEu6qUdcTesVPjY22pxINVYm4qg48UNc\" alt=\"Matplotlib - PyLab module  \" title=\"\"><\/figure>\n\n\n\n<p>Supported marker styles :<\/p>\n\n\n\n<p>&nbsp;\u201c<strong>+<\/strong>\u201d , \u201c<strong>,<\/strong>\u201d , \u201c<strong>.<\/strong>\u201d , \u201c<strong>1<\/strong>\u201d , \u201c<strong>2<\/strong>\u201d , \u201c<strong>3<\/strong>\u201d , \u201c<strong>4<\/strong>\u201d&nbsp; Supported line styles :&nbsp;<\/p>\n\n\n\n<p>\u201c<strong>&#8211;<\/strong>\u201d , \u201c<strong>&#8211; &#8211;<\/strong>\u201d , \u201c<strong>-.<\/strong>\u201d , \u201c<strong>:<\/strong>\u201d , \u201c<strong>steps<\/strong>\u201d&nbsp;&nbsp;<\/p>\n\n\n\n<p>Supported colour formats :&nbsp;<\/p>\n\n\n\n<p>\u201c<strong>b<\/strong>\u201d , \u201c<strong>g<\/strong>\u201d , \u201c<strong>r<\/strong>\u201d , \u201c<strong>c<\/strong>\u201d , \u201c<strong>m<\/strong>\u201d , \u201c<strong>y<\/strong>\u201d , \u201c<strong>k<\/strong>\u201d , \u201c<strong>w<\/strong>\u201d&nbsp;&nbsp;<\/p>\n\n\n\n<p>To plot symbols rather than lines and change colour , we need to&nbsp; provide an additional string argument.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np  \nimport matplotlib.pyplot as plt  \nx = np.linspace(-3, 3, 30)  \ny = x**2  \n## \u201cg\u201d represents the colour of the graph  \nplt.plot(x,y,\u2019g\u2019)  \n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/O1HWdq5AwV7woqvfuW7pa85Pj3-B8sE7IEmbNTQDdE75rkl6I4OTg9wwriWd7ntEulg22IhG1V8T2fuPkSv0YycDjor6B35IxrLe_ruDBP-kh5w2Fs7xI4N3sV4AHrwj2pkMLhVd\" alt=\"Supported colour formats\" title=\"\"><\/figure>\n\n\n\n<p>Now we will change the marker style of the above graph.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np  \nimport matplotlib.pyplot as plt  \nx = np.linspace(-3, 3, 30)  \ny = x**2  \n## \u201cr\u201d represents the colour and \u201c-o\u201d represents the marker style plt.plot(x,y,\u2019r-o\u2019)<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><strong>&nbsp;&nbsp;<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/dXpBFJJZla3uzNr57fhbmTcIunYCIF0IJj2XN6rRUJ7MVtk-zyQsEMV-TwLHnB0fr_o5L0gbApome0bwPhhjj0FYzxfC-aqPwjvW0ql9vg97YBgJRIETVBtg4GL5PW6FbK1ZYnpz\" alt=\"data visualization using matplot\" title=\"\"><\/figure>\n\n\n\n<p><strong>Plotting curves of given equation&nbsp;&nbsp;<\/strong><\/p>\n\n\n\n<p>Here, we use NumPy which is a general-purpose array-processing&nbsp; package in python.&nbsp;<\/p>\n\n\n\n<p>\u2022 To set the x-axis values, we use the <strong>np.arange() <\/strong>method in which the first two arguments are for range and the third one for step-wise increment. The result is a numpy array.&nbsp;<\/p>\n\n\n\n<p>\u2022 To get corresponding y-axis values, we simply use the predefined <strong>np.sin() <\/strong>method on the numpy array.&nbsp;<\/p>\n\n\n\n<p>\u2022 Finally, we plot the points by passing x and y arrays to the&nbsp; <strong>plt.plot() <\/strong>function.<\/p>\n\n\n\n<p><code>x = np.arange(0, 2*(np.pi), 0.1)&nbsp; y = np.sin(x) &nbsp;<br>plt.plot(x, y)<\/code><\/p>\n\n\n\n<p><strong>Output:<\/strong><strong>&nbsp;&nbsp;<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/kVHGuVOG4fK8Qyuf9guDz3ISLCKCn2-OmEwdynJVDconIY8SQ-dbefWst-MG1ruw0hKgp_9cnvd5gwjbY2IftQEoJIqooE89rcWMWyoWDB2tTRb5tmmDMIYo68hybEeOKAck25V8\" alt=\"Plotting curves of given equation  \" title=\"Plotting curves of given equation  \"\/><\/figure>\n\n\n\n<p><code>x = np.linspace(-3, 3, 30)&nbsp;&nbsp;<br>y = np.linspace(-3, 3, 20)&nbsp;&nbsp;<br>plt.plot(x, sin(x),'g--')&nbsp;&nbsp;plt.scatter(y,sin(y),c='b',marker='o')&nbsp; plt.show()<\/code><\/p>\n\n\n\n<p><strong>Output:<\/strong><strong>&nbsp;<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/2R82FpxVe50oT87PxMZvZTaEPqhqjmXw7ea4FR5PUqq9V0zudDfd7cr0axPPV8Hx9wpaFLaauKnhCfKyZoPY-M0JuVH3EBxkFtV0RjTJ1s5_lvbY8vZxHKuDa4YeW-wV9MeD6zW1\" alt=\"data visualization using matplot\" title=\"\"><\/figure>\n\n\n\n<p>Plots can be overlaid. Just use the multiple plot commands.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = np.linspace(-3, 3, 30)  \nplt.plot(x, sin(x),\u2019b\u2019)  \nplt.plot(x, cos(x), \u2018r\u2019)  \nplt.plot(x, -sin(x), \u2018g\u2019)  \nplt.legend(&#91;\u2018Sin\u2019,\u2019Cos','-Sin'])\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><strong>&nbsp;&nbsp;<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/KgIlKYNJ_xYsuAuGTahPWmRx9Eng6iXHTW-R2YfW5Z-fw6mh31uOfdpawucuUcp9jZsUcjyFLDhqbvRKlHn-H6_aV_lCsNzKgJbA_Rh7Q4KYu7bhlc0s_fA_q0yBN-LlNxNuQ09b\" alt=\"data visualization using matplot\" title=\"\"><\/figure>\n\n\n\n<p>\u2022 Here, we plot three curves on the same graph. We differentiate between them by giving them a name(<strong>label<\/strong>) which is passed as an argument of &nbsp;<strong>.plot() <\/strong>function.&nbsp;<\/p>\n\n\n\n<p>\u2022 The small rectangular box giving information about the type of line and its color is called a legend. We can add a legend to our plot&nbsp; using <strong>.legend() <\/strong>function.<\/p>\n\n\n\n<p><strong>Multiple Subplots <\/strong><strong>&nbsp;<\/strong><\/p>\n\n\n\n<p>Sometimes it is helpful to compare different views of data side by side.&nbsp; To this end, Matplotlib has the concept of subplots: groups of smaller axes that can exist together within a single figure. These subplots might be insets, grids of plots, or other more complicated layouts. In this section, we&#8217;ll explore four routines for creating subplots in Matplotlib.&nbsp;This is the general syntax for subplots&nbsp;&nbsp;<\/p>\n\n\n\n<p><code>subplot(nrows, ncols, index)&nbsp;&nbsp;<\/code><\/p>\n\n\n\n<p>Now we will try to create a empty 2 X 2 layout<\/p>\n\n\n\n<p><code>plt.subplots(2,2)<\/code><\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/nh1DHcfMtDisotLI6OzWsYO3cTYmlYAVBseJ97g5OtSb9pFahxVl8-DnyKEzqeRYBOd5rljDHLV-EGdKN9yfyPeqittOu0Fqpswzu1Hv_iQAlLU0O8rl5Tzsd9I0hsStiDKDUUJI\" alt=\"Multiple Subplots  \" title=\"\"><\/figure>\n\n\n\n<p>Now we will try to plot graphs using subplot function&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = np.linspace(-3, 3, 30)  \nplt.subplot(2,2,1)  \nplt.plot(x, sin(x),\u2019b\u2019)  \nplt.subplot(2,2,2)  \nplt.plot(x, cos(x),\u2019r\u2019)  \nplt.subplot(2,2,3)  \nplt.plot(x, -sin(x),\u2019g\u2019)  \nplt.subplot(2,2,4)  \nplt.plot(x, -cos(x),\u2019y')  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><strong>&nbsp;<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/qDIo_K_EYCcQcUelzpOljPJYEH5l-Z8XkXQcdtrPAXXCt1xQeA3n6G_sHQmHfXWYfGekcEEEEbxyKhKF26G4e_8qBs1emvmNaPD9QJQSKwJlEh-UELoySk6fIf8_fhBDvmNGDI77\" alt=\"data visualization using matplot\" title=\"\"><\/figure>\n\n\n\n<p>In the next article, we will learn how to visualize the remaining types of plots.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Matplotlib is one of the most popular Python packages used for data visualization. It is a cross-platform library for making 2D plots from data in arrays. It provides an object-oriented API that helps in embedding plots in applications using Python GUI toolkits such as PyQt,&nbsp; WxPython Tkinter. It can be used in Python and IPython [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":5429,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[500],"tags":[],"class_list":["post-5411","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-science-using-python-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/5411","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=5411"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/5411\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/5429"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=5411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=5411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=5411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}