{"id":1690,"date":"2021-11-12T23:17:02","date_gmt":"2021-11-12T23:17:02","guid":{"rendered":"https:\/\/thepythoncodingbook.com\/?page_id=1690"},"modified":"2024-05-21T11:40:32","modified_gmt":"2024-05-21T10:40:32","slug":"basics-of-data-visualisation-in-python-using-matplotlib","status":"publish","type":"page","link":"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/","title":{"rendered":"10 | Basics of Data Visualisation in Python Using Matplotlib"},"content":{"rendered":"\n<hr class=\"wp-block-separator has-text-color has-alpha-channel-opacity has-background aligncenter is-style-wide\" style=\"background-color:#6d227a;color:#6d227a\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">More and more applications of programming using Python involve large amounts of data. Visualising those data is an essential part of understanding what the data say, as every scientist, data scientist, and anyone who works with data will confirm. Displaying data visually is important for those studying the data and also for those to whom the data is presented. In this Chapter, you&#8217;ll learn about the basics of data visualisation in Python.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There are several third-party modules in Python that you can use to visualise data. One of the most important of these is Matplotlib. There are also newer modules that are very popular in specific applications. However, Matplotlib remains the most widely-used data visualisation module across Python in general. Even if you&#8217;ll eventually move to other visualisation libraries, a good knowledge of Matplotlib is essential. You can also translate many of the concepts you&#8217;ll learn about in this Chapter to other libraries that are used for data visualisation in Python.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>In this Chapter, you&#8217;ll learn:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>the <strong>fundamentals<\/strong> of plotting figures<\/li>\n\n\n\n<li>when and how to use the <strong>two interfaces<\/strong> in Matplotlib<\/li>\n\n\n\n<li>how to plot <strong>2D figures<\/strong>, including using <strong>subplots<\/strong><\/li>\n\n\n\n<li>how to <strong>display images<\/strong><\/li>\n\n\n\n<li>how to plot <strong>3D figures<\/strong><\/li>\n\n\n\n<li>how to <strong>create animations<\/strong><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">What this Chapter will <strong>not<\/strong> do is teach you about every function available in Matplotlib and how to plot every type of graph you&#8217;ll ever need. Matplotlib is a vast library that can be used in many versatile ways. However, once you understand the fundamentals, you&#8217;ll be able to find solutions to plot more advanced figures, too. The excellent <a href=\"https:\/\/matplotlib.org\/stable\/index.html\">Matplotlib documentation<\/a> will help you along your journey.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-alpha-channel-opacity has-background aligncenter is-style-wide\" style=\"background-color:#6d227a;color:#6d227a\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><em>A video course mirroring the content of this chapter is coming soon at <strong><a href=\"https:\/\/thepythoncodingplace.com\/membership?utm_source=tpcb\">The Python Coding Place<\/a><\/strong><\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-alpha-channel-opacity has-background aligncenter is-style-wide\" style=\"background-color:#6d227a;color:#6d227a\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"getting-started-with-matplotlib\">Getting Started With Matplotlib<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Matplotlib is a third-party library that you&#8217;ll need to install first. You can refer to the section <em>Installing Third-Party Modules<\/em> in the Chapter about <a href=\"https:\/\/thepythoncodingbook.com\/using-numpy-numerical-python-for-quantitative-applications\/\">using NumPy<\/a>, which has detailed instructions on the options available to install modules. You can either use your IDE&#8217;s in-built tools or type the following in the Terminal:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> pip install matplotlib<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">or<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> python -m pip install matplotlib<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;ve already installed NumPy when working on a previous Chapter that introduced this module. However, if you hadn&#8217;t already done so, installing Matplotlib will also install NumPy.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"plotting-your-first-figure\">Plotting your first figure<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Later in this Chapter, you&#8217;ll read about the two interfaces you can use in Matplotlib to plot figures. For now, you&#8217;ll use the simpler option:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import matplotlib.pyplot as plt\n\ndays = [\"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\", \"Sun\"]\nsteps_walked = [8934, 14902, 3409, 25672, 12300, 2023, 6890]\n\nplt.plot(steps_walked)\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You start by importing <code>matplotlib.pyplot<\/code> and use the alias <code>plt<\/code>, which is the alias used by convention for this submodule. Matplotlib is a library that contains several submodules such as <code>pyplot<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After defining the two lists <code>days<\/code> and <code>steps_walked<\/code>, you use two of the functions from <code>matplotlib.pyplot<\/code>. The <code>plot()<\/code> function plots a graph using the data in its argument. In this case, the data are the numbers in <code>steps_walked<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When writing code in a script, as in the example above, <code>plot()<\/code> by itself is not sufficient to display the graph on the screen. <code>show()<\/code> tells the program that you want the plot to be displayed. When using an interactive environment such as the Console, the call to <code>show()<\/code> is not required. In the Snippets section at the end of this Chapter you can also read about Jupyter, another interactive coding environment used extensively for data exploration and presentation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When you run the code above, the program will display a new window showing the following graph:<\/p>\n\n\n<div class=\"wp-block-image is-resized\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"554\" data-attachment-id=\"1692\" data-permalink=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/ch10_steps_1\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_1.png?fit=1280%2C960&amp;ssl=1\" data-orig-size=\"1280,960\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ch10_steps_1\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_1.png?fit=739%2C554&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_1.png?resize=739%2C554&#038;ssl=1\" alt=\"Basic Matplotlib figure for data visualisation in Python\" class=\"wp-image-1692\" srcset=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_1.png?resize=1024%2C768&amp;ssl=1 1024w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_1.png?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_1.png?resize=768%2C576&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_1.png?resize=1200%2C900&amp;ssl=1 1200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_1.png?resize=800%2C600&amp;ssl=1 800w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_1.png?resize=400%2C300&amp;ssl=1 400w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_1.png?resize=200%2C150&amp;ssl=1 200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_1.png?resize=1088%2C816&amp;ssl=1 1088w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_1.png?w=1280&amp;ssl=1 1280w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">The plot shows a line graph connecting the values for the number of steps walked each day. The labels on the <em>y<\/em>-axis show the number of steps. However, the <em>x<\/em>-axis shows the numbers between <code>0<\/code> and <code>6<\/code>. These numbers are the index positions of the values within the list.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can call <code>plot()<\/code> with two input arguments instead of one to determine what data to use for both the <em>x<\/em>&#8211; and <em>y<\/em>-axes:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"6\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import matplotlib.pyplot as plt\n\ndays = [\"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\", \"Sun\"]\nsteps_walked = [8934, 14902, 3409, 25672, 12300, 2023, 6890]\n\nplt.plot(days, steps_walked)\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The first argument in <code>plot()<\/code> corresponds to data you want to use for the <em>x<\/em>-axis, and the second argument represents the <em>y<\/em>-axis values. The code now gives the following output:<\/p>\n\n\n<div class=\"wp-block-image is-resized\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"554\" data-attachment-id=\"1694\" data-permalink=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/ch10_steps_2\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_2.png?fit=1280%2C960&amp;ssl=1\" data-orig-size=\"1280,960\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ch10_steps_2\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_2.png?fit=739%2C554&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_2.png?resize=739%2C554&#038;ssl=1\" alt=\"Plotting x against y in Matplotlib\" class=\"wp-image-1694\" srcset=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_2.png?resize=1024%2C768&amp;ssl=1 1024w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_2.png?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_2.png?resize=768%2C576&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_2.png?resize=1200%2C900&amp;ssl=1 1200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_2.png?resize=800%2C600&amp;ssl=1 800w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_2.png?resize=400%2C300&amp;ssl=1 400w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_2.png?resize=200%2C150&amp;ssl=1 200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_2.png?resize=1088%2C816&amp;ssl=1 1088w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_2.png?w=1280&amp;ssl=1 1280w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">The labels on the <em>x<\/em>-axis now show the days of the week since these are the values in the list <code>days<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"customising-the-plots\">Customising the plots<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can customise the plot further. First, you can add a marker to show where each data point is:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"6\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import matplotlib.pyplot as plt\n\ndays = [\"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\", \"Sun\"]\nsteps_walked = [8934, 14902, 3409, 25672, 12300, 2023, 6890]\n\nplt.plot(days, steps_walked, \"o\")\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The third argument in <code>plot()<\/code> now indicates what marker you&#8217;d like to use. The string <code>\"o\"<\/code> represents filled circles. The output now looks like this:<\/p>\n\n\n<div class=\"wp-block-image is-resized\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"554\" data-attachment-id=\"1696\" data-permalink=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/ch10_steps_3\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_3.png?fit=1280%2C960&amp;ssl=1\" data-orig-size=\"1280,960\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ch10_steps_3\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_3.png?fit=739%2C554&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_3.png?resize=739%2C554&#038;ssl=1\" alt=\"Using markers with plt.plot() in Matplotlib\" class=\"wp-image-1696\" srcset=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_3.png?resize=1024%2C768&amp;ssl=1 1024w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_3.png?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_3.png?resize=768%2C576&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_3.png?resize=1200%2C900&amp;ssl=1 1200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_3.png?resize=800%2C600&amp;ssl=1 800w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_3.png?resize=400%2C300&amp;ssl=1 400w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_3.png?resize=200%2C150&amp;ssl=1 200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_3.png?resize=1088%2C816&amp;ssl=1 1088w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_3.png?w=1280&amp;ssl=1 1280w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">There&#8217;s now a dot to show each data point. However, the line is no longer there. If you&#8217;d like to plot markers but keep the line connecting the data points, you can use <code>\"o-\"<\/code> as the format string:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"6\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import matplotlib.pyplot as plt\n\ndays = [\"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\", \"Sun\"]\nsteps_walked = [8934, 14902, 3409, 25672, 12300, 2023, 6890]\n\nplt.plot(days, steps_walked, \"o-\")\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Your plot now shows the marker and the line:<\/p>\n\n\n<div class=\"wp-block-image is-resized\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"554\" data-attachment-id=\"1698\" data-permalink=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/ch10_steps_4\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_4.png?fit=1280%2C960&amp;ssl=1\" data-orig-size=\"1280,960\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ch10_steps_4\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_4.png?fit=739%2C554&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_4.png?resize=739%2C554&#038;ssl=1\" alt=\"Using markers and solid lines in plt.plot() in Matplotlib\" class=\"wp-image-1698\" srcset=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_4.png?resize=1024%2C768&amp;ssl=1 1024w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_4.png?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_4.png?resize=768%2C576&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_4.png?resize=1200%2C900&amp;ssl=1 1200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_4.png?resize=800%2C600&amp;ssl=1 800w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_4.png?resize=400%2C300&amp;ssl=1 400w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_4.png?resize=200%2C150&amp;ssl=1 200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_4.png?resize=1088%2C816&amp;ssl=1 1088w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_4.png?w=1280&amp;ssl=1 1280w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">And you can add colour to the format string, too. In the example below, you also change the marker to an <code>x<\/code> marker:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"6\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import matplotlib.pyplot as plt\n\ndays = [\"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\", \"Sun\"]\nsteps_walked = [8934, 14902, 3409, 25672, 12300, 2023, 6890]\n\nplt.plot(days, steps_walked, \"x-r\")\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The format string now includes three characters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>x<\/code> shows that the marker should be the <code>x<\/code> symbol<\/li>\n\n\n\n<li><code>-<\/code> draws the line<\/li>\n\n\n\n<li><code>r<\/code> indicates the colour should be red<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This code gives the following output:<\/p>\n\n\n<div class=\"wp-block-image is-resized\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"554\" data-attachment-id=\"1700\" data-permalink=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/ch10_steps_5\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_5.png?fit=1280%2C960&amp;ssl=1\" data-orig-size=\"1280,960\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ch10_steps_5\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_5.png?fit=739%2C554&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_5.png?resize=739%2C554&#038;ssl=1\" alt=\"Using colour in plt.plot() in Matplotlib\" class=\"wp-image-1700\" srcset=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_5.png?resize=1024%2C768&amp;ssl=1 1024w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_5.png?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_5.png?resize=768%2C576&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_5.png?resize=1200%2C900&amp;ssl=1 1200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_5.png?resize=800%2C600&amp;ssl=1 800w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_5.png?resize=400%2C300&amp;ssl=1 400w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_5.png?resize=200%2C150&amp;ssl=1 200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_5.png?resize=1088%2C816&amp;ssl=1 1088w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_5.png?w=1280&amp;ssl=1 1280w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">You can finish this section with one final example using the format string <code>\"x:r\"<\/code>. The colon indicates that the line drawn should be a dotted line:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"6\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import matplotlib.pyplot as plt\n\ndays = [\"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\", \"Sun\"]\nsteps_walked = [8934, 14902, 3409, 25672, 12300, 2023, 6890]\n\nplt.plot(days, steps_walked, \"x:r\")\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The plot now has <code>x<\/code> as a marker and a dotted line connecting the data points:<\/p>\n\n\n<div class=\"wp-block-image is-resized\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"554\" data-attachment-id=\"1702\" data-permalink=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/ch10_steps_6\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_6.png?fit=1280%2C960&amp;ssl=1\" data-orig-size=\"1280,960\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ch10_steps_6\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_6.png?fit=739%2C554&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_6.png?resize=739%2C554&#038;ssl=1\" alt=\"Using dotted lines in plt.plot() in Matplotlib\" class=\"wp-image-1702\" srcset=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_6.png?resize=1024%2C768&amp;ssl=1 1024w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_6.png?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_6.png?resize=768%2C576&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_6.png?resize=1200%2C900&amp;ssl=1 1200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_6.png?resize=800%2C600&amp;ssl=1 800w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_6.png?resize=400%2C300&amp;ssl=1 400w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_6.png?resize=200%2C150&amp;ssl=1 200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_6.png?resize=1088%2C816&amp;ssl=1 1088w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_6.png?w=1280&amp;ssl=1 1280w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">You can see a list of all the markers, colours, and line styles you can use in the section labelled <em>Notes<\/em> on the <a href=\"https:\/\/matplotlib.org\/stable\/api\/_as_gen\/matplotlib.pyplot.plot.html?highlight=plot#matplotlib.pyplot.plot\">documentation page for <code>plot()<\/code><\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"adding-titles-and-labels\">Adding titles and labels<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can explore a few more functions in <code>matplotlib.pyplot<\/code> to add titles and labels to the plot:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"7-9\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import matplotlib.pyplot as plt\n\ndays = [\"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\", \"Sun\"]\nsteps_walked = [8934, 14902, 3409, 25672, 12300, 2023, 6890]\n\nplt.plot(days, steps_walked, \"x:r\")\nplt.title(\"Step count for the week\")\nplt.xlabel(\"Days of the week\")\nplt.ylabel(\"Steps walked\")\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>title()<\/code> function does what it says! And <code>xlabel()<\/code> and <code>ylabel()<\/code> add labels to the two axes:<\/p>\n\n\n<div class=\"wp-block-image is-resized\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"554\" data-attachment-id=\"1704\" data-permalink=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/ch10_steps_7\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_7.png?fit=1280%2C960&amp;ssl=1\" data-orig-size=\"1280,960\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ch10_steps_7\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_7.png?fit=739%2C554&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_7.png?resize=739%2C554&#038;ssl=1\" alt=\"Adding titles and axes labels with plt.title(), plt.xlabel(), and plt.ylabel() in Matplotlib\" class=\"wp-image-1704\" srcset=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_7.png?resize=1024%2C768&amp;ssl=1 1024w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_7.png?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_7.png?resize=768%2C576&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_7.png?resize=1200%2C900&amp;ssl=1 1200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_7.png?resize=800%2C600&amp;ssl=1 800w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_7.png?resize=400%2C300&amp;ssl=1 400w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_7.png?resize=200%2C150&amp;ssl=1 200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_7.png?resize=1088%2C816&amp;ssl=1 1088w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_7.png?w=1280&amp;ssl=1 1280w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">You can now add a second list with the number of steps walked the previous week so that you can compare this week&#8217;s step count with the previous week&#8217;s:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"5,7-9,12\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import matplotlib.pyplot as plt\n\ndays = [\"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\", \"Sun\"]\nsteps_walked = [8934, 14902, 3409, 25672, 12300, 2023, 6890]\nsteps_last_week = [9788, 8710, 5308, 17630, 21309, 4002, 5223]\n\nplt.plot(days, steps_walked, \"o-g\")\nplt.plot(days, steps_last_week, \"v--m\")\nplt.title(\"Step count | This week and last week\")\nplt.xlabel(\"Days of the week\")\nplt.ylabel(\"Steps walked\")\nplt.grid(True)\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You call <code>plot()<\/code> twice in the code above. One call plots the data in <code>steps_walked<\/code>. The format string you use is <code>\"o-g\"<\/code> which represents green circle markers and a solid line. The second call to <code>plot()<\/code> has <code>steps_last_week<\/code> as its second argument and the format string <code>\"v--m\"<\/code> which represents magenta triangle markers connected with a dashed line.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You also include a call to the <code>grid()<\/code> function, which allows you to toggle a grid displayed on the graph. The code above gives the following output:<\/p>\n\n\n<div class=\"wp-block-image is-resized\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"554\" data-attachment-id=\"1706\" data-permalink=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/ch10_steps_8\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_8.png?fit=1280%2C960&amp;ssl=1\" data-orig-size=\"1280,960\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ch10_steps_8\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_8.png?fit=739%2C554&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_8.png?resize=739%2C554&#038;ssl=1\" alt=\"Putting two plots in the same figure using plt.plot() in Matplotlib\" class=\"wp-image-1706\" srcset=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_8.png?resize=1024%2C768&amp;ssl=1 1024w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_8.png?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_8.png?resize=768%2C576&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_8.png?resize=1200%2C900&amp;ssl=1 1200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_8.png?resize=800%2C600&amp;ssl=1 800w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_8.png?resize=400%2C300&amp;ssl=1 400w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_8.png?resize=200%2C150&amp;ssl=1 200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_8.png?resize=1088%2C816&amp;ssl=1 1088w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_8.png?w=1280&amp;ssl=1 1280w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">To finish off this graph, you need to identify which plot is which. You can add a legend to your plot:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"13\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import matplotlib.pyplot as plt\n\ndays = [\"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\", \"Sun\"]\nsteps_walked = [8934, 14902, 3409, 25672, 12300, 2023, 6890]\nsteps_last_week = [9788, 8710, 5308, 17630, 21309, 4002, 5223]\n\nplt.plot(days, steps_walked, \"o-g\")\nplt.plot(days, steps_last_week, \"v--m\")\nplt.title(\"Step count | This week and last week\")\nplt.xlabel(\"Days of the week\")\nplt.ylabel(\"Steps walked\")\nplt.grid(True)\nplt.legend([\"This week\", \"Last week\"])\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You use a list of strings as an argument for <code>legend()<\/code> which gives the following figure:<\/p>\n\n\n<div class=\"wp-block-image is-resized\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"554\" data-attachment-id=\"1709\" data-permalink=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/ch10_steps_9\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_9.png?fit=1280%2C960&amp;ssl=1\" data-orig-size=\"1280,960\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ch10_steps_9\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_9.png?fit=739%2C554&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_9.png?resize=739%2C554&#038;ssl=1\" alt=\"Adding a legend to a Matplotlib plot using plt.legend()\" class=\"wp-image-1709\" srcset=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_9.png?resize=1024%2C768&amp;ssl=1 1024w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_9.png?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_9.png?resize=768%2C576&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_9.png?resize=1200%2C900&amp;ssl=1 1200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_9.png?resize=800%2C600&amp;ssl=1 800w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_9.png?resize=400%2C300&amp;ssl=1 400w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_9.png?resize=200%2C150&amp;ssl=1 200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_9.png?resize=1088%2C816&amp;ssl=1 1088w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_9.png?w=1280&amp;ssl=1 1280w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Now that you know the basics of plotting using Matplotlib, you can dive a bit deeper into the various components that make up a figure.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-s-a-matplotlib-figure-made-of\">What&#8217;s a Matplotlib Figure Made Of?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When working with data visualisation in Python, you&#8217;ll want to have control over all aspects of your figure. In this section, you&#8217;ll learn about the main components that make up a figure in Matplotlib.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Everything in Python is an object, and therefore, so is a Matplotlib figure. In fact, a Matplotlib figure is made up of several objects of different data types.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There are three main parts to a Matplotlib figure:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>Figure<\/code><\/strong>: This is the whole region of space that&#8217;s created when you create any figure. The <code>Figure<\/code> object is the overall object that contains everything else.<\/li>\n\n\n\n<li><strong><code>Axes<\/code><\/strong>: An <code>Axes<\/code> object is the object that contains the <em>x-<\/em>axis and <em>y-<\/em>axis for a 2D plot. Each <code>Axes<\/code> object corresponds to a plot or a graph. You can have more than one <code>Axes<\/code> object in a <code>Figure<\/code>, as you&#8217;ll see later on in this Chapter.<\/li>\n\n\n\n<li><strong><code>Axis<\/code><\/strong>: An <code>Axis<\/code> object contains one of the axes, the <em>x<\/em>-axis or the <em>y<\/em>-axis for a 2D plot.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Therefore, a Matplotlib figure is a <code>Figure<\/code> object which has one or more <code>Axes<\/code> objects. Each <code>Axes<\/code> object has two or three <code>Axis<\/code> objects.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can see the relationship between these three parts of a figure in the diagram below:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"739\" data-attachment-id=\"1710\" data-permalink=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/ch10_figure_anatomy_1\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_figure_anatomy_1.png?fit=800%2C800&amp;ssl=1\" data-orig-size=\"800,800\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ch10_figure_anatomy_1\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_figure_anatomy_1.png?fit=739%2C739&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_figure_anatomy_1.png?resize=739%2C739&#038;ssl=1\" alt=\"The anatomy of a Matplotlib figure with one Axes\" class=\"wp-image-1710\" srcset=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_figure_anatomy_1.png?w=800&amp;ssl=1 800w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_figure_anatomy_1.png?resize=300%2C300&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_figure_anatomy_1.png?resize=150%2C150&amp;ssl=1 150w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_figure_anatomy_1.png?resize=768%2C768&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_figure_anatomy_1.png?resize=400%2C400&amp;ssl=1 400w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_figure_anatomy_1.png?resize=200%2C200&amp;ssl=1 200w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"739\" data-attachment-id=\"1712\" data-permalink=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/ch10_figure_anatomy_2\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_figure_anatomy_2.png?fit=800%2C800&amp;ssl=1\" data-orig-size=\"800,800\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ch10_figure_anatomy_2\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_figure_anatomy_2.png?fit=739%2C739&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_figure_anatomy_2.png?resize=739%2C739&#038;ssl=1\" alt=\"The anatomy of a Matplotlib figure in Python with subplots\" class=\"wp-image-1712\" srcset=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_figure_anatomy_2.png?w=800&amp;ssl=1 800w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_figure_anatomy_2.png?resize=300%2C300&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_figure_anatomy_2.png?resize=150%2C150&amp;ssl=1 150w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_figure_anatomy_2.png?resize=768%2C768&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_figure_anatomy_2.png?resize=400%2C400&amp;ssl=1 400w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_figure_anatomy_2.png?resize=200%2C200&amp;ssl=1 200w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The first diagram shows the simplest figure you can create in Matplotlib in which the <code>Figure<\/code> object contains one <code>Axes<\/code> object. The <code>Axes<\/code> object contains two <code>Axis<\/code> objects. The diagram on the right shows four <code>Axes<\/code> objects within the same <code>Figure<\/code> object. These are called subplots, and you&#8217;ll read more about them shortly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There are other objects present in a figure, too. The general data type for objects in a figure is the <strong><code>Artist<\/code><\/strong> type. These include components of a figure such as the markers, lines connecting the data points, titles, legends, labels, and more.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"the-alternative-way-of-creating-a-figure-in-matplotlib\">The Alternative Way of Creating a Figure in Matplotlib<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Matplotlib offers two ways of creating a figure. You&#8217;ve already seen how to use one of the interfaces earlier in this Chapter. In this section, you&#8217;ll learn about the second option. You may wonder why you need to have two ways to do the same thing. You&#8217;ll find that each interface has some advantages and disadvantages. The short answer is that one option is simpler to use, and the other option gives you more flexibility to customise.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The two methods are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use <code>pyplot<\/code> functions directly. These functions will automatically create <code>Figure<\/code>, <code>Axes<\/code>, and other objects and manage them for you. This is the method you used earlier in this Chapter.<\/li>\n\n\n\n<li>Create <code>Figure<\/code> and <code>Axes<\/code> objects and call each object&#8217;s methods. This is the object-oriented programming approach.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">You can now recreate the last figure you plotted earlier using the object-oriented method. To create a figure, you can use the function <code>subplots()<\/code>, which returns a tuple containing a <code>Figure<\/code> object and an <code>Axes<\/code> object when it&#8217;s called without arguments:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import matplotlib.pyplot as plt\n\nfig, ax = plt.subplots()\n\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">By convention, the names <code>fig<\/code> and <code>ax<\/code> are used for <code>Figure<\/code> and <code>Axes<\/code> objects, although you can, of course, use other variable names if you have a reason to do so.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The visual output from this code is a figure containing a pair of axes:<\/p>\n\n\n<div class=\"wp-block-image is-resized\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"554\" data-attachment-id=\"1716\" data-permalink=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/ch10_steps_oop_1\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_1.png?fit=1280%2C960&amp;ssl=1\" data-orig-size=\"1280,960\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ch10_steps_oop_1\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_1.png?fit=739%2C554&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_1.png?resize=739%2C554&#038;ssl=1\" alt=\"Creating Figure and Axes in Matplotlib using the OOP approach\" class=\"wp-image-1716\" srcset=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_1.png?resize=1024%2C768&amp;ssl=1 1024w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_1.png?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_1.png?resize=768%2C576&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_1.png?resize=1200%2C900&amp;ssl=1 1200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_1.png?resize=800%2C600&amp;ssl=1 800w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_1.png?resize=400%2C300&amp;ssl=1 400w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_1.png?resize=200%2C150&amp;ssl=1 200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_1.png?resize=1088%2C816&amp;ssl=1 1088w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_1.png?w=1280&amp;ssl=1 1280w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Within your code, you have access to the <code>Figure<\/code> object and the <code>Axes<\/code> object separately through the variables <code>fig<\/code> and <code>ax<\/code>. Note that when using the simpler method earlier, you didn&#8217;t need to call any function to create the <code>Figure<\/code> or <code>Axes<\/code> objects as this was done automatically when you first call <code>plt.plot()<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can now plot the two sets of data and add the other components you had earlier:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import matplotlib.pyplot as plt\n\ndays = [\"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\", \"Sun\"]\nsteps_walked = [8934, 14902, 3409, 25672, 12300, 2023, 6890]\nsteps_last_week = [9788, 8710, 5308, 17630, 21309, 4002, 5223]\n\nfig, ax = plt.subplots()\n\nax.plot(days, steps_walked, \"o-g\")\nax.plot(days, steps_last_week, \"v--m\")\nax.set_title(\"Step count | This week and last week\")\nax.set_xlabel(\"Days of the week\")\nax.set_ylabel(\"Steps walked\")\nax.grid(True)\nax.legend([\"This week\", \"Last week\"])\n\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can compare this code with the example you wrote earlier using the simpler method. You&#8217;ll notice a few differences. Firstly, you&#8217;re now calling methods of the <code>Axes<\/code> class by using the variable name <code>ax<\/code>. In the previous code, you used <code>plt<\/code>, which is the alias for the submodule <code>matplotlib.pyplot<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Secondly, although some method names mirror the function names you used earlier, others are different. You&#8217;ve used the methods <code>set_title()<\/code>, <code>set_xlabel()<\/code>, and <code>set_ylabel()<\/code> which have different names to the <code>plt<\/code> functions you used earlier.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This code gives the following plot:<\/p>\n\n\n<div class=\"wp-block-image is-resized\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"554\" data-attachment-id=\"1718\" data-permalink=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/ch10_steps_oop_2\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_2.png?fit=1280%2C960&amp;ssl=1\" data-orig-size=\"1280,960\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ch10_steps_oop_2\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_2.png?fit=739%2C554&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_2.png?resize=739%2C554&#038;ssl=1\" alt=\"Creating a figure using subplots() in Matplotlib\" class=\"wp-image-1718\" srcset=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_2.png?resize=1024%2C768&amp;ssl=1 1024w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_2.png?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_2.png?resize=768%2C576&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_2.png?resize=1200%2C900&amp;ssl=1 1200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_2.png?resize=800%2C600&amp;ssl=1 800w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_2.png?resize=400%2C300&amp;ssl=1 400w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_2.png?resize=200%2C150&amp;ssl=1 200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_2.png?resize=1088%2C816&amp;ssl=1 1088w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_2.png?w=1280&amp;ssl=1 1280w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">This figure is identical to the one you plotted earlier. So, why do we need two methods? Let&#8217;s start exploring the additional flexibility you get from the object-oriented interface.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"adding-figure-specific-components\">Adding <code>Figure<\/code>-specific components<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The methods <code>plot()<\/code> and <code>grid()<\/code> have to be methods associated with <code>Axes<\/code> as that&#8217;s where the plot goes. However, the legend and title of a figure could be either linked to the <code>Axes<\/code> or the <code>Figure<\/code>. You can start by creating a second legend but this time linked to the <code>Figure<\/code> object:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"17\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import matplotlib.pyplot as plt\n\ndays = [\"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\", \"Sun\"]\nsteps_walked = [8934, 14902, 3409, 25672, 12300, 2023, 6890]\nsteps_last_week = [9788, 8710, 5308, 17630, 21309, 4002, 5223]\n\nfig, ax = plt.subplots()\n\nax.plot(days, steps_walked, \"o-g\")\nax.plot(days, steps_last_week, \"v--m\")\nax.set_title(\"Step count | This week and last week\")\nax.set_xlabel(\"Days of the week\")\nax.set_ylabel(\"Steps walked\")\nax.grid(True)\nax.legend([\"This week\", \"Last week\"])\n\nfig.legend([\"Steps this week\", \"Steps last week\"])\n\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;re now calling two methods called <code>legend()<\/code>. However, <code>ax.legend()<\/code> is a method of the <code>Axes<\/code> class, whereas <code>fig.legend()<\/code> is a method of the <code>Figure<\/code> class. The text in the figure-wide legend is different in this example to make it easier to identify which legend is which:<\/p>\n\n\n<div class=\"wp-block-image is-resized\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"554\" data-attachment-id=\"1720\" data-permalink=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/ch10_steps_oop_3\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_3.png?fit=1280%2C960&amp;ssl=1\" data-orig-size=\"1280,960\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ch10_steps_oop_3\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_3.png?fit=739%2C554&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_3.png?resize=739%2C554&#038;ssl=1\" alt=\"Adding legends using fig.legend() and ax.legend() in Matplotlib\" class=\"wp-image-1720\" srcset=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_3.png?resize=1024%2C768&amp;ssl=1 1024w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_3.png?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_3.png?resize=768%2C576&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_3.png?resize=1200%2C900&amp;ssl=1 1200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_3.png?resize=800%2C600&amp;ssl=1 800w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_3.png?resize=400%2C300&amp;ssl=1 400w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_3.png?resize=200%2C150&amp;ssl=1 200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_3.png?resize=1088%2C816&amp;ssl=1 1088w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_3.png?w=1280&amp;ssl=1 1280w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">There are now two legends in the output. One is linked to the <code>Axes<\/code> object and the other to the <code>Figure<\/code> object. The reason why you may need access to both versions will become clearer when you learn about subplots later in this Chapter.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You may have noticed that the figure-wide legend is partially obscuring the title. You can customise your plot in any way you wish to resolve issues with the default sizes and positions. In this case, you can choose a wider size for your figure by setting the <code>figsize<\/code> parameter when you create the figure. You can also add a figure-wide title using the <code>suptitle()<\/code> method of the <code>Figure<\/code> class:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"7,18\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import matplotlib.pyplot as plt\n\ndays = [\"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\", \"Sun\"]\nsteps_walked = [8934, 14902, 3409, 25672, 12300, 2023, 6890]\nsteps_last_week = [9788, 8710, 5308, 17630, 21309, 4002, 5223]\n\nfig, ax = plt.subplots(figsize=(8, 5))\n\nax.plot(days, steps_walked, \"o-g\")\nax.plot(days, steps_last_week, \"v--m\")\nax.set_title(\"Step count | This week and last week\")\nax.set_xlabel(\"Days of the week\")\nax.set_ylabel(\"Steps walked\")\nax.grid(True)\nax.legend([\"This week\", \"Last week\"])\n\nfig.legend([\"Steps this week\", \"Steps last week\"])\nfig.suptitle(\"Overall Figure Title\")\n\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The default size unit in Matplotlib is inches. The image displayed is now wider and the figure-wide legend no longer obscures the title. There are also two titles. One is linked to the <code>Axes<\/code> object and the other to the <code>Figure<\/code> object:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"462\" data-attachment-id=\"1722\" data-permalink=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/ch10_steps_oop_4\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_4.png?fit=1600%2C1000&amp;ssl=1\" data-orig-size=\"1600,1000\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ch10_steps_oop_4\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_4.png?fit=739%2C462&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_4.png?resize=739%2C462&#038;ssl=1\" alt=\"Plotting a Matplotlib figure in Python using the OOP approach\" class=\"wp-image-1722\" srcset=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_4.png?resize=1024%2C640&amp;ssl=1 1024w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_4.png?resize=300%2C188&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_4.png?resize=768%2C480&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_4.png?resize=1536%2C960&amp;ssl=1 1536w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_4.png?resize=1200%2C750&amp;ssl=1 1200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_4.png?resize=1088%2C680&amp;ssl=1 1088w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_4.png?w=1600&amp;ssl=1 1600w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_oop_4.png?w=1478&amp;ssl=1 1478w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">The distinction between figure-wide components and those specific to a set of axes will become clearer in the next section when you learn about subplots.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"creating-subplots\">Creating Subplots<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In the previous example, you used <code>plt.subplots()<\/code> either with no input arguments or with the keyword argument <code>figsize<\/code>. The function also has two positional arguments <code>nrows<\/code> and <code>ncols<\/code>, which both have a default value of <code>1<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can create a <code>Figure<\/code> object that contains more than one <code>Axes<\/code> object by using different values for <code>nrows<\/code> and <code>ncols<\/code>:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import matplotlib.pyplot as plt\n\nfig, axs = plt.subplots(1, 2)\n\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The arguments <code>1, 2<\/code> define a grid of axes consisting of one row and two columns. Therefore, this call to <code>subplots()<\/code> creates two <code>Axes<\/code> objects. When you create multiple <code>Axes<\/code> in this way, by convention, you can use the variable name <code>axs<\/code>, which is a <code>numpy.ndarray<\/code> containing the <code>Axes<\/code> objects.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The output from this code shows the two sets of axes displayed in the same figure:<\/p>\n\n\n<div class=\"wp-block-image is-resized\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"554\" data-attachment-id=\"1724\" data-permalink=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/ch10_steps_subplots_1\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_1.png?fit=1280%2C960&amp;ssl=1\" data-orig-size=\"1280,960\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ch10_steps_subplots_1\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_1.png?fit=739%2C554&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_1.png?resize=739%2C554&#038;ssl=1\" alt=\"Subplots in a Matplotlib figure\" class=\"wp-image-1724\" srcset=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_1.png?resize=1024%2C768&amp;ssl=1 1024w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_1.png?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_1.png?resize=768%2C576&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_1.png?resize=1200%2C900&amp;ssl=1 1200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_1.png?resize=800%2C600&amp;ssl=1 800w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_1.png?resize=400%2C300&amp;ssl=1 400w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_1.png?resize=200%2C150&amp;ssl=1 200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_1.png?resize=1088%2C816&amp;ssl=1 1088w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_1.png?w=1280&amp;ssl=1 1280w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Therefore, <code>plt.subplots()<\/code> returns one of the following return values:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>a tuple containing a <code>Figure<\/code> object and an <code>Axes<\/code> object if <code>nrows<\/code> and <code>ncols<\/code> are both <code>1<\/code>. In this case, only one <code>Axes<\/code> object is created<\/li>\n\n\n\n<li>a tuple containing a <code>Figure<\/code> object and a <code>numpy.ndarray<\/code> if <code>nrows<\/code> and <code>ncols<\/code> aren&#8217;t both <code>1<\/code>. The array contains <code>Axes<\/code> objects. The shape of the array is the same as the shape of the grid containing the subplots. Therefore, the call <code>plt.subplots(2, 2)<\/code>, for example, will return a <code>2x2<\/code> <code>numpy.ndarray<\/code> as its second return value.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Now, you can recreate the plot you worked on earlier in the first of these subplots:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import matplotlib.pyplot as plt\n\ndays = [\"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\", \"Sun\"]\nsteps_walked = [8934, 14902, 3409, 25672, 12300, 2023, 6890]\nsteps_last_week = [9788, 8710, 5308, 17630, 21309, 4002, 5223]\n\nfig, axs = plt.subplots(1, 2)\n\naxs[0].plot(days, steps_walked, \"o-g\")\naxs[0].plot(days, steps_last_week, \"v--m\")\naxs[0].set_title(\"Step count | This week and last week\")\naxs[0].set_xlabel(\"Days of the week\")\naxs[0].set_ylabel(\"Steps walked\")\naxs[0].grid(True)\n\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Since you&#8217;re creating a <code>1x2<\/code> grid of subplots, the array <code>axs<\/code> is also a <code>1x2<\/code> array. Therefore, <code>axs[0]<\/code> is the <code>Axes<\/code> object representing the first set of axes in the figure. This code gives the following output:<\/p>\n\n\n<div class=\"wp-block-image is-resized\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"554\" data-attachment-id=\"1726\" data-permalink=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/ch10_steps_subplots_2\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_2.png?fit=1280%2C960&amp;ssl=1\" data-orig-size=\"1280,960\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ch10_steps_subplots_2\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_2.png?fit=739%2C554&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_2.png?resize=739%2C554&#038;ssl=1\" alt=\"Plotting in one of the subplots in a Matplotlib figure\" class=\"wp-image-1726\" srcset=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_2.png?resize=1024%2C768&amp;ssl=1 1024w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_2.png?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_2.png?resize=768%2C576&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_2.png?resize=1200%2C900&amp;ssl=1 1200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_2.png?resize=800%2C600&amp;ssl=1 800w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_2.png?resize=400%2C300&amp;ssl=1 400w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_2.png?resize=200%2C150&amp;ssl=1 200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_2.png?resize=1088%2C816&amp;ssl=1 1088w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_2.png?w=1280&amp;ssl=1 1280w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">You can now represent the same data using a bar chart on the right-hand side:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"18-23\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import matplotlib.pyplot as plt\n\ndays = [\"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\", \"Sun\"]\nsteps_walked = [8934, 14902, 3409, 25672, 12300, 2023, 6890]\nsteps_last_week = [9788, 8710, 5308, 17630, 21309, 4002, 5223]\n\nfig, axs = plt.subplots(1, 2)\n\n# Plot line chart\naxs[0].plot(days, steps_walked, \"o-g\")\naxs[0].plot(days, steps_last_week, \"v--m\")\naxs[0].set_title(\"Step count | This week and last week\")\naxs[0].set_xlabel(\"Days of the week\")\naxs[0].set_ylabel(\"Steps walked\")\naxs[0].grid(True)\n\n# Plot bar chart\naxs[1].bar(days, steps_walked)\naxs[1].bar(days, steps_last_week)\naxs[1].set_title(\"Step count | This week and last week\")\naxs[1].set_xlabel(\"Days of the week\")\naxs[1].set_ylabel(\"Steps walked\")\naxs[1].grid(True)\n\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">On the second set of axes, you&#8217;re now using the <code>Axes<\/code> method <code>bar()<\/code> to draw two bar charts, one for the steps walked for the current week and another for the previous week. The output from this code is the following:<\/p>\n\n\n<div class=\"wp-block-image is-resized\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"554\" data-attachment-id=\"1728\" data-permalink=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/ch10_steps_subplots_3\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_3.png?fit=1280%2C960&amp;ssl=1\" data-orig-size=\"1280,960\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ch10_steps_subplots_3\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_3.png?fit=739%2C554&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_3.png?resize=739%2C554&#038;ssl=1\" alt=\"Plotting a bar plot in a subplot in Matplotlib in Python\" class=\"wp-image-1728\" srcset=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_3.png?resize=1024%2C768&amp;ssl=1 1024w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_3.png?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_3.png?resize=768%2C576&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_3.png?resize=1200%2C900&amp;ssl=1 1200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_3.png?resize=800%2C600&amp;ssl=1 800w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_3.png?resize=400%2C300&amp;ssl=1 400w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_3.png?resize=200%2C150&amp;ssl=1 200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_3.png?resize=1088%2C816&amp;ssl=1 1088w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_3.png?w=1280&amp;ssl=1 1280w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">You&#8217;ll note that there are a couple of issues with this figure. Firstly, everything is cramped and there&#8217;s overlap between elements of both subplots. You&#8217;ll fix this by changing the size of the figure as you did earlier using the parameter <code>figsize<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, the main problem is that the second bar chart you plotted is drawn on top of the first one. This means that, for some of the days, the data from the previous week is obscuring the information from the current week. This issue happens for the data on Monday, Wednesday, Friday, and Saturday in this example.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"customising-the-bar-plot\">Customising the bar plot<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can fix this by shifting each plot sideways so that they don&#8217;t overlap. Up until now, you used the list <code>days<\/code> as the data in the <em>x<\/em>-axis. You can get more control over where the bars are plotted by using a list of numbers instead. Start by creating two sets of <em>x<\/em>-coordinates for the two sets of bars. You can then use these lists as the first argument in the calls to <code>bar()<\/code>. You can also fix the cramming problem at this stage by setting the figure size when you call <code>plt.subplots()<\/code>:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"7,18-19,21-22\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import matplotlib.pyplot as plt\n\ndays = [\"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\", \"Sun\"]\nsteps_walked = [8934, 14902, 3409, 25672, 12300, 2023, 6890]\nsteps_last_week = [9788, 8710, 5308, 17630, 21309, 4002, 5223]\n\nfig, axs = plt.subplots(1, 2, figsize=(12, 5))\n\n# Plot line chart\naxs[0].plot(days, steps_walked, \"o-g\")\naxs[0].plot(days, steps_last_week, \"v--m\")\naxs[0].set_title(\"Step count | This week and last week\")\naxs[0].set_xlabel(\"Days of the week\")\naxs[0].set_ylabel(\"Steps walked\")\naxs[0].grid(True)\n\n# Plot bar chart\nx_range_current = [-0.2, 0.8, 1.8, 2.8, 3.8, 4.8, 5.8]\nx_range_previous = [0.2, 1.2, 2.2, 3.2, 4.2, 5.2, 6.2]\n\naxs[1].bar(x_range_current, steps_walked)\naxs[1].bar(x_range_previous, steps_last_week)\naxs[1].set_title(\"Step count | This week and last week\")\naxs[1].set_xlabel(\"Days of the week\")\naxs[1].set_ylabel(\"Steps walked\")\naxs[1].grid(True)\n\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;re using the numbers <code>0<\/code> to <code>6<\/code> to represent the days of the week. The numbers in <code>x_range_current<\/code> are shifted by <code>-0.2<\/code> from the numbers in the range <code>0<\/code> to <code>6<\/code>. The numbers in <code>x_range_previous<\/code> are shifted by <code>+0.2<\/code>. Therefore, when you use these values in the two calls to <code>bar()<\/code>, the bar charts plotted are shifted with respect to each other:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"308\" data-attachment-id=\"1730\" data-permalink=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/ch10_steps_subplots_4\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_4.png?fit=2400%2C1000&amp;ssl=1\" data-orig-size=\"2400,1000\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ch10_steps_subplots_4\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_4.png?fit=739%2C308&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_4.png?resize=739%2C308&#038;ssl=1\" alt=\"Adjusting the size of a Matplotlib figure using figsize\" class=\"wp-image-1730\" srcset=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_4.png?resize=1024%2C427&amp;ssl=1 1024w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_4.png?resize=300%2C125&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_4.png?resize=768%2C320&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_4.png?resize=1536%2C640&amp;ssl=1 1536w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_4.png?resize=2048%2C853&amp;ssl=1 2048w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_4.png?resize=1200%2C500&amp;ssl=1 1200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_4.png?resize=1088%2C453&amp;ssl=1 1088w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_4.png?w=1478&amp;ssl=1 1478w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_4.png?w=2217&amp;ssl=1 2217w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Although you can see the separate bars because of the shift, the bars are still overlapping. The default width of each bar is still too large. You can change the width of the bars to prevent them from overlapping. Since you shifted each set of bars by <code>0.2<\/code> from the centre, you can set the width of each bar to <code>0.4<\/code>. You can also change the colour of the bars so that you&#8217;re using the same colour scheme as in the plot on the left-hand side:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"21-22\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import matplotlib.pyplot as plt\n\ndays = [\"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\", \"Sun\"]\nsteps_walked = [8934, 14902, 3409, 25672, 12300, 2023, 6890]\nsteps_last_week = [9788, 8710, 5308, 17630, 21309, 4002, 5223]\n\nfig, axs = plt.subplots(1, 2, figsize=(12, 5))\n\n# Plot line chart\naxs[0].plot(days, steps_walked, \"o-g\")\naxs[0].plot(days, steps_last_week, \"v--m\")\naxs[0].set_title(\"Step count | This week and last week\")\naxs[0].set_xlabel(\"Days of the week\")\naxs[0].set_ylabel(\"Steps walked\")\naxs[0].grid(True)\n\n# Plot bar chart\nx_range_current = [-0.2, 0.8, 1.8, 2.8, 3.8, 4.8, 5.8]\nx_range_previous = [0.2, 1.2, 2.2, 3.2, 4.2, 5.2, 6.2]\n\naxs[1].bar(x_range_current, steps_walked, width=0.4, color=\"g\")\naxs[1].bar(x_range_previous, steps_last_week, width=0.4, color=\"m\")\naxs[1].set_title(\"Step count | This week and last week\")\naxs[1].set_xlabel(\"Days of the week\")\naxs[1].set_ylabel(\"Steps walked\")\naxs[1].grid(True)\n\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This code gives the following figure:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"308\" data-attachment-id=\"1733\" data-permalink=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/ch10_steps_subplots_5\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_5.png?fit=2400%2C1000&amp;ssl=1\" data-orig-size=\"2400,1000\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ch10_steps_subplots_5\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_5.png?fit=739%2C308&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_5.png?resize=739%2C308&#038;ssl=1\" alt=\"Shifting the bars in a bar plot in Matplotlib\" class=\"wp-image-1733\" srcset=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_5.png?resize=1024%2C427&amp;ssl=1 1024w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_5.png?resize=300%2C125&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_5.png?resize=768%2C320&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_5.png?resize=1536%2C640&amp;ssl=1 1536w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_5.png?resize=2048%2C853&amp;ssl=1 2048w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_5.png?resize=1200%2C500&amp;ssl=1 1200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_5.png?resize=1088%2C453&amp;ssl=1 1088w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_5.png?w=1478&amp;ssl=1 1478w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_5.png?w=2217&amp;ssl=1 2217w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">The problem now is that the labels on the <em>x<\/em>-axis no longer show the days of the week. You&#8217;ll also notice that the ticks on the <em>x<\/em>-axis are not the values you&#8217;re using in either of the bar charts you plotted. Matplotlib works out where to place the ticks for you. However, sometimes you may want to override where the ticks are. You can do so using the <code>set_xticks()<\/code> method. You can also change the labels for these ticks using <code>set_xticklabels()<\/code>:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"27-28\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import matplotlib.pyplot as plt\n\ndays = [\"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\", \"Sun\"]\nsteps_walked = [8934, 14902, 3409, 25672, 12300, 2023, 6890]\nsteps_last_week = [9788, 8710, 5308, 17630, 21309, 4002, 5223]\n\nfig, axs = plt.subplots(1, 2, figsize=(12, 5))\n\n# Plot line chart\naxs[0].plot(days, steps_walked, \"o-g\")\naxs[0].plot(days, steps_last_week, \"v--m\")\naxs[0].set_title(\"Step count | This week and last week\")\naxs[0].set_xlabel(\"Days of the week\")\naxs[0].set_ylabel(\"Steps walked\")\naxs[0].grid(True)\n\n# Plot bar chart\nx_range_current = [-0.2, 0.8, 1.8, 2.8, 3.8, 4.8, 5.8]\nx_range_previous = [0.2, 1.2, 2.2, 3.2, 4.2, 5.2, 6.2]\n\naxs[1].bar(x_range_current, steps_walked, width=0.4, color=\"g\")\naxs[1].bar(x_range_previous, steps_last_week, width=0.4, color=\"m\")\naxs[1].set_title(\"Step count | This week and last week\")\naxs[1].set_xlabel(\"Days of the week\")\naxs[1].set_ylabel(\"Steps walked\")\naxs[1].grid(True)\naxs[1].set_xticks(range(7))\naxs[1].set_xticklabels(days)\n\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The call to <code>set_xticks()<\/code> determines where the ticks are placed on the <em>x<\/em>-axis. You&#8217;ll recall that <code>range(7)<\/code> represents the integers between <code>0<\/code> and <code>6<\/code>. The call to <code>set_xticklabels()<\/code> then maps the strings in <code>days<\/code> to these ticks on the <em>x<\/em>-axis. This gives the following figure:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"308\" data-attachment-id=\"1734\" data-permalink=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/ch10_steps_subplots_6\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_6.png?fit=2400%2C1000&amp;ssl=1\" data-orig-size=\"2400,1000\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ch10_steps_subplots_6\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_6.png?fit=739%2C308&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_6.png?resize=739%2C308&#038;ssl=1\" alt=\"Customising x labels in a bar plot in Matplotlib\" class=\"wp-image-1734\" srcset=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_6.png?resize=1024%2C427&amp;ssl=1 1024w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_6.png?resize=300%2C125&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_6.png?resize=768%2C320&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_6.png?resize=1536%2C640&amp;ssl=1 1536w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_6.png?resize=2048%2C853&amp;ssl=1 2048w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_6.png?resize=1200%2C500&amp;ssl=1 1200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_6.png?resize=1088%2C453&amp;ssl=1 1088w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_6.png?w=1478&amp;ssl=1 1478w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_6.png?w=2217&amp;ssl=1 2217w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Before finishing off this figure, let&#8217;s tidy up this code to make it more Pythonic<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"making-the-code-more-pythonic\">Making the code more Pythonic!<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">When writing code, it&#8217;s often convenient to hardwire values in the code as you try out and explore options. However, you should aim to refactor your code to tidy it up when possible. Refactoring means making some changes to how the code looks, but not what it does, to make the code more future-proof and easier to read and maintain.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now that you know the width of the bars and how much to shift them, you can refactor your code as follows:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"18-20,22-23\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import matplotlib.pyplot as plt\n\ndays = [\"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\", \"Sun\"]\nsteps_walked = [8934, 14902, 3409, 25672, 12300, 2023, 6890]\nsteps_last_week = [9788, 8710, 5308, 17630, 21309, 4002, 5223]\n\nfig, axs = plt.subplots(1, 2, figsize=(12, 5))\n\n# Plot line chart\naxs[0].plot(days, steps_walked, \"o-g\")\naxs[0].plot(days, steps_last_week, \"v--m\")\naxs[0].set_title(\"Step count | This week and last week\")\naxs[0].set_xlabel(\"Days of the week\")\naxs[0].set_ylabel(\"Steps walked\")\naxs[0].grid(True)\n\n# Plot bar chart\nbar_width = 0.4\nx_range_current = [idx - bar_width\/2 for idx in range(7)]\nx_range_previous = [idx + bar_width\/2 for idx in range(7)]\n\naxs[1].bar(x_range_current, steps_walked, width=bar_width, color=\"g\")\naxs[1].bar(x_range_previous, steps_last_week, width=bar_width, color=\"m\")\naxs[1].set_title(\"Step count | This week and last week\")\naxs[1].set_xlabel(\"Days of the week\")\naxs[1].set_ylabel(\"Steps walked\")\naxs[1].grid(True)\naxs[1].set_xticks(range(7))\naxs[1].set_xticklabels(days)\n\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You define a variable called <code>bar_width<\/code> and then use it within list comprehensions to generate the shifted <em>x<\/em>-coordinate values for the two sets of bars. The figure displayed by this code is unchanged.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"choosing-figure-and-axes-components\">Choosing <code>Figure<\/code> and <code>Axes<\/code> components<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can now decide which components should be figure-wide and which are specific to one of the <code>Axes<\/code> objects. You can start by adding a legend to the figure. Since the legend should be the same for both subplots, you can use the <code>Figure<\/code> method <code>legend()<\/code> rather than the one that belongs to the <code>Axes<\/code> class. You can also move the separate <code>Axes<\/code> titles, which are identical to a <code>Figure<\/code> title, and replace the <code>Axes<\/code> titles with something more specific:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"12,24,31-33\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import matplotlib.pyplot as plt\n\ndays = [\"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\", \"Sun\"]\nsteps_walked = [8934, 14902, 3409, 25672, 12300, 2023, 6890]\nsteps_last_week = [9788, 8710, 5308, 17630, 21309, 4002, 5223]\n\nfig, axs = plt.subplots(1, 2, figsize=(12, 5))\n\n# Plot line chart\naxs[0].plot(days, steps_walked, \"o-g\")\naxs[0].plot(days, steps_last_week, \"v--m\")\naxs[0].set_title(\"Line graph\")\naxs[0].set_xlabel(\"Days of the week\")\naxs[0].set_ylabel(\"Steps walked\")\naxs[0].grid(True)\n\n# Plot bar chart\nbar_width = 0.4\nx_range_current = [idx - bar_width\/2 for idx in range(7)]\nx_range_previous = [idx + bar_width\/2 for idx in range(7)]\n\naxs[1].bar(x_range_current, steps_walked, width=bar_width, color=\"g\")\naxs[1].bar(x_range_previous, steps_last_week, width=bar_width, color=\"m\")\naxs[1].set_title(\"Bar chart\")\naxs[1].set_xlabel(\"Days of the week\")\naxs[1].set_ylabel(\"Steps walked\")\naxs[1].grid(True)\naxs[1].set_xticks(range(7))\naxs[1].set_xticklabels(days)\n\n# Figure-wide components\nfig.suptitle(\"Step count | This week and last week\")\nfig.legend([\"This week\", \"Last week\"])\n\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The separate control you have over the <code>Figure<\/code> object and <code>Axes<\/code> objects allows you to customise the figure in any way you wish. The code displays the following figure:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"308\" data-attachment-id=\"1736\" data-permalink=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/ch10_steps_subplots_7\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_7.png?fit=2400%2C1000&amp;ssl=1\" data-orig-size=\"2400,1000\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ch10_steps_subplots_7\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_7.png?fit=739%2C308&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_7.png?resize=739%2C308&#038;ssl=1\" alt=\"Adding figure-wide components in a Matplotlib figure with subplots\" class=\"wp-image-1736\" srcset=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_7.png?resize=1024%2C427&amp;ssl=1 1024w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_7.png?resize=300%2C125&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_7.png?resize=768%2C320&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_7.png?resize=1536%2C640&amp;ssl=1 1536w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_7.png?resize=2048%2C853&amp;ssl=1 2048w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_7.png?resize=1200%2C500&amp;ssl=1 1200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_7.png?resize=1088%2C453&amp;ssl=1 1088w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_7.png?w=1478&amp;ssl=1 1478w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_7.png?w=2217&amp;ssl=1 2217w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">To demonstrate this further, you can also remove the separate <em>y<\/em>-axis labels from each <code>Axes<\/code> object and add a single figure-wide <em>y<\/em>-axis label:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"32,34\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import matplotlib.pyplot as plt\n\ndays = [\"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\", \"Sun\"]\nsteps_walked = [8934, 14902, 3409, 25672, 12300, 2023, 6890]\nsteps_last_week = [9788, 8710, 5308, 17630, 21309, 4002, 5223]\n\nfig, axs = plt.subplots(1, 2, figsize=(12, 5))\n\n# Plot line chart\naxs[0].plot(days, steps_walked, \"o-g\")\naxs[0].plot(days, steps_last_week, \"v--m\")\naxs[0].set_title(\"Line graph\")\naxs[0].set_xlabel(\"Days of the week\")\naxs[0].grid(True)\n\n# Plot bar chart\nbar_width = 0.4\nx_range_current = [idx - bar_width\/2 for idx in range(7)]\nx_range_previous = [idx + bar_width\/2 for idx in range(7)]\n\naxs[1].bar(x_range_current, steps_walked, width=bar_width, color=\"g\")\naxs[1].bar(x_range_previous, steps_last_week, width=bar_width, color=\"m\")\naxs[1].set_title(\"Bar chart\")\naxs[1].set_xlabel(\"Days of the week\")\naxs[1].grid(True)\naxs[1].set_xticks(range(7))\naxs[1].set_xticklabels(days)\n\n# Figure-wide components\nfig.suptitle(\"Step count | This week and last week\")\nfig.legend([\"This week\", \"Last week\"])\nfig.supylabel(\"Steps walked\")\n\nfig.savefig(\"steps_comparison.png\")\n\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;ve also added a call to the <code>Figure<\/code> method <code>savefig()<\/code>, which allows you to save the figure to file. The final output from this example is the following figure:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"308\" data-attachment-id=\"1739\" data-permalink=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/ch10_steps_subplots_8\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_8.png?fit=2400%2C1000&amp;ssl=1\" data-orig-size=\"2400,1000\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ch10_steps_subplots_8\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_8.png?fit=739%2C308&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_8.png?resize=739%2C308&#038;ssl=1\" alt=\"Plotting using subplots and the OOP approach in Matplotlib\" class=\"wp-image-1739\" srcset=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_8.png?resize=1024%2C427&amp;ssl=1 1024w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_8.png?resize=300%2C125&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_8.png?resize=768%2C320&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_8.png?resize=1536%2C640&amp;ssl=1 1536w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_8.png?resize=2048%2C853&amp;ssl=1 2048w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_8.png?resize=1200%2C500&amp;ssl=1 1200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_8.png?resize=1088%2C453&amp;ssl=1 1088w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_8.png?w=1478&amp;ssl=1 1478w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_subplots_8.png?w=2217&amp;ssl=1 2217w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">You&#8217;ll also find a PNG file named <code>steps_comparison.png<\/code> in your Project folder.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the Snippets section, there are additional examples of more complex subplot grids.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"comparison-between-the-two-matplotlib-interfaces\">Comparison Between The Two Matplotlib Interfaces<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;ve learned about the two ways of creating figures in Matplotlib. In the simpler option, you use functions within the submodule <code>matplotlib.pyplot<\/code> directly. You use calls such as <code>plt.plot()<\/code> and <code>plt.title()<\/code>. Matplotlib will automatically create and manage the objects for you. This option is useful as it&#8217;s quicker and easier to use. However, it gives you less flexibility to customise your figures.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the alternative way, you create <code>Figure<\/code> and <code>Axes<\/code> objects using <code>plt.subplots()<\/code> and then you call methods of those two classes. Dealing with instances of <code>Figure<\/code> and <code>Axes<\/code> directly gives you more control over your figure.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Which option should you use? Both interfaces are available to you when using Matplotlib, and therefore, you can use whichever one you&#8217;re more comfortable with. The more direct approach is easier to start with. However, once you understand the anatomy of a figure, in particular how you have a <code>Figure<\/code> object that contains one or more <code>Axes<\/code> objects, you may prefer to use the object-oriented version in most cases.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Matplotlib documentation recommends using the simpler version when creating quick graphs in interactive environments like the Console or Jupyter and when exploring your data. However, for all other purposes, the object-oriented interface may be preferable.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can find all the functions available to use in the direct approach on the <a href=\"https:\/\/matplotlib.org\/stable\/api\/_as_gen\/matplotlib.pyplot.html#module-matplotlib.pyplot\"><code>pyplot<\/code> documentation page<\/a>. If you&#8217;re using the object-oriented approach, you can find all the methods you need in the <a href=\"https:\/\/matplotlib.org\/stable\/api\/figure_api.html?highlight=figure#matplotlib.figure.Figure\"><code>Figure<\/code> class documentation page<\/a> and in the <a href=\"https:\/\/matplotlib.org\/stable\/api\/axes_api.html?highlight=axes#matplotlib.axes.Axes\"><code>Axes<\/code> class documentation page<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As you become more proficient with Matplotlib, and if you require more complex plots, you can also dive further into other classes defined in Matplotlib. However, for the time being, the functions available in <code>pyplot<\/code> and the methods of the <code>Figure<\/code> and <code>Axes<\/code> classes are more than enough!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The rest of this Chapter will give a brief overview of some other plots you can create with Matplotlib.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"displaying-images-using-matplotlib\">Displaying Images Using Matplotlib<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">An image is an array of numbers. Therefore, it is possible to deal with images using the same tools as when dealing with any array of numbers. In this section, you&#8217;ll see how you can perform basic image manipulation using Matplotlib and NumPy.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There are other libraries in Python to deal with images and, in particular, to deal with image processing, machine vision, and related fields. We will not cover any of these in this book. This section aims is to give you a basic introduction to dealing with images from within a computer program.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;ll use a PNG image in this example, but you can use images of most standard formats in the same manner. You can download the image you&#8217;ll use in this example from <strong><a href=\"https:\/\/github.com\/codetoday-london\/the-python-coding-book\/archive\/refs\/heads\/main.zip\">The Python Coding Book File Repository<\/a><\/strong>. You\u2019ll need the file named <code>balconies.png<\/code>, and <em>you should place the file in your Project folder<\/em>.<\/p>\n\n\n\n<div class=\"wp-block-coblocks-alert is-style-info\" style=\"background-color:;color:\"><p class=\"wp-block-coblocks-alert__title\"><strong>Download <a href=\"https:\/\/github.com\/codetoday-london\/the-python-coding-book\/archive\/refs\/heads\/main.zip\">The Python Coding Book File Repository<\/a><br><\/strong><\/p><p class=\"wp-block-coblocks-alert__text\">Through the link above, you can download the folder you need directly to your computer. I would recommend this option which is the most straightforward. But if you prefer, you can also <a href=\"https:\/\/github.com\/codetoday-london\/the-python-coding-book\">access the repository through Github<\/a>.<\/p><\/div>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">You can read in the image using <code>plt.imread()<\/code> and explore what data type the function returns:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import matplotlib.pyplot as plt\n\nimg = plt.imread(\"balconies.png\")\n\nprint(type(img))\nprint(img.shape)\nprint(img[100, 100, 0])<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The output from the three calls to <code>print()<\/code> is the following:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;class 'numpy.ndarray'>\n(453, 456, 4)\n0.7294118<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>type(img)<\/code> shows that <code>imread()<\/code> returns a NumPy <code>ndarray<\/code>. The shape of the array is <code>(453, 456, 4)<\/code>. The first two values in this tuple show that this image is <code>453x456<\/code> pixels large. The <code>4<\/code> in the final position in the tuple shows that there are four <em>layers<\/em> of numbers. You&#8217;ll learn about what these four layers are soon.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The final call to <code>print()<\/code> returns the value of one of the cells in the array. In this case you&#8217;re printing the value of the pixel at <code>(100, 100)<\/code> in the first layer out of the four layers present. The values in this image range from <code>0<\/code> to <code>1<\/code>. In some images, the image values can range from <code>0<\/code> to <code>255<\/code>, too.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"representing-an-image-using-an-array\">Representing an image using an array<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">An image can be represented using one of three types of arrays:<\/p>\n\n\n\n<div class=\"wp-block-jetpack-markdown\"><ol>\n<li>an <code>(MxN)<\/code> array represents a <strong>grayscale image<\/strong> that\u2019s <code>MxN<\/code> pixels large.<\/li>\n<\/ol>\n<ul>\n<li>This array has just one layer, which represents the grayscale values of the image. Each value in the array represents the grayscale value of that pixel.<\/li>\n<li>Images typically either have values ranging from <code>0<\/code> to <code>1<\/code> or from <code>0<\/code> to <code>255<\/code>.<\/li>\n<li>On the <code>0...1<\/code> scale, <code>0<\/code> represents black, and <code>1<\/code> represents white. On the <code>0...255<\/code> scale, <code>0<\/code> represents black, and <code>255<\/code> represents white.<\/li>\n<\/ul>\n<ol start=\"2\">\n<li>an <code>(MxNx3)<\/code> array represents a <strong>colour image<\/strong> that\u2019s <code>MxN<\/code> pixels large.<\/li>\n<\/ol>\n<ul>\n<li>This array has three layers, each layer having <code>MxN<\/code> pixels.<\/li>\n<li>The first layer represents the amount of red in the image, the second layer represents green, and the third layer represents the level of blue. This is the <strong><a href=\"https:\/\/en.wikipedia.org\/wiki\/RGB_color_model\">RGB colour model<\/a><\/strong> of images.<\/li>\n<li>This means that each pixel of an image can represent over 16 million different colours (256 x 256 x 256).<\/li>\n<\/ul>\n<ol start=\"3\">\n<li>an <code>(MxNx4)<\/code> array represents a <strong>colour image with transparency<\/strong>. The image is <code>MxN<\/code> pixels large.<\/li>\n<\/ol>\n<ul>\n<li>The array has four layers, each layer having <code>MxN<\/code> pixels.<\/li>\n<li>The first three layers are the RGB layers as described above.<\/li>\n<li>The fourth layer represents the alpha value. This indicates what level of transparency the pixel has, ranging from fully transparent to fully opaque. This is the <strong><a href=\"https:\/\/en.wikipedia.org\/wiki\/RGBA_color_model\">RGBA colour model<\/a><\/strong> of images.<\/li>\n<\/ul>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">What&#8217;s special about the number 256? Eight computer bits are used for each pixel for each colour. A single bit can take one of two values, either <code>0<\/code> or <code>1<\/code>. Therefore, eight bits can represent <span class=\"katex-eq\" data-katex-display=\"false\">2^8<\/span> values, which is equal to 256.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, you can ignore the alpha channel. You can discard the information in the fourth layer:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import matplotlib.pyplot as plt\n\nimg = plt.imread(\"balconies.png\")\nimg = img[:, :, :3]\n\nprint(img.shape)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;re keeping all the pixels in the layers <code>0, 1, 2<\/code> since the slice <code>:3<\/code> which you use for the third dimension of the array represents the indices from <code>0<\/code> up to but excluding <code>3<\/code>. The output confirms that the shape of the array <code>img<\/code> is now <code>(453, 456, 3)<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"showing-the-images-and-the-separate-colour-channels\">Showing the images and the separate colour channels<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can use Matplotlib to display the image directly in a Matplotlib figure:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"6-7\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import matplotlib.pyplot as plt\n\nimg = plt.imread(\"balconies.png\")\nimg = img[:, :, :3]\n\nplt.imshow(img)\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You use the function <code>imshow()<\/code> in <code>matplotlib.pyplot<\/code>. This gives the following figure:<\/p>\n\n\n<div class=\"wp-block-image is-resized\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"554\" data-attachment-id=\"1748\" data-permalink=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/ch10_image_balcony_1\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_1.png?fit=1280%2C960&amp;ssl=1\" data-orig-size=\"1280,960\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ch10_image_balcony_1\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_1.png?fit=739%2C554&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_1.png?resize=739%2C554&#038;ssl=1\" alt=\"Image used with plt.imread() in Matplotlib in Python\" class=\"wp-image-1748\" srcset=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_1.png?resize=1024%2C768&amp;ssl=1 1024w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_1.png?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_1.png?resize=768%2C576&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_1.png?resize=1200%2C900&amp;ssl=1 1200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_1.png?resize=800%2C600&amp;ssl=1 800w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_1.png?resize=400%2C300&amp;ssl=1 400w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_1.png?resize=200%2C150&amp;ssl=1 200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_1.png?resize=1088%2C816&amp;ssl=1 1088w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_1.png?w=1280&amp;ssl=1 1280w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">You can now create a series of subplots to show the red, green, and blue components of this image separately. You&#8217;ll shift to using the object-oriented way of creating figures:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"6-9\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import matplotlib.pyplot as plt\n\nimg = plt.imread(\"balconies.png\")\nimg = img[:, :, :3]\n\nfig, axs = plt.subplots(1, 3, figsize=(12, 4))\naxs[0].imshow(img[:, :, 0])\naxs[1].imshow(img[:, :, 1])\naxs[2].imshow(img[:, :, 2])\n\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You learned earlier that <code>axs<\/code> is a NumPy <code>ndarray<\/code> containing <code>Axes<\/code> objects. Therefore, <code>axs[0]<\/code> is the <code>Axes<\/code> object for the first subplot. And the same applies to the other two subplots.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The image you get is not quite what you might have expected:<\/p>\n\n\n\n<figure class=\"wp-block-image alignwide size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"246\" data-attachment-id=\"1750\" data-permalink=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/ch10_image_balcony_2\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_2.png?fit=2400%2C800&amp;ssl=1\" data-orig-size=\"2400,800\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ch10_image_balcony_2\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_2.png?fit=739%2C246&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_2.png?resize=739%2C246&#038;ssl=1\" alt=\"Separating and RGB image into the three colour channels in Matplotlib\" class=\"wp-image-1750\" srcset=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_2.png?resize=1024%2C341&amp;ssl=1 1024w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_2.png?resize=300%2C100&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_2.png?resize=768%2C256&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_2.png?resize=1536%2C512&amp;ssl=1 1536w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_2.png?resize=2048%2C683&amp;ssl=1 2048w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_2.png?resize=1200%2C400&amp;ssl=1 1200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_2.png?resize=1088%2C363&amp;ssl=1 1088w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_2.png?w=1478&amp;ssl=1 1478w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_2.png?w=2217&amp;ssl=1 2217w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The three images are not the same. You can see this when comparing the first one (the red channel) compared to the other two.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>imshow()<\/code> uses a default colour map to represent the three images as each image only has one layer now. Therefore, these are grayscale images. A colour map is a mapping between colours and values. Earlier, when you displayed the <code>MxNx4<\/code> array <code>img<\/code>, Matplotlib recognised this as a colour image and therefore displayed it as a colour image.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can change the colour map for each subplot to grayscale:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"7-14\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import matplotlib.pyplot as plt\n\nimg = plt.imread(\"balconies.png\")\nimg = img[:, :, :3]\n\nfig, axs = plt.subplots(1, 3, figsize=(12, 4))\naxs[0].imshow(img[:, :, 0], cmap=\"gray\")\naxs[0].set_title(\"Red channel\")\n\naxs[1].imshow(img[:, :, 1], cmap=\"gray\")\naxs[1].set_title(\"Green channel\")\n\naxs[2].imshow(img[:, :, 2], cmap=\"gray\")\naxs[2].set_title(\"Blue channel\")\n\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You use the keyword parameter <code>cmap<\/code> to switch to a grayscale colour map. You&#8217;ve also added titles to each subplot to identify each colour channel in the image. The output now shows the three channels displayed in grayscale:<\/p>\n\n\n\n<figure class=\"wp-block-image alignwide size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"246\" data-attachment-id=\"1752\" data-permalink=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/ch10_image_balcony_3\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_3.png?fit=2400%2C800&amp;ssl=1\" data-orig-size=\"2400,800\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ch10_image_balcony_3\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_3.png?fit=739%2C246&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_3.png?resize=739%2C246&#038;ssl=1\" alt=\"Red, green, and blue colour channels displayed using subplots in Matplotlib\" class=\"wp-image-1752\" srcset=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_3.png?resize=1024%2C341&amp;ssl=1 1024w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_3.png?resize=300%2C100&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_3.png?resize=768%2C256&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_3.png?resize=1536%2C512&amp;ssl=1 1536w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_3.png?resize=2048%2C683&amp;ssl=1 2048w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_3.png?resize=1200%2C400&amp;ssl=1 1200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_3.png?resize=1088%2C363&amp;ssl=1 1088w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_3.png?w=1478&amp;ssl=1 1478w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_image_balcony_3.png?w=2217&amp;ssl=1 2217w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"understanding-the-three-colour-channels\">Understanding the three colour channels<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s make sense of what you see in these images. When you look at the original colour image, you see that the balcony at the centre of the screen has a light blue colour and the one on the right has a dark green colour. Let&#8217;s focus on the central balcony first. Light blue consists of high values for blue and green and a low value in the red channel.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When you look at the three separate channels, the middle balcony appears dark in the red-channel image. This shows that there isn&#8217;t a lot of red in those pixels. The green and blue channels show the middle balcony in a lighter colour, showing that there&#8217;s a lot of green and blue in the pixels that make up the middle balcony. The balcony appears nearly white in the blue channel because these pixels are almost at their maximum level in the blue channel.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Still looking at the middle balcony, if you look at the windows, you&#8217;ll notice that these are shown in a bright shade in all three colour channels. In the colour image, the reflection from these windows makes them look white, and white is represented by maximum values for all three channels: red, green, and blue.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The balcony on the right has a dark green colour. In the three separate subplots, you can see that the balcony appears almost black in the red channel. There&#8217;s very little red in these pixels. This balcony appears brightest in the green channel. However, as the balcony is dark green, it only appears as an intermediate shade of grey in the green channel.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When dealing with data visualisation in Python, you may have images as part of your data set. You can now start exploring any image using Matplotlib.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"plotting-in-3d\">Plotting in 3D<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Another common requirement in data visualisation in Python is to display 3d plots. You can plot data in 3D using Matplotlib.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the Chapter about <a href=\"https:\/\/thepythoncodingbook.com\/using-numpy-numerical-python-for-quantitative-applications\/\">using NumPy<\/a>, the final section dealt with representing equations in Python. You&#8217;ll use the same example in this section, but you&#8217;ll convert the equation you used in that section from 1D into 2D.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>Note:<\/em><\/strong> <em>I&#8217;m avoiding using the term function to refer to mathematical functions to avoid confusion with a Python function. Although there are similarities between a mathematical function and a Python function, there are also significant differences. You&#8217;ll read more about this topic in the Chapter on Functional Programming.<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the Chapter about NumPy you plotted the following equation:<\/p>\n\n\n\n<div class=\"wp-block-katex-display-block katex-eq\" data-katex-display=\"true\"><pre>y=\\frac{\\sin(x-a)}{x}<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">You can simplify this by making <code>a = 0<\/code>:<\/p>\n\n\n\n<div class=\"wp-block-katex-display-block katex-eq\" data-katex-display=\"true\"><pre>y=\\frac{\\sin(x)}{x}<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">This is known as the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Sinc_function\">sinc function<\/a> in mathematics. You can consider a 2D version of this equation:<\/p>\n\n\n\n<div class=\"wp-block-katex-display-block katex-eq\" data-katex-display=\"true\"><pre>z=\\frac{\\sin(x)}{x}\\frac{\\sin(y)}{y}<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">You can create the arrays that represent the <em>x<\/em>-axis and <em>y<\/em>-axis:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import numpy as np\n\nx = np.linspace(-10, 10, 1000)\ny = np.linspace(-10, 10, 1000)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">1D arrays such as <code>x<\/code> and <code>y<\/code> were sufficient to create 1D equations. However, <em>z<\/em> depends on two variables. Therefore, the data structure that will hold the values of <em>z<\/em> needs to be a 2D array.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"using-meshgrid\">Using <code>meshgrid()<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can convert the 1D arrays <code>x<\/code> and <code>y<\/code> into their 2D counterparts using the function <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.meshgrid.html\"><code>np.meshgrid()<\/code><\/a>:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import matplotlib.pyplot as plt\nimport numpy as np\n\nx = np.linspace(-10, 10, 1000)\ny = np.linspace(-10, 10, 1000)\n\nX, Y = np.meshgrid(x, y)\n\n# Temporary code: Visualising X and Y returned by meshgrid()\nfig, axs = plt.subplots(1, 2)\naxs[0].imshow(X, cmap=\"gray\")\naxs[0].set_title(\"X\")\naxs[1].imshow(Y, cmap=\"gray\")\naxs[1].set_title(\"Y\")\n\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>meshgrid()<\/code> function returns two 2D <code>ndarrays<\/code> which you&#8217;re naming <code>X<\/code> and <code>Y<\/code>. They extend the 1D arrays <code>x<\/code> and <code>y<\/code> into two dimensions. You show these arrays as images using <code>imshow()<\/code> to get the following figure:<\/p>\n\n\n<div class=\"wp-block-image is-resized\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"554\" data-attachment-id=\"1754\" data-permalink=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/ch10_3d_1\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_1.png?fit=1280%2C960&amp;ssl=1\" data-orig-size=\"1280,960\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ch10_3d_1\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_1.png?fit=739%2C554&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_1.png?resize=739%2C554&#038;ssl=1\" alt=\"Outputs from np.meshgrid() used for 2D plots using Matplotlib in Python\" class=\"wp-image-1754\" srcset=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_1.png?resize=1024%2C768&amp;ssl=1 1024w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_1.png?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_1.png?resize=768%2C576&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_1.png?resize=1200%2C900&amp;ssl=1 1200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_1.png?resize=800%2C600&amp;ssl=1 800w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_1.png?resize=400%2C300&amp;ssl=1 400w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_1.png?resize=200%2C150&amp;ssl=1 200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_1.png?resize=1088%2C816&amp;ssl=1 1088w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_1.png?w=1280&amp;ssl=1 1280w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">The values of <code>X<\/code> and <code>Y<\/code> range from <code>-10<\/code> to <code>10<\/code>. Black represents <code>-10<\/code>, and white represents <code>10<\/code> in these plots. You can see from the plots that <code>X<\/code> varies from <code>-10<\/code> to <code>10<\/code> from left to right, and <code>Y<\/code> varies across the same range from top to bottom.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can now use <code>X<\/code> and <code>Y<\/code> to create <code>Z<\/code> using the equation above:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import matplotlib.pyplot as plt\nimport numpy as np\n\nx = np.linspace(-10, 10, 1000)\ny = np.linspace(-10, 10, 1000)\n\nX, Y = np.meshgrid(x, y)\n\nZ = (np.sin(X) \/ X) * (np.sin(Y) \/ Y)\n\nplt.imshow(Z)\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You create <code>Z<\/code> from the 2D arrays <code>X<\/code> and <code>Y<\/code>, and therefore, <code>Z<\/code> is also a 2D array. The figure created when you use <code>imshow()<\/code> is the following:<\/p>\n\n\n<div class=\"wp-block-image is-resized\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"554\" data-attachment-id=\"1756\" data-permalink=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/ch10_3d_2\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_2.png?fit=1280%2C960&amp;ssl=1\" data-orig-size=\"1280,960\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ch10_3d_2\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_2.png?fit=739%2C554&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_2.png?resize=739%2C554&#038;ssl=1\" alt=\"Using imshow() in Matplotlib to visualise 2D functions\" class=\"wp-image-1756\" srcset=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_2.png?resize=1024%2C768&amp;ssl=1 1024w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_2.png?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_2.png?resize=768%2C576&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_2.png?resize=1200%2C900&amp;ssl=1 1200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_2.png?resize=800%2C600&amp;ssl=1 800w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_2.png?resize=400%2C300&amp;ssl=1 400w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_2.png?resize=200%2C150&amp;ssl=1 200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_2.png?resize=1088%2C816&amp;ssl=1 1088w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_2.png?w=1280&amp;ssl=1 1280w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">The colour in the 2D image represents the third dimension. In this colour map, the yellow colour represents the highest values, and the dark purple colours are the lowest values in the array <code>Z<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"plotting-in-3d\">Plotting in 3D<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">However, another way of visualising the 2D equation is by using a 3D plot. Earlier in this Chapter, you created <code>Axes<\/code> objects that were 2D. You can also create 3D axes:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"11-13\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import matplotlib.pyplot as plt\nimport numpy as np\n\nx = np.linspace(-10, 10, 1000)\ny = np.linspace(-10, 10, 1000)\n\nX, Y = np.meshgrid(x, y)\n\nZ = (np.sin(X) \/ X) * (np.sin(Y) \/ Y)\n\nfig, ax = plt.subplots(subplot_kw={'projection': \"3d\"})\n\nax.plot_surface(X, Y, Z, cmap=\"viridis\")\n\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>subplot_kw<\/code> parameter in <code>plt.subplots()<\/code> allows you to pass keyword parameters into the creation of subplots. In this case, you&#8217;re choosing the projection of the plot to be 3D. This creates an <code>Axes3D<\/code> object. One of its methods is <code>plot_surface()<\/code> which plots the array <code>Z<\/code> as a 3D surface. You use the <code>\"viridis\"<\/code> colour map:<\/p>\n\n\n\n<figure class=\"wp-block-image alignwide size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"554\" data-attachment-id=\"1758\" data-permalink=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/ch10_3d_3\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_3.png?fit=1280%2C960&amp;ssl=1\" data-orig-size=\"1280,960\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ch10_3d_3\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_3.png?fit=739%2C554&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_3.png?resize=739%2C554&#038;ssl=1\" alt=\"3D surface plot using plot_surface() in Matplotlib\" class=\"wp-image-1758\" srcset=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_3.png?resize=1024%2C768&amp;ssl=1 1024w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_3.png?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_3.png?resize=768%2C576&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_3.png?resize=1200%2C900&amp;ssl=1 1200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_3.png?resize=800%2C600&amp;ssl=1 800w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_3.png?resize=400%2C300&amp;ssl=1 400w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_3.png?resize=200%2C150&amp;ssl=1 200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_3.png?resize=1088%2C816&amp;ssl=1 1088w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_3d_3.png?w=1280&amp;ssl=1 1280w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Although the colour in the colour map still represents the third dimension as before, the plot is now also displayed in 3D, making it easier to visualise, understand, and study.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"creating-animations-using-matplotlib\">Creating Animations Using Matplotlib<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The final example in this Chapter will extend the static plots into dynamic animations. You&#8217;ll use the following equation for this exercise:<\/p>\n\n\n\n<div class=\"wp-block-katex-display-block katex-eq\" data-katex-display=\"true\"><pre>z=\\sin(x^2+y^2)<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;ll extend this by adding a shift to the sine function:<\/p>\n\n\n\n<div class=\"wp-block-katex-display-block katex-eq\" data-katex-display=\"true\"><pre>z=\\sin(x^2+y^2-a)<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s see what this equation looks like:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import numpy as np\nimport matplotlib.pyplot as plt\n\nx = np.linspace(-5, 5, 1000)\ny = np.linspace(-5, 5, 1000)\n\nX, Y = np.meshgrid(x, y)\n\nfig, ax = plt.subplots()\n\na = 0\nZ = np.sin(X ** 2 + Y ** 2 - a)\n\nax.imshow(Z)\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With <code>a = 0<\/code>, this equation looks like this:<\/p>\n\n\n<div class=\"wp-block-image is-resized\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"554\" data-attachment-id=\"1759\" data-permalink=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/ch10_animation_1\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_animation_1.png?fit=1280%2C960&amp;ssl=1\" data-orig-size=\"1280,960\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ch10_animation_1\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_animation_1.png?fit=739%2C554&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_animation_1.png?resize=739%2C554&#038;ssl=1\" alt=\"Creating animations in Python using Matplotlib\" class=\"wp-image-1759\" srcset=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_animation_1.png?resize=1024%2C768&amp;ssl=1 1024w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_animation_1.png?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_animation_1.png?resize=768%2C576&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_animation_1.png?resize=1200%2C900&amp;ssl=1 1200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_animation_1.png?resize=800%2C600&amp;ssl=1 800w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_animation_1.png?resize=400%2C300&amp;ssl=1 400w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_animation_1.png?resize=200%2C150&amp;ssl=1 200w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_animation_1.png?resize=1088%2C816&amp;ssl=1 1088w, https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_animation_1.png?w=1280&amp;ssl=1 1280w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">You want to explore how this equation changes as you use different values for <code>a<\/code>. Visually, an animation would be the best solution to explore this.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can achieve this using a <code>for<\/code> loop:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"11-14\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import numpy as np\nimport matplotlib.pyplot as plt\n\nx = np.linspace(-5, 5, 1000)\ny = np.linspace(-5, 5, 1000)\n\nX, Y = np.meshgrid(x, y)\n\nfig, ax = plt.subplots()\n\nfor a in np.linspace(-np.pi, np.pi, 50):\n    Z = np.sin(X ** 2 + Y ** 2 - a)\n    ax.imshow(Z)\n    plt.pause(0.001)\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;re iterating through an <code>ndarray<\/code> you create using <code>linspace()<\/code>. This array contains <code>50<\/code> values ranging from <em>-\u03c0<\/em> to <em>\u03c0<\/em>. You assign these values to the parameter <code>a<\/code> in the <code>for<\/code> loop statement. The function <code>plt.pause()<\/code> can be used to display the plot and introduce a short delay which you can use to partially control the speed of the animation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The speed of the animation displayed when you run this code will depend on the computer you&#8217;re using and what processes you have running on your device. However, the animation will likely be rather slow. You can reduce the amount of time in the <code>pause()<\/code> function, but this will not make much difference as the bottleneck is elsewhere in the loop. Each iteration needs to work out the new value of <code>Z<\/code> and display it. This slows things down.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There are several ways you can resolve this problem. You&#8217;ll look at two of these solutions in the next two subsections.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"saving-the-images-to-file\">Saving the images to file<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">One option is to save the images to file as JPG or PNG images and then use external software to create a movie from the series of images. Yes, this option relies on external software. However, if you&#8217;re comfortable using other software that can create videos from static images, this option can be very useful.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can save the images to file as you iterate in the loop. For simplicity, I&#8217;m saving the files directly in the Project folder in the example below. If you prefer, you can create a subfolder in your Project folder, say one called <code>Images<\/code>, and then add <code>\"Images\/\"<\/code> (Mac) or <code>\"Images\\\"<\/code> (Windows) to the file name in the code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"10,14-16\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import numpy as np\nimport matplotlib.pyplot as plt\n\nx = np.linspace(-5, 5, 1000)\ny = np.linspace(-5, 5, 1000)\n\nX, Y = np.meshgrid(x, y)\nfig, ax = plt.subplots()\n\nfile_number = 0\nfor a in np.linspace(-np.pi, np.pi, 50):\n    Z = np.sin(X ** 2 + Y ** 2 - a)\n    ax.imshow(Z)\n    print(f\"Saving image {file_number + 1}\")\n    fig.savefig(f\"image_{file_number}.png\")\n    file_number += 1<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Rather than displaying the images on screen, you&#8217;re creating the figure &#8216;behind the scenes&#8217; and saving each figure to a PNG file using <code>fig.savefig()<\/code>. You increment the file number using the variable <code>file_number<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There is a more Pythonic way of incrementing the file number using Python&#8217;s built-in <code>enumerate()<\/code> function. I&#8217;ll show this option below without dwelling on how <code>enumerate()<\/code> works. You can read more about <code>enumerate()<\/code> in the Snippets section at the end of this Chapter:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"10-14\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import numpy as np\nimport matplotlib.pyplot as plt\n\nx = np.linspace(-5, 5, 1000)\ny = np.linspace(-5, 5, 1000)\n\nX, Y = np.meshgrid(x, y)\nfig, ax = plt.subplots()\n\nfor file_number, a in enumerate(np.linspace(-np.pi, np.pi, 50)):\n    Z = np.sin(X ** 2 + Y ** 2 - a)\n    ax.imshow(Z)\n    print(f\"Saving image {file_number + 1}\")\n    fig.savefig(f\"image_{file_number}.png\")<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">I&#8217;ve used Quicktime Player on a Mac and its <em>Open Image Sequence\u2026<\/em> option to create the video below. There are also several web-based, free platforms that will allow you to upload an image sequence to generate this movie file:<\/p>\n\n\n\n<figure class=\"wp-block-video aligncenter wp-block-embed is-type-video is-provider-videopress\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"VideoPress Video Player\" aria-label='VideoPress Video Player' width='739' height='554' src='https:\/\/videopress.com\/embed\/zEgBs2hw?cover=1&amp;loop=1&amp;preloadContent=metadata&amp;hd=0' frameborder='0' allowfullscreen data-resize-to-parent=\"true\" allow='clipboard-write'><\/iframe><script src='https:\/\/v0.wordpress.com\/js\/next\/videopress-iframe.js?m=1674852142'><\/script>\n<\/div><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"using-matplotlib-animation\">Using <code>matplotlib.animation<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">One of the submodules in <code>matplotlib<\/code> is the <a href=\"https:\/\/matplotlib.org\/stable\/api\/animation_api.html\"><code>animation<\/code> submodule<\/a> that provides functionality to create and customise your animations directly in your Python code. This section will briefly demonstrate one way of using this submodule:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"3,11-18\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.animation as animation\n\nx = np.linspace(-5, 5, 1000)\ny = np.linspace(-5, 5, 1000)\n\nX, Y = np.meshgrid(x, y)\nfig, ax = plt.subplots()\n\nimages = []\nfor a in np.linspace(-np.pi, np.pi, 50):\n    Z = np.sin(X ** 2 + Y ** 2 - a)\n    img = ax.imshow(Z)\n    images.append([img])\n\noutput = animation.ArtistAnimation(fig, images, interval=50, blit=True)\nplt.show()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;ve imported the submodule <code>matplotlib.animation<\/code> using the alias <code>animation<\/code>. The <code>for<\/code> loop is similar to the one you used earlier, iterating from <em>-\u03c0<\/em> to <em>\u03c0<\/em> in <code>50<\/code> steps. This time, instead of displaying the image or saving the figure to file, you append the image to a list in each iteration of the loop. Note that each item you append is itself a list with the image within it. Therefore, <code>images<\/code> is a list of lists.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You then create an <code>ArtistAnimation<\/code> object which is one of the objects that allows Matplotlib to deal with animations. The arguments you use when you create the instance of <code>ArtistAnimation<\/code> are the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>fig<\/code> refers to the <code>Figure<\/code> object to be used.<\/li>\n\n\n\n<li><code>images<\/code> is a list of lists. Each list within <code>images<\/code> contains the images to be included in a single frame of the animation. In this case, each frame only has one image within it, but you can have several images or plots combined into a single frame.<\/li>\n\n\n\n<li><code>interval<\/code> determines the delay between the frames of the animation in milliseconds.<\/li>\n\n\n\n<li><code>blit<\/code> turns on functionality that optimises the drawings to make the animation smoother.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">When you run this code, you&#8217;ll see the same animation shown earlier, but in this case, the animation runs directly in a Matplotlib figure.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;re now familiar with how to get started with data visualisation in Python using Matplotlib. This library provides a lot of functionality that allows you to customise your plots. If you plan to dive deeper into data visualisation in Python, you&#8217;ll need to bookmark the <a href=\"https:\/\/matplotlib.org\/stable\/index.html\">Matplotlib documentation pages<\/a>. The documentation also contains many examples covering several types of visualisations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>In this Chapter, you&#8217;ve learned:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>the <strong>fundamentals<\/strong> of plotting figures<\/li>\n\n\n\n<li>when and how to use the <strong>two interfaces<\/strong> in Matplotlib<\/li>\n\n\n\n<li>how to plot <strong>2D figures<\/strong>, including using <strong>subplots<\/strong><\/li>\n\n\n\n<li>how to <strong>display images<\/strong><\/li>\n\n\n\n<li>how to plot <strong>3D figures<\/strong><\/li>\n\n\n\n<li>how to <strong>create animations<\/strong><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">You can now start exploring data visualisation in Python with any of your own data sets.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-alpha-channel-opacity has-background aligncenter is-style-wide\" style=\"background-color:#6d227a;color:#6d227a\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"additional-reading\">Additional Reading<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Some useful links to the Matplotlib documentation:<\/li>\n\n\n\n<li><a href=\"https:\/\/matplotlib.org\">Documentation homepage<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/matplotlib.org\/stable\/tutorials\/introductory\/usage.html#sphx-glr-tutorials-introductory-usage-py\">Main user guide<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/matplotlib.org\/stable\/tutorials\/index.html#tutorials\">List of tutorials on matplotlib.org<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/matplotlib.org\/stable\/gallery\/index.html\">More customisation examples<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/matplotlib.org\/stable\/api\/pyplot_summary.html?highlight=pyplot\">List of functions in the <code>pyplot<\/code> interface<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/matplotlib.org\/stable\/api\/figure_api.html?highlight=figure#matplotlib.figure.Figure\">Documentation on the <code>Figure<\/code> class<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/matplotlib.org\/stable\/api\/axes_api.html?highlight=axes#the-axes-class\">Documentation on the <code>Axes<\/code> class<\/a><\/li>\n\n\n\n<li>You can read the article about how to create any image using only sine functions using the <a href=\"https:\/\/thepythoncodingbook.com\/2021\/08\/30\/2d-fourier-transform-in-python-and-fourier-synthesis-of-images\/\">2D Fourier Transform in Python<\/a> for another tutorial that uses many of the Matplotlib functionality you read about in this Chapter.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Image Credit: Malta Balconies <a href=\"https:\/\/pixabay.com\/users\/mcsmandalas-1918387\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=3575467\">Image by Alex B from Pixabay<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>[This article uses <a href=\"https:\/\/wordpress.org\/plugins\/katex\/\">KaTeX<\/a> By <a href=\"https:\/\/churchman.nl\">Thomas Churchman<\/a>]<\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-alpha-channel-opacity has-background aligncenter is-style-wide\" style=\"background-color:#6d227a;color:#6d227a\"\/>\n\n\n\n<div class=\"wp-block-buttons is-horizontal is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-7d812b4c wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button has-custom-width wp-block-button__width-50 is-style-outline is-style-outline--1\"><a class=\"wp-block-button__link has-text-color wp-element-button\" href=\"https:\/\/thepythoncodingbook.com\/more-chapters-coming-soon\/\" style=\"border-radius:3px;color:#6d227a\">Next Chapter<\/a><\/div>\n<\/div>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-buttons is-horizontal is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-7d812b4c wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button has-custom-width wp-block-button__width-75 is-style-fill\"><a class=\"wp-block-button__link has-text-color has-background wp-element-button\" href=\"https:\/\/thepythoncodingbook.com\/book-outline\/\" style=\"border-radius:3px;color:#0d363a;background-color:#fdb33b\">Browse Zeroth Edition<\/a><\/div>\n<\/div>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\"><\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<div class=\"wp-block-group alignfull\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<div class=\"wp-block-group alignfull\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<div class=\"wp-block-coblocks-hero alignfull coblocks-hero-230194844519\"><div class=\"wp-block-coblocks-hero__inner has-background hero-center-left-align has-padding has-huge-padding\" style=\"background-color:#1a6b72;min-height:500px\"><div class=\"wp-block-coblocks-hero__content-wrapper\"><div class=\"wp-block-coblocks-hero__content\" style=\"max-width:560px\">\n<h2 class=\"wp-block-heading has-text-color has-link-color wp-elements-806dd89de56256fb948e5020de497de4\" style=\"color:#fff3e6\">Become a Member of<\/h2>\n\n\n\n<h2 class=\"wp-block-heading has-text-color has-link-color wp-elements-7d78fcf199c064aa3173690e46837383\" style=\"color:#fff3e6\">The Python Coding Place<\/h2>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-57091ca0dd26b2b579f184ab1723dc89 wp-block-paragraph\" style=\"color:#fff3e6\">Video courses, live cohort-based courses, workshops, weekly videos, members&#8217; forum, and more\u2026<\/p>\n\n\n\n<div style=\"height:66px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button has-custom-width wp-block-button__width-50\"><a class=\"wp-block-button__link has-black-color has-text-color has-background wp-element-button\" href=\"https:\/\/thepythoncodingplace.com\" style=\"background-color:#fdb33b\">Become a Member<\/a><\/div>\n<\/div>\n<\/div><\/div><\/div><\/div>\n<\/div><\/div>\n<\/div><\/div>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-jetpack-markdown\"><h2>Snippets<\/h2>\n<p>Coming soon\u2026<\/p>\n<\/div>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-alpha-channel-opacity has-background aligncenter is-style-wide\" style=\"background-color:#6d227a;color:#6d227a\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><\/h2>\n\n\n\n<h4 class=\"wp-block-heading\"><\/h4>\n\n\n\n<div class=\"wp-block-jetpack-markdown\"><\/div>\n\n\n\n\n\n<hr class=\"wp-block-separator has-text-color has-alpha-channel-opacity has-background aligncenter is-style-wide\" style=\"background-color:#6d227a;color:#6d227a\"\/>\n\n\n\n\n","protected":false},"excerpt":{"rendered":"<p>More and more applications of programming using Python involve large amounts of data. Visualising those data is an essential part of understanding what the data say, as every scientist, data scientist, and anyone who works with data will confirm. Displaying data visually is important for those studying the data and also for those to whom&hellip; <a class=\"more-link\" href=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/\">Continue reading <span class=\"screen-reader-text\">10 | Basics of Data Visualisation in Python Using Matplotlib<\/span> <span class=\"meta-nav\" aria-hidden=\"true\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":192321682,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"templates\/full-width-page.php","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","_crdt_document":"","footnotes":""},"class_list":["post-1690","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>10 | Basics of Data Visualisation in Python Using Matplotlib<\/title>\n<meta name=\"description\" content=\"In this Chapter you&#039;ll learn about data visualisation in Python using Matplotlib. You&#039;ll create 2D and 3D plots, images, and animations\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"10 | Basics of Data Visualisation in Python Using Matplotlib\" \/>\n<meta property=\"og:description\" content=\"In this Chapter you&#039;ll learn about data visualisation in Python using Matplotlib. You&#039;ll create 2D and 3D plots, images, and animations\" \/>\n<meta property=\"og:url\" content=\"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/\" \/>\n<meta property=\"og:site_name\" content=\"The Python Coding Book\" \/>\n<meta property=\"article:modified_time\" content=\"2024-05-21T10:40:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_animation_1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"960\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_animation_1.png?fit=1280%2C960&ssl=1\" \/>\n<meta name=\"twitter:label1\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"96 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/thepythoncodingbook.com\\\/basics-of-data-visualisation-in-python-using-matplotlib\\\/\",\"url\":\"https:\\\/\\\/thepythoncodingbook.com\\\/basics-of-data-visualisation-in-python-using-matplotlib\\\/\",\"name\":\"10 | Basics of Data Visualisation in Python Using Matplotlib\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/thepythoncodingbook.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/thepythoncodingbook.com\\\/basics-of-data-visualisation-in-python-using-matplotlib\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/thepythoncodingbook.com\\\/basics-of-data-visualisation-in-python-using-matplotlib\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/thepythoncodingbook.com\\\/wp-content\\\/uploads\\\/2021\\\/11\\\/ch10_steps_1-1024x768.png\",\"datePublished\":\"2021-11-12T23:17:02+00:00\",\"dateModified\":\"2024-05-21T10:40:32+00:00\",\"description\":\"In this Chapter you'll learn about data visualisation in Python using Matplotlib. You'll create 2D and 3D plots, images, and animations\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/thepythoncodingbook.com\\\/basics-of-data-visualisation-in-python-using-matplotlib\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/thepythoncodingbook.com\\\/basics-of-data-visualisation-in-python-using-matplotlib\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/thepythoncodingbook.com\\\/basics-of-data-visualisation-in-python-using-matplotlib\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/thepythoncodingbook.com\\\/wp-content\\\/uploads\\\/2021\\\/11\\\/ch10_steps_1.png?fit=1280%2C960&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/thepythoncodingbook.com\\\/wp-content\\\/uploads\\\/2021\\\/11\\\/ch10_steps_1.png?fit=1280%2C960&ssl=1\",\"width\":1280,\"height\":960},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/thepythoncodingbook.com\\\/basics-of-data-visualisation-in-python-using-matplotlib\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/thepythoncodingbook.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"10 | Basics of Data Visualisation in Python Using Matplotlib\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/thepythoncodingbook.com\\\/#website\",\"url\":\"https:\\\/\\\/thepythoncodingbook.com\\\/\",\"name\":\"The Python Coding Book\",\"description\":\"The friendly, relaxed programming book\",\"publisher\":{\"@id\":\"https:\\\/\\\/thepythoncodingbook.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/thepythoncodingbook.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/thepythoncodingbook.com\\\/#organization\",\"name\":\"Codetoday\",\"url\":\"https:\\\/\\\/thepythoncodingbook.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/thepythoncodingbook.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/thepythoncodingbook.com\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/cropped-icon-only.png?fit=512%2C512&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/thepythoncodingbook.com\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/cropped-icon-only.png?fit=512%2C512&ssl=1\",\"width\":512,\"height\":512,\"caption\":\"Codetoday\"},\"image\":{\"@id\":\"https:\\\/\\\/thepythoncodingbook.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"10 | Basics of Data Visualisation in Python Using Matplotlib","description":"In this Chapter you'll learn about data visualisation in Python using Matplotlib. You'll create 2D and 3D plots, images, and animations","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:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/","og_locale":"en_GB","og_type":"article","og_title":"10 | Basics of Data Visualisation in Python Using Matplotlib","og_description":"In this Chapter you'll learn about data visualisation in Python using Matplotlib. You'll create 2D and 3D plots, images, and animations","og_url":"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/","og_site_name":"The Python Coding Book","article_modified_time":"2024-05-21T10:40:32+00:00","og_image":[{"width":1280,"height":960,"url":"https:\/\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_animation_1.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_image":"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_animation_1.png?fit=1280%2C960&ssl=1","twitter_misc":{"Estimated reading time":"96 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/","url":"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/","name":"10 | Basics of Data Visualisation in Python Using Matplotlib","isPartOf":{"@id":"https:\/\/thepythoncodingbook.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/#primaryimage"},"image":{"@id":"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/#primaryimage"},"thumbnailUrl":"https:\/\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_1-1024x768.png","datePublished":"2021-11-12T23:17:02+00:00","dateModified":"2024-05-21T10:40:32+00:00","description":"In this Chapter you'll learn about data visualisation in Python using Matplotlib. You'll create 2D and 3D plots, images, and animations","breadcrumb":{"@id":"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/"]}]},{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/#primaryimage","url":"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_1.png?fit=1280%2C960&ssl=1","contentUrl":"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/11\/ch10_steps_1.png?fit=1280%2C960&ssl=1","width":1280,"height":960},{"@type":"BreadcrumbList","@id":"https:\/\/thepythoncodingbook.com\/basics-of-data-visualisation-in-python-using-matplotlib\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/thepythoncodingbook.com\/"},{"@type":"ListItem","position":2,"name":"10 | Basics of Data Visualisation in Python Using Matplotlib"}]},{"@type":"WebSite","@id":"https:\/\/thepythoncodingbook.com\/#website","url":"https:\/\/thepythoncodingbook.com\/","name":"The Python Coding Book","description":"The friendly, relaxed programming book","publisher":{"@id":"https:\/\/thepythoncodingbook.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/thepythoncodingbook.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Organization","@id":"https:\/\/thepythoncodingbook.com\/#organization","name":"Codetoday","url":"https:\/\/thepythoncodingbook.com\/","logo":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/thepythoncodingbook.com\/#\/schema\/logo\/image\/","url":"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/04\/cropped-icon-only.png?fit=512%2C512&ssl=1","contentUrl":"https:\/\/i0.wp.com\/thepythoncodingbook.com\/wp-content\/uploads\/2021\/04\/cropped-icon-only.png?fit=512%2C512&ssl=1","width":512,"height":512,"caption":"Codetoday"},"image":{"@id":"https:\/\/thepythoncodingbook.com\/#\/schema\/logo\/image\/"}}]}},"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/Pd1Q8F-rg","_links":{"self":[{"href":"https:\/\/thepythoncodingbook.com\/wp-json\/wp\/v2\/pages\/1690","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thepythoncodingbook.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/thepythoncodingbook.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/thepythoncodingbook.com\/wp-json\/wp\/v2\/users\/192321682"}],"replies":[{"embeddable":true,"href":"https:\/\/thepythoncodingbook.com\/wp-json\/wp\/v2\/comments?post=1690"}],"version-history":[{"count":53,"href":"https:\/\/thepythoncodingbook.com\/wp-json\/wp\/v2\/pages\/1690\/revisions"}],"predecessor-version":[{"id":3578,"href":"https:\/\/thepythoncodingbook.com\/wp-json\/wp\/v2\/pages\/1690\/revisions\/3578"}],"wp:attachment":[{"href":"https:\/\/thepythoncodingbook.com\/wp-json\/wp\/v2\/media?parent=1690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}