{"id":309,"date":"2023-07-23T17:16:39","date_gmt":"2023-07-23T17:16:39","guid":{"rendered":"http:\/\/python.garden\/?p=309"},"modified":"2023-08-07T16:17:23","modified_gmt":"2023-08-07T16:17:23","slug":"getting-started-with-matplotlib","status":"publish","type":"post","link":"https:\/\/python.garden\/index.php\/2023\/07\/23\/getting-started-with-matplotlib\/","title":{"rendered":"Getting Started with Matplotlib"},"content":{"rendered":"\n<p><strong>Introduction<\/strong><\/p>\n\n\n\n<p>Matplotlib is a powerful plotting library for Python that enables the creation of a wide range of static, animated, and interactive plots. It offers a high level of control over the look and feel of the visual output, which is ideal for anyone needing to prepare clear and professional-quality visualizations: analysts, scientists, statisticians, engineers, and more.<\/p>\n\n\n\n<p>Matplotlib was created by John Hunter in 2003 as an attempt to replicate MatLab&#8217;s (another programming language) plotting capabilities in Python. Today, it stands on its own and has significantly expanded its functionality beyond its initial inspiration. Matplotlib has become a vital component of the Python data stack, along with libraries like NumPy, Pandas, and SciPy.<\/p>\n\n\n\n<p>One key characteristic of Matplotlib is its compatibility with many operating systems and graphics backends, providing a lot of flexibility for where and how you can use it. Matplotlib is also well integrated with Jupyter Notebook and JupyterLab, making it convenient for exploratory data analysis and reporting.<\/p>\n\n\n\n<p>Matplotlib consists of several plots like line, bar, scatter, histogram etc. You can customize the style, font, text and many more aspects of the plot using this library. It&#8217;s multi-platform, multi-purpose, and built to work well with other Python packages like NumPy and Pandas.<\/p>\n\n\n\n<p>Here is a simple example to illustrate how to plot a line graph using Matplotlib:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import matplotlib.pyplot as plt\nimport numpy as np\n\n# Create a range of x values from 0 to 2*pi\nx = np.linspace(0, 2 * np.pi, 100)\n\n# Calculate the y values by applying the sine function to x\ny = np.sin(x)\n\n# Create a plot of y vs x\nplt.plot(x, y)\n\n# Display the plot\nplt.show()\n<\/pre><\/div>\n\n\n\n<p>In this example, we first import the required modules. We then create a range of <code>x<\/code> values and calculate the corresponding <code>y<\/code> values by applying the sine function to <code>x<\/code>. We then plot <code>y<\/code> vs <code>x<\/code> using <code>plt.plot()<\/code>, and finally, we display the plot using <code>plt.show()<\/code>.<\/p>\n\n\n\n<p>The resulting line graph ( using idle ) should look like this:<br><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"538\" src=\"https:\/\/i0.wp.com\/python.garden\/wp-content\/uploads\/2023\/07\/Figure-1_001.png?resize=640%2C538\" alt=\"\" class=\"wp-image-310\" srcset=\"https:\/\/i0.wp.com\/python.garden\/wp-content\/uploads\/2023\/07\/Figure-1_001.png?w=640&amp;ssl=1 640w, https:\/\/i0.wp.com\/python.garden\/wp-content\/uploads\/2023\/07\/Figure-1_001.png?resize=300%2C252&amp;ssl=1 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\n<\/div>\n\n\n<p>However, this is just scratching the surface of what you can do with Matplotlib. You can create complex multi-plot grids, three-dimensional plots, and interactive plots. It is also possible to embed Matplotlib plots in GUI applications.<\/p>\n\n\n\n<p>Matplotlib is an open-source project and you can use and modify its source code freely. To help with that, the Matplotlib community has written extensive documentation and examples, and there are many third-party tutorials available.<\/p>\n\n\n\n<p>In the next sections, we&#8217;ll delve deeper into the different components of Matplotlib, learn about the different types of plots and figures, and explore ways to customize these visualizations. As we go along, you&#8217;ll find that Matplotlib is a versatile and powerful tool for visualizing data in Python.<\/p>\n\n\n\n<p>Absolutely! Let&#8217;s dive deeper into the core components of Matplotlib: plots and figures.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Matplotlib Plots and Figures<\/strong><\/p>\n\n\n\n<p>Matplotlib is fundamentally structured around the concept of &#8220;Figures&#8221; and &#8220;Axes&#8221;. Understanding these components and their relationship is crucial to creating and customizing visualizations in Matplotlib.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Figure:<\/strong> A <code>Figure<\/code> in Matplotlib is like a blank canvas. It is the top-level container for all plot elements. You can think of it as the window or page that holds the plots. A <code>Figure<\/code> can contain one or more <code>Axes<\/code> objects (the plots themselves), along with titles, labels, or legends that are associated with the figure but not any individual plot. You can create a new figure using <code>plt.figure()<\/code>. This function also takes optional parameters such as <code>figsize<\/code>, which specifies the width and height of the figure in inches.<\/li>\n\n\n\n<li><strong>Axes:<\/strong> An <code>Axes<\/code> is what you probably think of as &#8216;a plot&#8217;. Each <code>Axes<\/code> object is a separate plot with its own x-axis, y-axis, title, etc. Every plot you make is drawn on an <code>Axes<\/code>. Each <code>Axes<\/code> object resides within a <code>Figure<\/code> object, and a figure can contain many <code>Axes<\/code> objects arranged in a grid pattern. An <code>Axes<\/code> object can be added to a figure using the <code>add_subplot<\/code> method, like so: <code>fig.add_subplot()<\/code>. Alternatively, the <code>plt.subplots()<\/code> function can create a new figure and set of subplots (axes) at once.<\/li>\n<\/ul>\n\n\n\n<p>Here&#8217;s an example of creating a figure with two subplots:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;disableCopy&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import matplotlib.pyplot as plt\nimport numpy as np\n\n# Create some data\nx = np.linspace(0, 2 * np.pi, 100)\ny1 = np.sin(x)\ny2 = np.cos(x)\n\n# Create a new figure\nfig = plt.figure()\n\n# Add the first subplot (sin wave)\nax1 = fig.add_subplot(2, 1, 1)  # 2 rows, 1 column, first plot\nax1.plot(x, y1)\nax1.set_title('Sine wave')\n\n# Add the second subplot (cos wave)\nax2 = fig.add_subplot(2, 1, 2)  # 2 rows, 1 column, second plot\nax2.plot(x, y2)\nax2.set_title('Cosine wave')\n\n# Display the figure with its two subplots\nplt.tight_layout()  # Adjusts subplot params so that subplots fit into the figure area\nplt.show()<\/pre><\/div>\n\n\n\n<p>In this code, we first create some data (<code>x<\/code>, <code>y1<\/code>, and <code>y2<\/code>). We then create a new figure and add two subplots to it, arranging them in a 2-row, 1-column grid. The first subplot is a plot of a sine wave, and the second subplot is a plot of a cosine wave.<\/p>\n\n\n\n<p>The two line plot show should two distinct subplots as below:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"538\" src=\"https:\/\/i0.wp.com\/python.garden\/wp-content\/uploads\/2023\/07\/Figure-1_002.png?resize=640%2C538\" alt=\"\" class=\"wp-image-311\" srcset=\"https:\/\/i0.wp.com\/python.garden\/wp-content\/uploads\/2023\/07\/Figure-1_002.png?w=640&amp;ssl=1 640w, https:\/\/i0.wp.com\/python.garden\/wp-content\/uploads\/2023\/07\/Figure-1_002.png?resize=300%2C252&amp;ssl=1 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\n<\/div>\n\n\n<p>Once you have your <code>Axes<\/code> objects, there are a multitude of functions available for data plotting. Some commonly used ones include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>plot()<\/code>: Line plot.<\/li>\n\n\n\n<li><code>scatter()<\/code>: Scatter plot.<\/li>\n\n\n\n<li><code>bar()<\/code>: Vertical bar plot.<\/li>\n\n\n\n<li><code>barh()<\/code>: Horizontal bar plot.<\/li>\n\n\n\n<li><code>hist()<\/code>: Histogram.<\/li>\n\n\n\n<li><code>pie()<\/code>: Pie plot.<\/li>\n<\/ul>\n\n\n\n<p>Each of these functions takes various arguments to customize the appearance of the plot. Further customization can be achieved by using other functions to add or modify plot elements, such as <code>set_title()<\/code>, <code>set_xlabel()<\/code>, <code>set_ylabel()<\/code>, <code>legend()<\/code>, and many more.<\/p>\n\n\n\n<p>In the next sections, we will explore some of these plot types and functions in more detail, and discuss more ways to customize your Matplotlib plots.<\/p>\n\n\n\n<p>Absolutely, let&#8217;s delve into more specifics on types of plots, how to create them, and ways to customize them.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Exploring Matplotlib Plot Types and Customizations<\/strong><\/p>\n\n\n\n<p>Once you have a grasp of figures and axes, you can start creating a variety of plot types using Matplotlib. The flexibility to create different plots is one of the key features that makes Matplotlib an excellent tool for data visualization. Below are some of the most commonly used plot types:<\/p>\n\n\n\n<p><strong>1. Line Plots<\/strong><\/p>\n\n\n\n<p>Line plots can be created in Matplotlib using <code>plt.plot()<\/code>. They are commonly used for displaying trends over time or other continuous sequence of values.<\/p>\n\n\n\n<div class=\"wp-block-columns are-vertically-aligned-bottom is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-bottom is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import matplotlib.pyplot as plt\nimport numpy as np\n\nx = np.linspace(0, 10, 100)\ny = np.sin(x)\n\nplt.plot(x, y)\nplt.title('Sine Wave')\nplt.xlabel('x')\nplt.ylabel('sin(x)')\nplt.show()<\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-bottom is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"538\" src=\"https:\/\/i0.wp.com\/python.garden\/wp-content\/uploads\/2023\/07\/Figure-1_003.png?resize=640%2C538\" alt=\"\" class=\"wp-image-312\" srcset=\"https:\/\/i0.wp.com\/python.garden\/wp-content\/uploads\/2023\/07\/Figure-1_003.png?w=640&amp;ssl=1 640w, https:\/\/i0.wp.com\/python.garden\/wp-content\/uploads\/2023\/07\/Figure-1_003.png?resize=300%2C252&amp;ssl=1 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>2. Scatter Plots<\/strong><\/p>\n\n\n\n<p>Scatter plots are used to display the relationship between two different sets of data. It&#8217;s particularly useful when you want to show how much one variable is affected by another variable. This is done using the <code>plt.scatter()<\/code> function.<\/p>\n\n\n\n<div class=\"wp-block-columns are-vertically-aligned-bottom is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-bottom is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:50%\">\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">x = np.random.rand(50)\ny = np.random.rand(50)\n\nplt.scatter(x, y)\nplt.title('Scatter Plot')\nplt.xlabel('x')\nplt.ylabel('y')\nplt.show()<\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-bottom is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:50%\">\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"538\" src=\"https:\/\/i0.wp.com\/python.garden\/wp-content\/uploads\/2023\/07\/Figure-1_004.png?resize=640%2C538\" alt=\"\" class=\"wp-image-313\" srcset=\"https:\/\/i0.wp.com\/python.garden\/wp-content\/uploads\/2023\/07\/Figure-1_004.png?w=640&amp;ssl=1 640w, https:\/\/i0.wp.com\/python.garden\/wp-content\/uploads\/2023\/07\/Figure-1_004.png?resize=300%2C252&amp;ssl=1 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p><strong>3. Bar Plots<\/strong><\/p>\n\n\n\n<p>Bar plots are used to compare the quantity, frequency, or other measure for different categories or groups. <code>plt.bar()<\/code> is used for vertical bar plots, and <code>plt.barh()<\/code> is used for horizontal bar plots.<\/p>\n\n\n\n<div class=\"wp-block-columns are-vertically-aligned-bottom is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-bottom is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:50%\">\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">categories = ['A', 'B', 'C', 'D', 'E']\nvalues = [7, 12, 15, 8, 17]\n\nplt.bar(categories, values)\nplt.title('Bar Plot')\nplt.xlabel('Categories')\nplt.ylabel('Values')\nplt.show()<\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-bottom is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:50%\">\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"538\" src=\"https:\/\/i0.wp.com\/python.garden\/wp-content\/uploads\/2023\/07\/Figure-1_005.png?resize=640%2C538\" alt=\"\" class=\"wp-image-314\" srcset=\"https:\/\/i0.wp.com\/python.garden\/wp-content\/uploads\/2023\/07\/Figure-1_005.png?w=640&amp;ssl=1 640w, https:\/\/i0.wp.com\/python.garden\/wp-content\/uploads\/2023\/07\/Figure-1_005.png?resize=300%2C252&amp;ssl=1 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p><strong>4. Histograms<\/strong><\/p>\n\n\n\n<p>Histograms are used to visualize the distribution of numeric data. They are created using the <code>plt.hist()<\/code> function.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-bottom is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:50%\">\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">data = np.random.randn(1000)\n\nplt.hist(data, bins=30)\nplt.title('Histogram')\nplt.xlabel('Value')\nplt.ylabel('Frequency')\nplt.show()<\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:50%\">\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"538\" src=\"https:\/\/i0.wp.com\/python.garden\/wp-content\/uploads\/2023\/07\/Figure-1_006-1.png?resize=640%2C538\" alt=\"\" class=\"wp-image-317\" srcset=\"https:\/\/i0.wp.com\/python.garden\/wp-content\/uploads\/2023\/07\/Figure-1_006-1.png?w=640&amp;ssl=1 640w, https:\/\/i0.wp.com\/python.garden\/wp-content\/uploads\/2023\/07\/Figure-1_006-1.png?resize=300%2C252&amp;ssl=1 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p><strong>Customizations<\/strong><\/p>\n\n\n\n<p>Beyond creating different types of plots, Matplotlib provides various ways to customize your plots:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Colors, Markers, and Line Styles<\/strong>: You can change the color, marker style, and line style of your plots using various parameters in the plotting functions.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:50%\">\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">x = np.linspace(0, 10, 100)\ny = np.sin(x)\n\nplt.plot(x, y, color='purple', marker='o', linestyle='--')\nplt.show()<\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:50%\">\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"538\" src=\"https:\/\/i0.wp.com\/python.garden\/wp-content\/uploads\/2023\/07\/Figure-1_007.png?resize=640%2C538\" alt=\"\" class=\"wp-image-316\" srcset=\"https:\/\/i0.wp.com\/python.garden\/wp-content\/uploads\/2023\/07\/Figure-1_007.png?w=640&amp;ssl=1 640w, https:\/\/i0.wp.com\/python.garden\/wp-content\/uploads\/2023\/07\/Figure-1_007.png?resize=300%2C252&amp;ssl=1 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Legends<\/strong>: You can add a legend to your plot with the <code>plt.legend()<\/code> function. You should also provide a label for each line in your plot using the <code>label<\/code> parameter of the plotting functions.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-columns are-vertically-aligned-bottom is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-bottom is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:50%\">\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">x = np.linspace(0, 10, 100)\ny1 = np.sin(x)\ny2 = np.cos(x)\n\nplt.plot(x, y1, label='sin(x)')\nplt.plot(x, y2, label='cos(x)')\nplt.legend()\nplt.show()<\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-bottom is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:50%\">\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"538\" src=\"https:\/\/i0.wp.com\/python.garden\/wp-content\/uploads\/2023\/07\/Figure-1_007-1.png?resize=640%2C538\" alt=\"\" class=\"wp-image-318\" srcset=\"https:\/\/i0.wp.com\/python.garden\/wp-content\/uploads\/2023\/07\/Figure-1_007-1.png?w=640&amp;ssl=1 640w, https:\/\/i0.wp.com\/python.garden\/wp-content\/uploads\/2023\/07\/Figure-1_007-1.png?resize=300%2C252&amp;ssl=1 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Text and Annotations<\/strong>: You can add text at any location in the plot using the <code>plt.text()<\/code> function. To add a text box with a frame at any location in the plot, you can use the <code>plt.annotate()<\/code> function.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-columns are-vertically-aligned-bottom is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-bottom is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:50%\">\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">x = np.linspace(0, 10, 100)\ny = np.sin(x)\n\nplt.plot(x, y)\nplt.text(5, 0, 'Here is the sin(x) curve')\nplt.show()<\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-bottom is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:50%\">\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"538\" src=\"https:\/\/i0.wp.com\/python.garden\/wp-content\/uploads\/2023\/07\/Figure-1_008.png?resize=640%2C538\" alt=\"\" class=\"wp-image-319\" srcset=\"https:\/\/i0.wp.com\/python.garden\/wp-content\/uploads\/2023\/07\/Figure-1_008.png?w=640&amp;ssl=1 640w, https:\/\/i0.wp.com\/python.garden\/wp-content\/uploads\/2023\/07\/Figure-1_008.png?resize=300%2C252&amp;ssl=1 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p>These are just a few examples of the many plot types and customizations that Matplotlib offers. The library is quite extensive, and by exploring its functionalities, you can create just about any plot or visualization that you need for your data analysis.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Matplotlib is a powerful plotting library for Python that enables the creation of a wide range of static, animated, and interactive plots. It offers a high level of control&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[214,213],"tags":[],"class_list":["post-309","post","type-post","status-publish","format-standard","hentry","category-beginners-guide","category-python"],"featured_image_src":null,"author_info":{"display_name":"shababdoo","author_link":"https:\/\/python.garden\/index.php\/author\/shababdoo\/"},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/posts\/309","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/comments?post=309"}],"version-history":[{"count":0,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/posts\/309\/revisions"}],"wp:attachment":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/media?parent=309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/categories?post=309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/tags?post=309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}