{"id":82540,"date":"2025-07-01T13:28:19","date_gmt":"2025-07-01T07:58:19","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=82540"},"modified":"2025-09-10T15:14:45","modified_gmt":"2025-09-10T09:44:45","slug":"fundamentals-of-matplotlib","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/fundamentals-of-matplotlib\/","title":{"rendered":"Getting Started with Matplotlib: Fundamentals Explained"},"content":{"rendered":"\n<p>Matplotlib is one of the Python libraries that is open source and is used to visualize data in the form of charts, graphs, plots, etc. The graphical output is easy to observe and understand by any user.<\/p>\n\n\n\n<p>Here in this blog, we\u2019re learning the basics of this data visualization library, which covers fundamental features such as line plots, scatter plots, histograms, pie charts, and bar graphs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Getting started with Matplotlib<\/h2>\n\n\n\n<p>Before moving into the coding part, let us learn about pyplot. pyplot, an application interface (API), is a submodule of <a href=\"https:\/\/matplotlib.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">Matplotlib<\/a> in the <a href=\"https:\/\/www.guvi.in\/blog\/python-libraries-for-machine-learning\/\" target=\"_blank\" rel=\"noreferrer noopener\">Python library<\/a> that allows us to make changes to figures, such as creating labels, the style of our plots, representing in various colors, etc.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Installing Matplotlib<\/h3>\n\n\n\n<p><strong>Using pip:<\/strong> Open the command prompt \u2192 type the below command.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>pip install matplotlib<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Using Anaconda:<\/strong> Open the Anaconda prompt \u2192 type the below command.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>conda install matplotlib<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Check Version<\/h3>\n\n\n\n<p>After successfully installing matplotlib, check the version by executing the command given below to verify the installation.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>import matplotlib<br>print(matplotlib.__version__)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Import pyplot<\/h3>\n\n\n\n<p>Import pyplot from Matplotlib by executing the command given below.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>from matplotlib import pyplot<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Let\u2019s walk through the types of plots along with the example program.<\/p>\n\n\n\n<p>Interested in learning more about <a href=\"https:\/\/www.guvi.in\/blog\/benefits-of-learning-python\/\" target=\"_blank\" rel=\"noreferrer noopener\">Python programming<\/a>? HCL Guvi provides an IIT-Madras-certified <a href=\"https:\/\/www.guvi.in\/zen-class\/full-stack-development-course\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=Getting+Started+With+Matplotlib\" target=\"_blank\" rel=\"noreferrer noopener\">Full Stack Development Course<\/a>. This is an online bootcamp that covers all the necessary skills required for a full-stack developer. This bootcamp follows a detailed roadmap and an updated curriculum with <a href=\"https:\/\/www.guvi.in\/blog\/list-of-free-ai-tools\/\" target=\"_blank\" rel=\"noreferrer noopener\">AI tools<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Types of Plots in Matplotlib<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1200\" height=\"630\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/07\/Types-of-Plots-in-Matplotlib.jpg\" alt=\"Types of Plots in Matplotlib\" class=\"wp-image-83738\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/07\/Types-of-Plots-in-Matplotlib.jpg 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/07\/Types-of-Plots-in-Matplotlib-300x158.jpg 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/07\/Types-of-Plots-in-Matplotlib-768x403.jpg 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/07\/Types-of-Plots-in-Matplotlib-150x79.jpg 150w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">1. Line Plot<\/h3>\n\n\n\n<p>A line plot is a type of plot that represents the data as dots and is connected by line segments.<\/p>\n\n\n\n<p><strong>Example:<\/strong> In the below code, we are plotting (0,0) and (6,100) using a numpy array.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>from matplotlib import pyplot as plt<br>import numpy as np<br>x=np.array([0,6])<br>y=np.array([0,100])<br>plt.plot(x,y)<br>plt.show()<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXe3e5kwwisoetZMFqieZcUsgOPa4RwNexkb_iM4_tpL0MuxdmN1hbcGoOWBNqmbhSvFOF9enEPlrpZ5_tiX1rSj2VF3XXXCUrcYQDQ7fOig3P3by7zijpZVpQsf4kO47S-Mu2nn2A?key=lf4UuhQ1qc-Xv5AnBzgArw\" alt=\"simple line plot\" title=\"\"><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">2. Scatter Plot<\/h3>\n\n\n\n<p>A scatter plot is a type of plot that displays the correlation between two variables.<\/p>\n\n\n\n<p><strong>Example:<\/strong> Let us take a simple example of a weather plot with day and temperature.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>day=[1,2,3,4,5,6,7]<br>temp=[50,30,20,80,70,45,55]<br>plt.title(&#8220;Weather plot&#8221;)<br>plt.xlabel(&#8216;day&#8217;)<br>plt.ylabel(&#8216;temperature&#8217;)<br>plt.scatter(day,temp,color=&#8217;y&#8217;,s=100) #s-size of the plot<br>plt.figure(figsize=(3,3),dpi=100) #canvas size can be modified,dpi-resolution can be fixed<br>plt.show()<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXclAeQHes287nfDcgSge8Iw2aniv2DFL1f6ZOvI9Y8T1XdoLALN2V33jhF3jnoKx-R8J_EmO1HFraWm8g8-ZtrUOrlv_LtCU9q0rAcVoGGB04igb3qeyNxSgmlHL_kUFE7RfK6o?key=lf4UuhQ1qc-Xv5AnBzgArw\" alt=\"scatter plot\" title=\"\"><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">3. Bar Chart<\/h3>\n\n\n\n<p>A Bar chart is a type of graph that represents categorical data using rectangular bars.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong> Let us take a simple example of subjects and marks.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>x=np.array([&#8220;Language&#8221;, &#8220;English&#8221;, &#8220;Maths&#8221;, &#8220;Science&#8221;, &#8220;SocialScience&#8221;])<br>y=np.array([60,95,40,85,98])<br>plt.bar(x,y,width=0.5,color=&#8221;m&#8221;) #width-width of the bar<br>plt.title(&#8220;Subject-Marks&#8221;)<br>plt.xlabel(&#8216;Subjects&#8217;)<br>plt.ylabel(&#8216;Marks&#8217;)<br>plt.show()<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXfCvXXhkOCCRa6Iw29pS2X5YzKHUJOaxqPQKfNf0HKvVAeAYdXpqVSkzzigQxZiSo7EyX95ND6rJQadvTtxhWgNrxlVGCD0PSKjcHgzyDT8ds2IL1Uh2V6UsQlc__xcwlNVvliJMg?key=lf4UuhQ1qc-Xv5AnBzgArw\" alt=\"bar chart in matplotlib\" title=\"\"><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Example 2:<\/strong> The same graph can be represented horizontally, and we can add a grid (that can be used in any type of plot according to our preferences) to better understand these plots.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>x=np.array([&#8220;Language&#8221;,&#8221;English&#8221;,&#8221;Maths&#8221;,&#8221;Science&#8221;,&#8221;SocialScience&#8221;])<br>y=np.array([60,95,40,85,98])<br>plt.grid(color=&#8221;r&#8221;,ls=&#8221;&#8211;&#8220;,lw=0.5) #grid properties<br>plt.title(&#8220;Subject-Marks&#8221;)<br>plt.xlabel(&#8216;Subjects&#8217;)<br>plt.ylabel(&#8216;Marks&#8217;)<br>plt.barh(x,y,color=&#8221;y&#8221;) #barh-hoizontal view<br>plt.show()<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXd94vutwtFUovEFv-_tJMatPAxlw2PEsaAbouVv2CNlhkBbHdhZzaLUEARh1vEkaYmQo5U833PaHHT7k75dFRqQ461dje55uiRdrP_glS3EH34ToEgk1SmFTj5719uqSXk9L6mw7A?key=lf4UuhQ1qc-Xv5AnBzgArw\" alt=\"horizontal bar graph\" title=\"\"><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">4. Histogram<\/h3>\n\n\n\n<p>A histogram is a type of graphical representation that groups the data into ranges.<\/p>\n\n\n\n<p><strong>Example:<\/strong> For better understanding, let&#8217;s take the random variables using the numpy array, also here we use the hatch function to fill the plot space with patterns.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>x=np.random.normal(100,10,50)<br>plt.title(&#8220;Histogram&#8221;)<br>plt.xlabel(&#8216;x axis&#8217;)<br>plt.ylabel(&#8216;y axis&#8217;)<br>plt.hist(x,color=&#8221;w&#8221;,hatch=&#8221;*&#8221;) #hatch is used to fill the bar space<br>plt.show()<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXfm28Zu2dcT0KpNrMlehg72Oa4gtDkc3Vff_p81BgiMWeyt7l1BInR_NvGow5eZAQP_SJUk0PSpNy5aA4hDdf3Hh7eU1C412gutWEl0d-UdDxYkPYItEE5B2e3CzicoRMvxGF48Yw?key=lf4UuhQ1qc-Xv5AnBzgArw\" alt=\"histogram\" title=\"\"><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">5. Pie Chart<\/h3>\n\n\n\n<p>A Pie chart is a circular graphical representation that divides the data into slices.<\/p>\n\n\n\n<p><strong>Example:<\/strong> For better understanding, let\u2019s take the same example of the subject and marks which we\u2019ve used in the example of the bar chart.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>sub=np.array([&#8220;Language&#8221;,&#8221;English&#8221;,&#8221;Maths&#8221;,&#8221;Science&#8221;,&#8221;SocialScience&#8221;])<br>marks=np.array([60,95,40,85,98])<br>plt.pie(marks,labels=sub)<br>plt.show()<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXfYvLejhFV3sfgqqhCfj43gG3gQ-cr44uXB-tlzhYuR6EKLmolYVsU9G-2TIN9EdC-99DVY8wVIufx0LQu3BN4JsxDK5fSlXNQbrrcZISaHE0LbhJ5MOQ5Im4IfoH_9tUHctsV72Q?key=lf4UuhQ1qc-Xv5AnBzgArw\" alt=\"Simple pie chart\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Matplotlib is a foundational library in the <a href=\"https:\/\/www.guvi.in\/blog\/a-beginners-guide-to-python-data-visualization\/\" target=\"_blank\" rel=\"noreferrer noopener\">Python data visualization <\/a>ecosystem, offering flexibility and control for creating a wide range of static, animated, and interactive plots. We also covered the most commonly used plot types, such as line plots, bar charts, scatter plots, histograms, and pie charts. These are the basic plots we need to know before moving advanced into it. Happy learning!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Matplotlib is one of the Python libraries that is open source and is used to visualize data in the form of charts, graphs, plots, etc. The graphical output is easy to observe and understand by any user. Here in this blog, we\u2019re learning the basics of this data visualization library, which covers fundamental features such [&hellip;]<\/p>\n","protected":false},"author":57,"featured_media":83736,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[294,717],"tags":[],"views":"3210","authorinfo":{"name":"Roopa Dharshini","url":"https:\/\/www.guvi.in\/blog\/author\/roopa-dharshini\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/07\/Getting-Started-with-Matplotlib-300x116.png","jetpack_featured_media_url":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/07\/Getting-Started-with-Matplotlib.png","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/82540"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/57"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=82540"}],"version-history":[{"count":5,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/82540\/revisions"}],"predecessor-version":[{"id":86882,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/82540\/revisions\/86882"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/83736"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=82540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=82540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=82540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}