

{"id":117905,"date":"2023-12-09T18:00:04","date_gmt":"2023-12-09T12:30:04","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=117905"},"modified":"2023-12-09T18:17:28","modified_gmt":"2023-12-09T12:47:28","slug":"pyplot-in-matplotlib","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/pyplot-in-matplotlib\/","title":{"rendered":"Pyplot in Matplotlib"},"content":{"rendered":"<p>The ability to effectively share and comprehend information relies heavily on data visualization&#8217;s ability to help us draw insights from complicated datasets. The Matplotlib Python package is widely used because it has robust capabilities for producing eye-catching visualisations.<\/p>\n<p>The pyplot module is an important part of this package since it streamlines the production of plots and charts. This in-depth tutorial will teach you how to use Matplotlib&#8217;s pyplot for data visualisation and will equip you with the skills you need to make impressive visual representations of your data.<\/p>\n<h2>Learning the Foundations of Pyplot in Matplotlib<\/h2>\n<h3>Installation<\/h3>\n<p>Let&#8217;s check whether Matplotlib is available in our Colab environment before we get started. There is no need to explicitly install Matplotlib because it is already included in Colab. However, Matplotlib may be installed with the Python package manager (pip) if you&#8217;re using a different Python environment.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">!pip install matplotlib\r\n<\/pre>\n<p>Let&#8217;s go into using Pyplot now that Matplotlib is set up.<\/p>\n<h3>Matplotlib and Pyplot import<\/h3>\n<p>Importing the required libraries into your Python environment is the first step in utilising Matplotlib&#8217;s pyplot. The following lines of code will get you there:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import matplotlib.pyplot as plott\r\nimport numpy as numpyy\r\n<\/pre>\n<p>Here, the Matplotlib library is brought in and aliased to Plott for simplicity. NumPy (abbreviated numpyy) is also imported since it is often used to produce data for graphing.<\/p>\n<h3>Outlining a Simple Storyline<\/h3>\n<h4>1. Figure and axis initialization<\/h4>\n<p>Axes relate to the specific plotting region inside a figure, whereas figures represent the complete window or page in Matplotlib. Use the following code to generate a simple graph:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">figuree, axess = plott.subplots()\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Figure-and-axis-initialization.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125316 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Figure-and-axis-initialization.webp\" alt=\"Figure and axis initialization\" width=\"558\" height=\"418\" \/><\/a><\/p>\n<h4>2. Use Pyplot&#8217;s plot() function to visually represent data<\/h4>\n<p>The plot() method allows you to see your data once you have prepared the figure and axes. Assume that the a and b positions of your data points are stored in two separate arrays, a and b. The following lines provide a visual representation of this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">figuree, axess = plott.subplots()\r\n\r\n\r\na = numpyy.linspace(3, 30, 300)\r\nb = numpyy.cos(a)\r\n\r\n\r\naxess.plot(a, b)\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Use-Pyplots-plot-function-to-visually-represent-data.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125317 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Use-Pyplots-plot-function-to-visually-represent-data.webp\" alt=\" Use Pyplot's plot() function to visually represent data\" width=\"568\" height=\"413\" \/><\/a><\/p>\n<p>This piece of code creates an array of 300 values from 3 to 30 and uses the sine function to get their associated y-coordinates. These data points are then used by the plot() method to generate a graph.<\/p>\n<h4>3. Adding Title<\/h4>\n<p>Visualisations are useless without descriptive titles. Titles may be easily added to charts in Pyplot. Let&#8217;s have a look at a basic plot of a sine wave with a name:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import matplotlib.pyplot as plott\r\nimport numpy as numpyy\r\n\r\n\r\n# Generate data\r\nx_axis = numpyy.linspace(10, 5 * numpyy.pi, 50)\r\ny_axis = numpyy.sin(x_axis)\r\n\r\n\r\n# Create the plot\r\nplott.plot(x_axis, y_axis)\r\n\r\n\r\n# Add title\r\nplott.title(\"Sine Wave Example\")\r\n\r\n\r\n# Display the plot\r\nplott.show()\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Adding-Title.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125318 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Adding-Title.webp\" alt=\" Adding Title \" width=\"568\" height=\"435\" \/><\/a><\/p>\n<p>Using plt.title(), we give our plot the title &#8220;Sine Wave Example&#8221; in this demonstration. You may change the size, colour, and alignment of the titles to suit your needs.<\/p>\n<h3>Graphing Varieties of Data<\/h3>\n<p>The ability to see and communicate trends and patterns in your data is greatly enhanced by data visualisation. The pyplot module of Matplotlib offers a variety of plotting capabilities. This tutorial will walk you through the steps of using Matplotlib&#8217;s pyplot to successfully visualise your data in a variety of formats.<\/p>\n<h4>Line Graphs<\/h4>\n<p><strong>1. Drawing a straight line graph<\/strong><\/p>\n<p>The plot() method may be used to generate a simple line plot. Let&#8217;s say we&#8217;re interested in graphing the outcomes of the function b = a:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">a = numpyy.linspace(5, 50, 550)\r\nb = a\r\nplott.plot(a, b)\r\nplott.show()\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Drawing-a-straight-line-graph.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125320 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Drawing-a-straight-line-graph.webp\" alt=\" Drawing a straight line graph\" width=\"543\" height=\"413\" \/><\/a><\/p>\n<p>This code creates an array of 100 integers from 0 to 10 and finds the squares of those numbers. The plot() method then draws a line between each of these points to create a line plot.<\/p>\n<p><strong>2. Altering the appearance of lines and their markers<\/strong><\/p>\n<p>Changing the line&#8217;s look is as simple as adjusting a few sliders. For instance, the following code may be modified to switch the line colour to red and the style to dashed:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">plott.plot(a, b, linestyle='-.', color='green')\r\nplott.show()\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Altering-the-appearance-of-lines-and-their-markers.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125321 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Altering-the-appearance-of-lines-and-their-markers.webp\" alt=\"Altering the appearance of lines and their markers\" width=\"543\" height=\"413\" \/><\/a><\/p>\n<p>Adding marks to emphasise the data points is also possible through the marker option.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">plott.plot(a, b, marker='x')\r\nplott.show()\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Altering-the-aAdding-marks-to-emphasise.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125322 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Altering-the-aAdding-marks-to-emphasise.webp\" alt=\"Adding marks to emphasise\" width=\"543\" height=\"413\" \/><\/a><\/p>\n<p><strong>3. Increasing the number of lines in a graph<\/strong><\/p>\n<p>It is possible to include many lines in a single plot when using Pyplot. Take the following two equations, line1 = a2 and line2 = a3, and graph them side by side:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">line1 = a ** 3\r\nline2 = a ** 2\r\n\r\n\r\nplott.plot(a, line1, label='a^3')\r\nplott.plot(a, line2, label='a^2')\r\nplott.legend()\r\nplott.show()\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Increasing-the-number-of-lines-in-a-graph.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125323 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Increasing-the-number-of-lines-in-a-graph.webp\" alt=\"Increasing the number of lines in a graph\" width=\"560\" height=\"413\" \/><\/a><\/p>\n<p>Here, we use the plott.legend() function to plot a legend above each line, and we provide the legend using the label argument.<\/p>\n<h4>Scatter Diagrams<\/h4>\n<p><strong>1. Discrete data points as symbols in a graph<\/strong><\/p>\n<p>The correlation between two variables may be shown clearly in a scatter plot. Use the scatter() procedure to make a scatter diagram. Let&#8217;s take a look at a scenario where we have two sets of data coordinates, a and b, represented by separate arrays:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">a = [7, 14, 28, 56, 112]\r\nb = [14, 28, 56, 112, 224]\r\n\r\n\r\nplott.scatter(a, b)\r\nplott.show()\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Discrete-data-points-as-symbols-in-a-graph.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125325 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Discrete-data-points-as-symbols-in-a-graph.webp\" alt=\"Discrete data points as symbols in a graph\" width=\"552\" height=\"413\" \/><\/a><\/p>\n<p>The code generates a graph with discrete markers representing the data points.<\/p>\n<p><strong>2. Size, hue, and opacity controls for markers<\/strong><\/p>\n<p>Changes to the s, c, and alpha parameters allow you to alter the size, colour, and opacity of the marks. Here&#8217;s an illustration:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">plott.scatter(a, b, s=130, c='green', alpha=0.5)\r\nplott.show()\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Size-hue-and-opacity-controls.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125326 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Size-hue-and-opacity-controls.webp\" alt=\"Size hue and opacity controls \" width=\"552\" height=\"413\" \/><\/a><\/p>\n<p>The size of the marker is changed to 130, the colour to green, and the transparency to 0.5 in this code piece.<\/p>\n<p><strong>3. Including supplementary visuals (such as a grid or a legend)<\/strong><\/p>\n<p>Add a grid and a legend using plott.grid() and plott.legend(), respectively, to improve the visual representation. Here&#8217;s an illustration:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">plott.scatter(a, b, label='Data Flair')\r\nplott.legend()\r\nplott.grid(True)\r\nplott.show()\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Including-supplementary-visuals.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125327 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Including-supplementary-visuals.webp\" alt=\"Including supplementary visuals\" width=\"552\" height=\"413\" \/><\/a><\/p>\n<p>This piece of code renders a grid over the plot with a legend titled &#8220;Data Flair.&#8221;<\/p>\n<h4>Bar Graphs<\/h4>\n<p><strong>1. Making bar graphs, both horizontal and vertical<\/strong><\/p>\n<p>When comparing discrete groups of information, bar graphs shine. Plott.bar() generates vertical bar plots, whereas plott.barh() generates horizontal bar plots. Consider this case:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">cats = ['Apple', 'Banana', 'Guava', 'Mango']\r\nvals = [20, 25, 14, 18]\r\n\r\n\r\nplott.bar(cats, vals)\r\nplott.show()\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/graphs-both-horizontal-and-vertical.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125328 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/graphs-both-horizontal-and-vertical.webp\" alt=\"graphs both orizontal and vertical\" width=\"543\" height=\"413\" \/><\/a><\/p>\n<p>With this code, you can generate a vertical bar plot, where the x-axis represents categories and the y-axis represents values.<\/p>\n<p><strong>2. Adjustable label placement, bar width, and coloration<\/strong><\/p>\n<p>The bar chart&#8217;s appearance may be altered in many ways. The colour option, for instance, lets you customise the hues of the bars:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">plott.bar(cats, vals, color='maroon')\r\nplott.show()\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Adjustable-label-placement.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125329 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Adjustable-label-placement.webp\" alt=\"Adjustable label placement\" width=\"543\" height=\"413\" \/><\/a><\/p>\n<p><strong>You may also adjust the bar width using the width parameter:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">plott.bar(cats, vals, width=0.3)\r\nplott.show()\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/bar-width-and-coloration.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125330 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/bar-width-and-coloration.webp\" alt=\"bar width and coloration\" width=\"543\" height=\"413\" \/><\/a><\/p>\n<p>The plott.xlabel() and plott.ylabel() routines may be used to add labels to the bars.<\/p>\n<p><strong>3. Bar graphs with stacking and grouping<\/strong><\/p>\n<p>Matplotlib lets you create group and stacked bar graphs to display many different variables. Multiple calls to plott.bar() with the same bottom argument will produce a stacked bar plot. Each bar&#8217;s x-coordinate may be changed independently in grouped bar graphs. Here&#8217;s an illustration:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">cats = ['Apple', 'Banana', 'Guava', 'Mango']\r\nvals1 = [20, 25, 14, 18]\r\nvals2 = [15, 28, 13, 11]\r\n\r\n\r\nplott.bar(cats, vals1, bottom=vals2, label='1st Group')\r\nplott.bar(cats, vals2, label='2nd Group')\r\nplott.legend()\r\nplott.show()\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/stacking-and-grouping.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125331 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/stacking-and-grouping.webp\" alt=\"stacking and grouping\" width=\"543\" height=\"413\" \/><\/a><\/p>\n<p>This code snippet generates a stacked bar plot consisting of two separate bar groups.<\/p>\n<h4>Histograms<\/h4>\n<p><strong>1. Displaying data in a graphical format<\/strong><\/p>\n<p>Histograms display the distribution of data points graphically. Using the plott.hist() method, a histogram may be generated. Let&#8217;s take a look at a scenario where we have a set of values in an array:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">dataflair_data = numpyy.random.randn(2500)\r\n\r\n\r\nplott.hist(dataflair_data)\r\nplott.show()\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Displaying-data-in-a-graphical-format.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125332 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Displaying-data-in-a-graphical-format.webp\" alt=\" Displaying data in a graphical format\" width=\"552\" height=\"413\" \/><\/a><\/p>\n<p>This piece of code creates a histogram with 2500 values chosen at random.<\/p>\n<p><strong>2. Adjusting the bin size and bin width<\/strong><\/p>\n<p>The default number of histogram bins is set by Matplotlib. However, the bins option allows you to choose the desired number of bins. Case in point:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">plott.hist(dataflair_data, bins=30)\r\nplott.show()\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Adjusting-the-bin-size-and-bin-width.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125333 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Adjusting-the-bin-size-and-bin-width.webp\" alt=\"Adjusting the bin size and bin width\" width=\"552\" height=\"413\" \/><\/a><\/p>\n<p>A histogram with 30 bins is generated by this piece of code.<\/p>\n<p><strong>Bin width may be adjusted using the range parameter:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">plott.hist(dataflair_data, bins=30, range=(-5, 5))\r\nplott.show()\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Adjusting-the-bin-size-and-bin-.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125334 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Adjusting-the-bin-size-and-bin-.webp\" alt=\"Adjusting the bin size and bin \" width=\"552\" height=\"413\" \/><\/a><\/p>\n<p><strong>3. Histograms with added density curves<\/strong><\/p>\n<p>Using the density parameter, a density curve may be superimposed on top of the histogram. Here&#8217;s an illustration:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">plott.hist(dataflair_data, bins=30, density=True)\r\nplott.show()\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Histograms-with-added-.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125335 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Histograms-with-added-.webp\" alt=\"Histograms with added\" width=\"556\" height=\"413\" \/><\/a><\/p>\n<p>This piece of code renders the density curve beside the histogram.<\/p>\n<h4>Pie Charts<\/h4>\n<p><strong>1. Making a pie chart using categorised information<\/strong><\/p>\n<p>Categorical data can be easily visualised using pie charts, and percentages can be shown with ease. The plott.pie() method may be used to make a pie chart. Consider this case:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">cats = ['Bluecurrent', 'Mango', 'Guava', 'Apple']\r\nvalues = [22, 33, 24, 10]\r\n\r\n\r\nplott.pie(values, labels=cats)\r\nplott.show()\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/pie-chart-using-categorised-information.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125336 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/pie-chart-using-categorised-information.webp\" alt=\"pie chart using categorised information\" width=\"411\" height=\"389\" \/><\/a><\/p>\n<p>This code creates a pie chart where each section is labelled and its size corresponds to the percentage of data in that section.<\/p>\n<p><strong>2. Altering the slice&#8217;s title, colour, and explosion size<\/strong><\/p>\n<p>Several components of a pie chart may be altered to improve its visual appeal. The colours argument, for instance, allows us to alter the slice colours:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">df_colors = ['blue', 'yellow', 'green', 'red']\r\n\r\n\r\nplott.pie(values, labels=cats, colors=df_colors)\r\nplott.show()\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/the-slices-title-colour.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125337 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/the-slices-title-colour.webp\" alt=\"the slice's title colour\" width=\"411\" height=\"389\" \/><\/a><\/p>\n<p>In addition, the explode option allows us to highlight a particular slice by exploding it.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">df_explode = [0, 0, 0, 0.1]\r\ndf_colors = ['blue', 'yellow', 'green', 'red']\r\nplott.pie(values, labels=cats, colors=df_colors, explode=df_explode)\r\nplott.show()\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/slices-title-colour-and-explosion-size.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125338 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/slices-title-colour-and-explosion-size.webp\" alt=\"slice's title colour and explosion size\" width=\"421\" height=\"389\" \/><\/a><\/p>\n<p><strong>The autopct argument allows us to automatically label each slice with the corresponding percentage:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">plott.pie(values, labels=cats, autopct='%1.2f%%')\r\nplott.show()\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/the-slices-title-colour-and-explosion-size.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125339 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/the-slices-title-colour-and-explosion-size.webp\" alt=\"the slice's title colour and explosion size\" width=\"411\" height=\"389\" \/><\/a><\/p>\n<p><strong>3. Bringing out certain slices<\/strong><\/p>\n<p><strong>A pie chart&#8217;s individual segments may be highlighted by removing them from the whole. Here&#8217;s an illustration:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">df_explode = [0, 0, 0, 0.1]\r\ndf_colors = ['blue', 'yellow', 'green', 'red']\r\nplott.pie(values, labels=cats, explode=df_explode, colors=df_colors, shadow=True, startangle=90)\r\nplott.show()\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Bringing-out-certain-slices.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125341 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Bringing-out-certain-slices.webp\" alt=\"Bringing out certain slices\" width=\"422\" height=\"389\" \/><\/a><\/p>\n<p>With this line of code, you can give your pie chart a shadow effect and have the first slice begin at a right angle.<\/p>\n<h4>Limits and Checkboxes<\/h4>\n<p>If you want to zero in on certain data ranges and make your plots more legible, you need to take command of the axis boundaries and tick labels. The x and y axes in Pyplot can have their limits and tick labels modified to suit your needs. Let&#8217;s try this out on a scatter plot and see what happens.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Generate random data\r\nx_axis = numpyy.random.rand(100)\r\ny_axis = numpyy.random.rand(100)\r\n\r\n\r\n# Create the plot\r\nplott.scatter(x_axis, y_axis)\r\n\r\n\r\n# Set axis limits and tick labels\r\nplott.xlim(0, 1)\r\nplott.ylim(0, 1)\r\nplott.xticks([0, 0.4, 1])\r\nplott.yticks([0, 0.4, 1])\r\n\r\n\r\n# Add labels\r\nplott.xlabel(\"X-axis\")\r\nplott.ylabel(\"Y-axis\")\r\n\r\n\r\n# Add title\r\nplott.title(\"Scatter Plot with Custom Axis Limits and Tick Labels\")\r\n\r\n\r\n# Display the plot\r\nplott.show()\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Limits-and-Checkboxes.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125342 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Limits-and-Checkboxes.webp\" alt=\" Limits and Checkboxes \" width=\"578\" height=\"455\" \/><\/a><\/p>\n<p>Here, we restrict the range of both the x and y axes by employing the plt.xlim() and plt.ylim() functions. In order to specify where the tick marks will appear along each axis, use the plt.xticks() and plt.yticks() routines.<\/p>\n<h4>Figure Class<\/h4>\n<p>Matplotlib&#8217;s Figure class offers a more sophisticated method of handling numerous plots in a single visualisation. You may use it to develop and organise subplots. Let&#8217;s make a diagram with two plots running parallel to one another:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Generate data\r\nx_axis = numpyy.linspace(10, 5 * numpyy.pi, 50)\r\ny_sine = numpyy.sin(x_axis)\r\ny_cosine = numpyy.cos(x_axis)\r\n\r\n\r\ny_axis = numpyy.sin(x_axis)\r\n\r\n\r\n# Create a figure with two subplots\r\nfiguree, axes = plott.subplots(1, 2, figsize=(12, 6))\r\n\r\n\r\n# Plot the sine wave\r\naxes[0].plot(x_axis, y_sine)\r\naxes[0].set_title(\"Sine Wave Figure Data Flair\")\r\n\r\n\r\n# Plot the cosine wave\r\naxes[1].plot(x_axis, y_cosine)\r\naxes[1].set_title(\"Cosine Wave\")\r\n\r\n\r\n# Display the plots\r\nplott.show()\r\n<\/pre>\n<p>Here, we use plt.subplots() to generate a chart with two adjacent subplots. The figure&#8217;s dimensions can be set with the figsize option.<\/p>\n<h4>The Plot Is Saved<\/h4>\n<p>It&#8217;s common practice to export finished visualisations as images for use in reports and presentations. Plots made using Pyplot can be exported to many different file types, including PNG, JPEG, PDF, and many more. Let&#8217;s create a PNG picture of our sine wave plot:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Generate data\r\nx_axis = numpyy.linspace(10, 5 * numpyy.pi, 50)\r\ny_axis = numpyy.sin(x_axis)\r\n\r\n\r\n# Create the plot\r\nplott.plot(x_axis, y_axis)\r\n\r\n\r\n# Add title\r\nplott.title(\"Sine Wave Figure\")\r\n\r\n\r\n# Save the plot as a PNG image\r\nplott.savefig(\"sine_wave_plot.png\")\r\n\r\n\r\n# Display the plot\r\nplott.show()\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/The-Plot-Is-Saved.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-125343 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/The-Plot-Is-Saved.webp\" alt=\"The Plot Is Saved\" width=\"568\" height=\"435\" \/><\/a><\/p>\n<p>Here, the plot is saved as a PNG file in the current working directory with the name &#8220;sine_wave_plot.png&#8221; using the plt.savefig() function.<\/p>\n<h3>Conclusion<\/h3>\n<p>In this in-depth tutorial, we looked at the powerful and flexible data visualisation tool Pyplot in Matplotlib has to offer. Using Matplotlib&#8217;s pyplot module, we learned how to generate a variety of graphical representations, including line plots, scatter plots, bar plots, histograms, and pie charts. Plots may be made more aesthetically pleasing and instructive by the use of different colours, markers, labels, and styles.<\/p>\n<p>The capabilities of Matplotlib are much more than those covered in this introduction. I hope you&#8217;ll take some time to investigate Matplotlib and all the cool things it can do.<\/p>\n<p>Matplotlib is an indispensable tool for data visualisation in Python, enabling you to express your creativity and effectively convey insights from your data thanks to its vast documentation and active user community. Let the planning begin!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The ability to effectively share and comprehend information relies heavily on data visualization&#8217;s ability to help us draw insights from complicated datasets. The Matplotlib Python package is widely used because it has robust capabilities&#46;&#46;&#46;<\/p>\n","protected":false},"author":86671,"featured_media":117907,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27777],"tags":[8601,29063,29088,29062,29065,29064],"class_list":["post-117905","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-matplotlib-tutorials","tag-matplotlib","tag-matplotlib-pyplot","tag-matplotlib-tutorials","tag-pyplot-in-matplotlib","tag-pyplot-tutorial","tag-what-is-pyplot-in-matplotlib"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Pyplot in Matplotlib - DataFlair<\/title>\n<meta name=\"description\" content=\"By using Matplotlib&#039;s pyplot module, we learned how to generate a variety of graphical representations like line plots, scatter plots, bar plots etc.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/pyplot-in-matplotlib\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Pyplot in Matplotlib - DataFlair\" \/>\n<meta property=\"og:description\" content=\"By using Matplotlib&#039;s pyplot module, we learned how to generate a variety of graphical representations like line plots, scatter plots, bar plots etc.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/pyplot-in-matplotlib\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-12-09T12:30:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-09T12:47:28+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/pyplot-in-matplotlib.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"13 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Pyplot in Matplotlib - DataFlair","description":"By using Matplotlib's pyplot module, we learned how to generate a variety of graphical representations like line plots, scatter plots, bar plots etc.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/pyplot-in-matplotlib\/","og_locale":"en_US","og_type":"article","og_title":"Pyplot in Matplotlib - DataFlair","og_description":"By using Matplotlib's pyplot module, we learned how to generate a variety of graphical representations like line plots, scatter plots, bar plots etc.","og_url":"https:\/\/data-flair.training\/blogs\/pyplot-in-matplotlib\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-12-09T12:30:04+00:00","article_modified_time":"2023-12-09T12:47:28+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/pyplot-in-matplotlib.webp","type":"image\/webp"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"13 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/pyplot-in-matplotlib\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/pyplot-in-matplotlib\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/0e594f928e31fc96628ac40f6ae74f49"},"headline":"Pyplot in Matplotlib","datePublished":"2023-12-09T12:30:04+00:00","dateModified":"2023-12-09T12:47:28+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/pyplot-in-matplotlib\/"},"wordCount":1690,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/pyplot-in-matplotlib\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/pyplot-in-matplotlib.webp","keywords":["Matplotlib","matplotlib pyplot","matplotlib tutorials","pyplot in matplotlib","pyplot tutorial","what is pyplot in matplotlib"],"articleSection":["Matplotlib Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/pyplot-in-matplotlib\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/pyplot-in-matplotlib\/","url":"https:\/\/data-flair.training\/blogs\/pyplot-in-matplotlib\/","name":"Pyplot in Matplotlib - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/pyplot-in-matplotlib\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/pyplot-in-matplotlib\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/pyplot-in-matplotlib.webp","datePublished":"2023-12-09T12:30:04+00:00","dateModified":"2023-12-09T12:47:28+00:00","description":"By using Matplotlib's pyplot module, we learned how to generate a variety of graphical representations like line plots, scatter plots, bar plots etc.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/pyplot-in-matplotlib\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/pyplot-in-matplotlib\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/pyplot-in-matplotlib\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/pyplot-in-matplotlib.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/pyplot-in-matplotlib.webp","width":1200,"height":628,"caption":"pyplot in matplotlib"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/pyplot-in-matplotlib\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Matplotlib Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/matplotlib-tutorials\/"},{"@type":"ListItem","position":3,"name":"Pyplot in Matplotlib"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/0e594f928e31fc96628ac40f6ae74f49","name":"TechVidvan Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","caption":"TechVidvan Team"},"description":"TechVidvan Team provides high-quality content &amp; courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.","url":"https:\/\/data-flair.training\/blogs\/author\/test001\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/117905","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/86671"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=117905"}],"version-history":[{"count":7,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/117905\/revisions"}],"predecessor-version":[{"id":126550,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/117905\/revisions\/126550"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/117907"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=117905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=117905"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=117905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}