{"id":6281,"date":"2026-10-19T10:00:27","date_gmt":"2026-10-19T10:00:27","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=6281"},"modified":"2026-10-20T05:58:55","modified_gmt":"2026-10-20T05:58:55","slug":"python-matplotlib","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/python\/python-data-visualization\/python-matplotlib\/","title":{"rendered":"Python Matplotlib"},"content":{"rendered":"<h1><strong><b>Python Matplotlib Tutorial for Beginners<\/b><\/strong><\/h1>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">A table of numbers tells you very little. Put the same numbers on a chart, and in a few seconds you can see the trend, the odd value that does not belong, and the group that is far ahead of the rest. That is why every data analyst, data scientist and machine learning engineer learns to plot early, and why Matplotlib is usually the first plotting library they meet.<\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Matplotlib is a Python library for drawing charts. It can make line plots, bar charts, histograms, pie charts, scatter plots, contour plots, 3D surfaces and more. Many other libraries, such as Seaborn and the plotting tools in pandas, are built on top of it, so what you learn here carries over.<\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">In this tutorial, we start with the setup and your first line plot, and then build up one chart type at a time: simple plots, multiple plots, subplots, bar, histogram, pie, scatter, contour, quiver, violin, 3D contour, 3D wireframe and grids. Every program is written as text that you can copy, and every one has been run to make sure it works. We also cover titles and labels, colours and markers, saving a figure, and the common mistakes.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Prerequisites<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">You need three things on your computer: Python 3, a code editor or Jupyter Notebook, and two libraries, Matplotlib and NumPy. Install the libraries from your terminal:<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">terminal<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">pip install matplotlib numpy<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The programs in this tutorial were tested on Python 3.13 with Matplotlib 3.11.2 and NumPy 2.5.3. You can check your own versions like this:<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">import matplotlib\r\nimport numpy as np\r\nprint(matplotlib.__version__, np.__version__)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">3.11.2 2.5.3<\/pre>\n<\/div>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">What is Matplotlib?<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Matplotlib is a cross platform data visualization and plotting library for Python. Cross platform means it runs on Windows, Mac and Linux. Its main module is <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">pyplot<\/code>, which gives you a simple set of commands to build a figure step by step, in a style borrowed from MATLAB. Each command changes the figure a little: it adds a line, sets a title, or labels an axis.<\/p>\n<h3 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 21px; line-height: 1.35; font-weight: bold; color: #0f2a4a; margin: 28px 0 10px;\">What is Pyplot?<\/h3>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Pyplot is the module inside Matplotlib that holds these commands. By habit, everyone imports it with the short name <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">plt<\/code>. The name <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">plt<\/code> is only an alias that saves typing. It is not the name of the package.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">import matplotlib.pyplot as plt<\/pre>\n<\/div>\n<h3 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 21px; line-height: 1.35; font-weight: bold; color: #0f2a4a; margin: 28px 0 10px;\">What is NumPy?<\/h3>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">NumPy is a Python library for fast work with large arrays of numbers. Most of the data you plot comes from NumPy arrays, so the two libraries are almost always used together. It is imported with the alias <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">np<\/code>.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">import numpy as np<\/pre>\n<\/div>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Jupyter Notebook Introduction<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Jupyter Notebook is an open source tool that lets you write code, see its output, and add notes in one document. You can find it at <a style=\"color: #0b5cad; text-decoration: underline;\" href=\"https:\/\/jupyter.org\/\" rel=\"noopener\">jupyter.org<\/a>. It is a good place to learn Matplotlib, because each plot appears right under the code that made it. You can install it with <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">pip install notebook<\/code> and start it with the command <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">jupyter notebook<\/code>. In a plain Python file, you need to call <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">plt.show()<\/code> to open the plot window. In Jupyter, the plot appears on its own.<\/p>\n<p style=\"margin: 0 0 16px;\"><img decoding=\"async\" style=\"display: block; max-width: 100%; height: auto; margin: 0 auto; border: 1px solid #d9e0ea; border-radius: 8px;\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/03\/Screenshot-647.png\" alt=\"Jupyter Notebook screen showing a Matplotlib line plot under its code cell\" \/><\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Plot a Line in Matplotlib<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The simplest plot is a line. Pass a list of x values and a list of y values to <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">plt.plot<\/code>, and then call <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">plt.show<\/code>. Here is a working example, with labels added:<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">import matplotlib.pyplot as plt\r\nimport numpy as np\r\n\r\nplt.plot([1, 2, 3, 4], [10, 20, 25, 30])\r\nplt.xlabel(\"Day\")\r\nplt.ylabel(\"Units sold\")\r\nplt.title(\"Simple line plot\")\r\nplt.show()<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">You will see a straight rising line through four points, with Day on the bottom and Units sold on the side. If you give <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">plt.plot<\/code> only one list, Matplotlib uses the positions 0, 1, 2 and so on for the x values.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Simple Plot: A Sine Wave<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">To plot a smooth curve, create many x values with NumPy, work out y for each one, and plot the pair. The function <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">np.arange<\/code> makes values from a start to a stop in equal steps, and <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">np.sin<\/code> gives the sine of each value.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">import matplotlib.pyplot as plt\r\nimport numpy as np\r\n\r\nx = np.arange(0, 3 * np.pi, 0.1)\r\ny = np.sin(x)\r\nplt.plot(x, y)\r\nplt.title(\"Sine wave\")\r\nplt.show()<\/pre>\n<\/div>\n<p style=\"margin: 0 0 16px;\"><img decoding=\"async\" style=\"display: block; max-width: 100%; height: auto; margin: 0 auto; border: 1px solid #d9e0ea; border-radius: 8px;\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/03\/Screenshot-649.png\" alt=\"Python program for the simple sine wave plot\" \/><\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The result is a smooth wave that rises to 1, falls to minus 1, and rises again, about one and a half cycles.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Multi Plot: Several Lines in One Figure<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">To compare two or more series, call <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">plt.plot<\/code> more than once before <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">plt.show<\/code>. Give each line a label and call <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">plt.legend<\/code>, so the reader knows which is which.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">import matplotlib.pyplot as plt\r\nimport numpy as np\r\n\r\nx = np.arange(0, 3 * np.pi, 0.1)\r\nplt.plot(x, np.sin(x), label=\"sin\")\r\nplt.plot(x, np.cos(x), label=\"cos\")\r\nplt.legend()\r\nplt.title(\"Sine and cosine\")\r\nplt.show()<\/pre>\n<\/div>\n<p style=\"margin: 0 0 16px;\"><img decoding=\"async\" style=\"display: block; max-width: 100%; height: auto; margin: 0 auto; border: 1px solid #d9e0ea; border-radius: 8px;\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/03\/Screenshot-651.png\" alt=\"Python program for the multi plot with sine and cosine lines\" \/><\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Matplotlib gives each line its own colour automatically. The cosine wave is the sine wave shifted to the left.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Subplot: Several Graphs in One Window<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">A subplot divides the window into a grid and places one plot in each cell. The call <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">plt.subplot(rows, columns, position)<\/code> picks a cell. The short form 211 means 2 rows, 1 column, position 1. So 211 is the top plot and 212 is the bottom one. The <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">facecolor<\/code> argument sets the background, and the value must be written with straight quotes.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">import matplotlib.pyplot as plt\r\nimport numpy as np\r\n\r\nx = np.arange(0, 3 * np.pi, 0.1)\r\nplt.subplot(211)\r\nplt.plot(x, np.sin(x))\r\nplt.title(\"Top: sine\")\r\nplt.subplot(212, facecolor=\"y\")\r\nplt.plot(x, np.cos(x))\r\nplt.title(\"Bottom: cosine\")\r\nplt.tight_layout()\r\nplt.show()<\/pre>\n<\/div>\n<p style=\"margin: 0 0 16px;\"><img decoding=\"async\" style=\"display: block; max-width: 100%; height: auto; margin: 0 auto; border: 1px solid #d9e0ea; border-radius: 8px;\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/03\/Screenshot-653.png\" alt=\"Subplot output with a sine plot on top and a cosine plot on a yellow background below\" \/><\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The top plot has a white background, and the bottom plot has a yellow one. The call <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">plt.tight_layout<\/code> stops the titles from overlapping.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Bar Plot<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">A bar plot compares values across categories, such as sales by region or students by course. The height of each bar shows the value. In this example, <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">fig.add_axes<\/code> takes four numbers, the left, bottom, width and height of the plot area, as a fraction of the figure.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">import matplotlib.pyplot as plt\r\nimport numpy as np\r\n\r\nfig = plt.figure()\r\nax = fig.add_axes([0.1, 0.1, 0.8, 0.8])\r\nlanguages = [\"Python\", \"Java\", \"C\", \"SQL\"]\r\nstudents = [25, 18, 12, 20]\r\nax.bar(languages, students)\r\nax.set_title(\"Students per language\")\r\nax.set_ylabel(\"Students\")\r\nplt.show()<\/pre>\n<\/div>\n<p style=\"margin: 0 0 16px;\"><img decoding=\"async\" style=\"display: block; max-width: 100%; height: auto; margin: 0 auto; border: 1px solid #d9e0ea; border-radius: 8px;\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/03\/Screenshot-655.png\" alt=\"Python program for the bar plot of students per language\" \/><\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">You will see four bars, with Python the tallest at 25. Use <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">ax.barh<\/code> instead of <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">ax.bar<\/code> for horizontal bars, which is better when the category names are long.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Histogram<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">A histogram shows how numeric data is spread. It groups the values into ranges called bins and counts how many fall into each one. A histogram looks like a bar graph, but a bar chart compares separate categories, while a histogram shows the shape of one continuous variable, so the bars touch.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">import matplotlib.pyplot as plt\r\nimport numpy as np\r\n\r\nmarks = np.array([22, 87, 5, 43, 56, 73, 55, 54, 11, 20, 51, 5, 79, 31, 27])\r\nfig, ax = plt.subplots()\r\nax.hist(marks, bins=[0, 25, 50, 75, 100], edgecolor=\"black\")\r\nax.set(title=\"Marks distribution\", xlabel=\"Marks\", ylabel=\"Students\")\r\nplt.show()<\/pre>\n<\/div>\n<p style=\"margin: 0 0 16px;\"><img decoding=\"async\" style=\"display: block; max-width: 100%; height: auto; margin: 0 auto; border: 1px solid #d9e0ea; border-radius: 8px;\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/03\/Screenshot-657.png\" alt=\"Python program for the histogram of student marks\" \/><\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">There are four bins, 0 to 25, 25 to 50, 50 to 75 and 75 to 100. The <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">bins<\/code> argument controls them. Different bins can tell a different story, so try a few.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Pie Chart<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">A pie chart shows how one total is split into parts. Use it only for a small number of slices, ideally five or fewer, and when the parts add up to a whole. The slices show percentages only when you ask for them with the <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">autopct<\/code> argument.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">import matplotlib.pyplot as plt\r\nimport numpy as np\r\n\r\nfig, ax = plt.subplots()\r\nax.pie([40, 30, 20, 10], labels=[\"Python\", \"Java\", \"C\", \"SQL\"], autopct=\"%1.1f%%\")\r\nax.set_title(\"Share of students\")\r\nplt.show()<\/pre>\n<\/div>\n<p style=\"margin: 0 0 16px;\"><img decoding=\"async\" style=\"display: block; max-width: 100%; height: auto; margin: 0 auto; border: 1px solid #d9e0ea; border-radius: 8px;\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/03\/Screenshot-659.png\" alt=\"Python program for the pie chart of student share by language\" \/><\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The slices show 40.0%, 30.0%, 20.0% and 10.0%, as labelled.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Scatter Plot<\/h2>\n<p style=\"margin: 0 0 16px;\"><img decoding=\"async\" style=\"display: block; max-width: 100%; height: auto; margin: 0 auto; border: 1px solid #d9e0ea; border-radius: 8px;\" src=\"https:\/\/miro.medium.com\/max\/805\/1*aUSZsGFCMPNYCkQygs4aGQ.jpeg\" alt=\"Example scatter plot made with Matplotlib\" \/><\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">A scatter plot draws one dot for each pair of values. It shows whether two variables are related, whether the relationship goes up or down, and whether any points sit far from the rest. The example below makes made up data where more study hours give higher scores, with some random noise.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">import matplotlib.pyplot as plt\r\nimport numpy as np\r\n\r\nrng = np.random.default_rng(42)\r\nhours = rng.uniform(1, 10, 40)\r\nscore = 35 + 6 * hours + rng.normal(0, 5, 40)\r\nfig, ax = plt.subplots()\r\nax.scatter(hours, score)\r\nax.set_title(\"Study hours against score\")\r\nax.set_xlabel(\"Hours studied\")\r\nax.set_ylabel(\"Score\")\r\nplt.show()<\/pre>\n<\/div>\n<p style=\"margin: 0 0 16px;\"><img decoding=\"async\" style=\"display: block; max-width: 100%; height: auto; margin: 0 auto; border: 1px solid #d9e0ea; border-radius: 8px;\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/03\/Screenshot-661.png\" alt=\"Python program for the scatter plot of study hours against score\" \/><\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The dots rise from the lower left to the upper right, which shows a positive relationship. The call <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">plt.subplots<\/code> creates the figure and the plotting area, and <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">ax.scatter<\/code> places the dots in it.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Contour Plot<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">A contour plot shows a 3D surface on a flat page by drawing lines that join points of equal height, like the lines on a map. Here, <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">np.linspace<\/code> makes evenly spaced numbers, <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">np.meshgrid<\/code> turns two lists into a grid of points, and the height at each point is its distance from the centre.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">import matplotlib.pyplot as plt\r\nimport numpy as np\r\n\r\nxs = np.linspace(-3, 3, 100)\r\nys = np.linspace(-3, 3, 100)\r\nX, Y = np.meshgrid(xs, ys)\r\nZ = np.sqrt(X ** 2 + Y ** 2)\r\nfig, ax = plt.subplots()\r\ncs = ax.contour(X, Y, Z)\r\nfig.colorbar(cs)\r\nax.set_title(\"Contour plot\")\r\nplt.show()<\/pre>\n<\/div>\n<p style=\"margin: 0 0 16px;\"><img decoding=\"async\" style=\"display: block; max-width: 100%; height: auto; margin: 0 auto; border: 1px solid #d9e0ea; border-radius: 8px;\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/03\/Screenshot-663.png\" alt=\"Python program for the contour plot\" \/><\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">You will see circles around the centre, because the height depends only on the distance from it. The colorbar on the side tells you which colour means which height.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Quiver Plot<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">A quiver plot draws arrows. Each arrow sits at a point and shows a direction and a size, which makes it a good fit for wind, water flow or force fields. Velocity is one use, but any quantity that has a direction and a size can be shown. Here is a complete example.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">import matplotlib.pyplot as plt\r\nimport numpy as np\r\n\r\ngx, gy = np.meshgrid(np.arange(0, 5), np.arange(0, 5))\r\nu = np.cos(gx)\r\nv = np.sin(gy)\r\nfig, ax = plt.subplots()\r\nax.quiver(gx, gy, u, v)\r\nax.set_title(\"Quiver plot\")\r\nplt.show()<\/pre>\n<\/div>\n<p style=\"margin: 0 0 16px;\"><img decoding=\"async\" style=\"display: block; max-width: 100%; height: auto; margin: 0 auto; border: 1px solid #d9e0ea; border-radius: 8px;\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/03\/Screenshot-671.png\" alt=\"Python program for the quiver plot\" \/><\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The first two arguments give the position of each arrow, and <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">u<\/code> and <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">v<\/code> give its horizontal and vertical size. You will see a 5 by 5 grid of small arrows that turn as you move across it.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Violin Plot<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">A violin plot shows how data is spread, and it is similar to a box plot. The difference is that it also draws the shape of the density, so you can see where the values are packed together. It is more detailed than a plain box plot.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">import matplotlib.pyplot as plt\r\nimport numpy as np\r\n\r\nrng = np.random.default_rng(42)\r\ndata = [rng.normal(50, s, 200) for s in (5, 10, 15)]\r\nfig, ax = plt.subplots()\r\nax.violinplot(data, showmedians=True)\r\nax.set_xticks([1, 2, 3])\r\nax.set_xticklabels([\"Batch A\", \"Batch B\", \"Batch C\"])\r\nax.set_title(\"Violin plot\")\r\nplt.show()<\/pre>\n<\/div>\n<p style=\"margin: 0 0 16px;\"><img decoding=\"async\" style=\"display: block; max-width: 100%; height: auto; margin: 0 auto; border: 1px solid #d9e0ea; border-radius: 8px;\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/03\/Screenshot-673.png\" alt=\"Python program for the violin plot of three batches\" \/><\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">You will see three shapes centred near 50. Batch A is narrow, because its marks are close together, and Batch C is wide, because its marks vary more.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">3D Contour Plot<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">For 3D plots, create the axes with <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">projection<\/code> set to <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">\"3d\"<\/code>. The method <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">contour3D<\/code> then draws the contour lines in three dimensions, with height shown on the vertical axis.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">import matplotlib.pyplot as plt\r\nimport numpy as np\r\n\r\nxs = np.linspace(-5, 5, 60)\r\nys = np.linspace(-5, 5, 60)\r\nX, Y = np.meshgrid(xs, ys)\r\nZ = np.sin(np.sqrt(X ** 2 + Y ** 2))\r\nfig = plt.figure()\r\nax = fig.add_subplot(projection=\"3d\")\r\nax.contour3D(X, Y, Z, 50, cmap=\"viridis\")\r\nax.set_title(\"3D contour\")\r\nplt.show()<\/pre>\n<\/div>\n<p style=\"margin: 0 0 16px;\"><img decoding=\"async\" style=\"display: block; max-width: 100%; height: auto; margin: 0 auto; border: 1px solid #d9e0ea; border-radius: 8px;\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/03\/Screenshot-675.png\" alt=\"Python program for the 3D contour plot\" \/><\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The number 50 is the number of contour levels. You will see a ripple shape, like the rings that spread out when you drop a stone in water.<\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\"><strong>Output:<\/strong><\/p>\n<p style=\"margin: 0 0 16px;\"><img decoding=\"async\" style=\"display: block; max-width: 100%; height: auto; margin: 0 auto; border: 1px solid #d9e0ea; border-radius: 8px;\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/03\/Screenshot-604.png\" alt=\"3D contour plot output showing a ripple shaped surface\" \/><\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">3D Wireframe Plot<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">A wireframe draws a surface as a mesh of lines, so you can see the shape without a solid fill. It uses the same X, Y and Z grid as the 3D contour above, so only the last call changes. The program is shown in full so that you can run it on its own.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">import matplotlib.pyplot as plt\r\nimport numpy as np\r\n\r\nxs = np.linspace(-5, 5, 60)\r\nys = np.linspace(-5, 5, 60)\r\nX, Y = np.meshgrid(xs, ys)\r\nZ = np.sin(np.sqrt(X ** 2 + Y ** 2))\r\nax = plt.figure().add_subplot(projection=\"3d\")\r\nax.plot_wireframe(X, Y, Z, color=\"green\")\r\nax.set_title(\"3D wireframe\")\r\nplt.show()<\/pre>\n<\/div>\n<p style=\"margin: 0 0 16px;\"><img decoding=\"async\" style=\"display: block; max-width: 100%; height: auto; margin: 0 auto; border: 1px solid #d9e0ea; border-radius: 8px;\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/03\/Screenshot-678.png\" alt=\"Python program for the 3D wireframe plot\" \/><\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">You will see the same ripple, drawn as a green net.<\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\"><strong>Output:<\/strong><\/p>\n<p style=\"margin: 0 0 16px;\"><img decoding=\"async\" style=\"display: block; max-width: 100%; height: auto; margin: 0 auto; border: 1px solid #d9e0ea; border-radius: 8px;\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/03\/Screenshot-608.png\" alt=\"3D wireframe plot output showing a green mesh with a ripple shape\" \/><\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Grids<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Gridlines make it easier to read values off a plot. Turn them on with <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">grid(True)<\/code> and off with <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">grid(False)<\/code>. In recent versions of Matplotlib, the default is no grid for most plot styles.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">import matplotlib.pyplot as plt\r\nimport numpy as np\r\n\r\nx = np.arange(0, 3 * np.pi, 0.1)\r\nfig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 3))\r\nax1.plot(x, np.sin(x))\r\nax1.grid(True)\r\nax1.set_title(\"grid on\")\r\nax2.plot(x, np.sin(x))\r\nax2.grid(False)\r\nax2.set_title(\"grid off\")\r\nplt.show()<\/pre>\n<\/div>\n<p style=\"margin: 0 0 16px;\"><img decoding=\"async\" style=\"display: block; max-width: 100%; height: auto; margin: 0 auto; border: 1px solid #d9e0ea; border-radius: 8px;\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/03\/Screenshot-680.png\" alt=\"Python program for the grid on and grid off plots\" \/><\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The left plot has gridlines and the right one does not.<\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\"><strong>Output:<\/strong><\/p>\n<p style=\"margin: 0 0 16px;\"><img loading=\"lazy\" decoding=\"async\" style=\"display: block; max-width: 100%; height: auto; margin: 0 auto; border: 1px solid #d9e0ea; border-radius: 8px;\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/03\/Screenshot-612-1024x405.png\" alt=\"Two sine plots side by side, the left with gridlines and the right without\" width=\"684\" height=\"270\" \/><\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Titles, Labels, Colours and Markers<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">These options will make your charts clear enough to share.<\/p>\n<div style=\"border: 1px solid #d9e0ea; border-radius: 8px; overflow-x: auto; margin: 20px 0; -webkit-overflow-scrolling: touch;\">\n<table style=\"width: 100%; min-width: 720px; border-collapse: collapse; font-family: Arial,Helvetica,sans-serif; font-size: 16px; line-height: 1.6; color: #1f2937;\">\n<caption style=\"caption-side: top; text-align: left; padding: 14px 16px; font-weight: bold; font-size: 17px; color: #0f2a4a;\">Common styling options<\/caption>\n<thead>\n<tr>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38; width: 170px;\" scope=\"col\">What you want<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Command<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Example<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Title<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">plt.title<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">plt.title(&#8220;Sales&#8221;)<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Axis labels<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">plt.xlabel, plt.ylabel<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">plt.xlabel(&#8220;Day&#8221;)<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Line colour<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">color argument<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">color=&#8221;navy&#8221;<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Line style<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">linestyle argument<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">linestyle=&#8221;&#8211;&#8220;<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Point markers<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">marker argument<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">marker=&#8221;o&#8221;<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Legend<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">label argument, then plt.legend<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">label=&#8221;sin&#8221;<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Figure size<\/th>\n<td style=\"padding: 12px 16px; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">figsize argument<\/td>\n<td style=\"padding: 12px 16px; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">plt.figure(figsize=(8, 4))<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">import matplotlib.pyplot as plt\r\nimport numpy as np\r\n\r\nx = np.arange(0, 3 * np.pi, 0.1)\r\nplt.plot(x, np.sin(x), color=\"navy\", linestyle=\"--\", marker=\"o\", markersize=3, label=\"sin\")\r\nplt.legend()\r\nplt.xlabel(\"x\")\r\nplt.ylabel(\"sin(x)\")\r\nplt.show()<\/pre>\n<\/div>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">How to Save a Plot as an Image?<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Use <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">plt.savefig<\/code> before <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">plt.show<\/code>. After the window is shown and closed, the figure is cleared and you would save an empty image. The <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">dpi<\/code> argument controls the sharpness, and <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">bbox_inches<\/code> set to <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">\"tight\"<\/code> trims the empty border.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">import matplotlib.pyplot as plt\r\nimport numpy as np\r\n\r\nx = np.arange(0, 3 * np.pi, 0.1)\r\nplt.plot(x, np.sin(x))\r\nplt.savefig(\"sine.png\", dpi=150, bbox_inches=\"tight\")\r\nplt.show()<\/pre>\n<\/div>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Which Chart Should You Use?<\/h2>\n<div style=\"border: 1px solid #d9e0ea; border-radius: 8px; overflow-x: auto; margin: 20px 0; -webkit-overflow-scrolling: touch;\">\n<table style=\"width: 100%; min-width: 780px; border-collapse: collapse; font-family: Arial,Helvetica,sans-serif; font-size: 16px; line-height: 1.6; color: #1f2937;\">\n<caption style=\"caption-side: top; text-align: left; padding: 14px 16px; font-weight: bold; font-size: 17px; color: #0f2a4a;\">Choosing a chart type<\/caption>\n<thead>\n<tr>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38; width: 230px;\" scope=\"col\">Chart<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Use it to show<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Matplotlib call<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Line<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">A trend over time<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">plt.plot<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Bar<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">A comparison across categories<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">ax.bar, ax.barh<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Histogram<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">How one numeric variable is spread<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">ax.hist<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Pie<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Parts of a whole, few slices<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">ax.pie<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Scatter<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">The link between two numeric variables<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">ax.scatter<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Contour<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">A surface as lines of equal height<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">ax.contour<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Quiver<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Direction and size at many points<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">ax.quiver<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Violin<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Spread and density of groups<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">ax.violinplot<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">3D contour and wireframe<\/th>\n<td style=\"padding: 12px 16px;\">A surface in three dimensions<\/td>\n<td style=\"padding: 12px 16px; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">ax.contour3D, ax.plot_wireframe<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Common Mistakes With Matplotlib<\/h2>\n<ul style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px; padding-left: 24px;\">\n<li style=\"margin: 0 0 8px;\"><strong>Curly quotes in code.<\/strong> Text copied from a web page or a Word file can turn straight quotes into curly ones, and Python rejects them.<\/li>\n<li style=\"margin: 0 0 8px;\"><strong>Misspelt function names.<\/strong> The correct names are <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">np.arange<\/code> and <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">np.linspace<\/code>.<\/li>\n<li style=\"margin: 0 0 8px;\"><strong>Forgetting the brackets.<\/strong> <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">ax = fig.add_axes<\/code> does nothing. You need to call it, with the four numbers.<\/li>\n<li style=\"margin: 0 0 8px;\"><strong>Saving after show.<\/strong> Call <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">plt.savefig<\/code> before <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">plt.show<\/code>, or you will save a blank image.<\/li>\n<li style=\"margin: 0 0 8px;\"><strong>Overlapping titles in subplots.<\/strong> Add <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">plt.tight_layout<\/code>.<\/li>\n<li style=\"margin: 0 0 8px;\"><strong>Too many pie slices.<\/strong> Use a bar chart when there are more than five or six parts.<\/li>\n<li style=\"margin: 0 0 8px;\"><strong>No labels.<\/strong> A chart without a title and axis labels is hard to read. Always add them.<\/li>\n<\/ul>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Complete Script to Run Yourself<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Here is the full script that tests every plot on this page. It saves each plot as an image file in a folder called <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">mpl_out<\/code>, so create that folder first. The line that says <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">matplotlib.use(\"Agg\")<\/code> lets it run without opening windows. Remove it if you want to see the plots on screen.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">import matplotlib\r\nmatplotlib.use(\"Agg\")  # lets the script run without a window; remove this line in Jupyter\r\nimport matplotlib.pyplot as plt\r\nimport numpy as np\r\n\r\nprint(matplotlib.__version__, np.__version__)\r\n\r\n# 1. Plot a line\r\nplt.plot([1, 2, 3, 4], [10, 20, 25, 30])\r\nplt.xlabel(\"Day\")\r\nplt.ylabel(\"Units sold\")\r\nplt.title(\"Simple line plot\")\r\nplt.savefig(\"mpl_out\/01_line.png\")\r\nplt.close()\r\n\r\n# 2. Simple plot: a sine wave\r\nx = np.arange(0, 3 * np.pi, 0.1)\r\ny = np.sin(x)\r\nplt.plot(x, y)\r\nplt.title(\"Sine wave\")\r\nplt.savefig(\"mpl_out\/02_sine.png\")\r\nplt.close()\r\n\r\n# 3. Several lines in one figure, with a legend\r\nplt.plot(x, np.sin(x), label=\"sin\")\r\nplt.plot(x, np.cos(x), label=\"cos\")\r\nplt.legend()\r\nplt.title(\"Sine and cosine\")\r\nplt.savefig(\"mpl_out\/03_multi.png\")\r\nplt.close()\r\n\r\n# 4. Subplots: top and bottom\r\nplt.subplot(211)\r\nplt.plot(x, np.sin(x))\r\nplt.title(\"Top: sine\")\r\nplt.subplot(212, facecolor=\"y\")\r\nplt.plot(x, np.cos(x))\r\nplt.title(\"Bottom: cosine\")\r\nplt.tight_layout()\r\nplt.savefig(\"mpl_out\/04_subplot.png\")\r\nplt.close()\r\n\r\n# 5. Bar plot\r\nfig = plt.figure()\r\nax = fig.add_axes([0.1, 0.1, 0.8, 0.8])\r\nlanguages = [\"Python\", \"Java\", \"C\", \"SQL\"]\r\nstudents = [25, 18, 12, 20]\r\nax.bar(languages, students)\r\nax.set_title(\"Students per language\")\r\nax.set_ylabel(\"Students\")\r\nfig.savefig(\"mpl_out\/05_bar.png\")\r\nplt.close(fig)\r\n\r\n# 6. Histogram\r\nmarks = np.array([22, 87, 5, 43, 56, 73, 55, 54, 11, 20, 51, 5, 79, 31, 27])\r\nfig, ax = plt.subplots()\r\nax.hist(marks, bins=[0, 25, 50, 75, 100], edgecolor=\"black\")\r\nax.set(title=\"Marks distribution\", xlabel=\"Marks\", ylabel=\"Students\")\r\nfig.savefig(\"mpl_out\/06_hist.png\")\r\nplt.close(fig)\r\n\r\n# 7. Pie chart\r\nfig, ax = plt.subplots()\r\nax.pie([40, 30, 20, 10], labels=[\"Python\", \"Java\", \"C\", \"SQL\"], autopct=\"%1.1f%%\")\r\nax.set_title(\"Share of students\")\r\nfig.savefig(\"mpl_out\/07_pie.png\")\r\nplt.close(fig)\r\n\r\n# 8. Scatter plot\r\nrng = np.random.default_rng(42)\r\nhours = rng.uniform(1, 10, 40)\r\nscore = 35 + 6 * hours + rng.normal(0, 5, 40)\r\nfig, ax = plt.subplots()\r\nax.scatter(hours, score)\r\nax.set_title(\"Study hours against score\")\r\nax.set_xlabel(\"Hours studied\")\r\nax.set_ylabel(\"Score\")\r\nfig.savefig(\"mpl_out\/08_scatter.png\")\r\nplt.close(fig)\r\n\r\n# 9. Contour plot\r\nxs = np.linspace(-3, 3, 100)\r\nys = np.linspace(-3, 3, 100)\r\nX, Y = np.meshgrid(xs, ys)\r\nZ = np.sqrt(X ** 2 + Y ** 2)\r\nfig, ax = plt.subplots()\r\ncs = ax.contour(X, Y, Z)\r\nfig.colorbar(cs)\r\nax.set_title(\"Contour plot\")\r\nfig.savefig(\"mpl_out\/09_contour.png\")\r\nplt.close(fig)\r\n\r\n# 10. Quiver plot\r\ngx, gy = np.meshgrid(np.arange(0, 5), np.arange(0, 5))\r\nu = np.cos(gx)\r\nv = np.sin(gy)\r\nfig, ax = plt.subplots()\r\nax.quiver(gx, gy, u, v)\r\nax.set_title(\"Quiver plot\")\r\nfig.savefig(\"mpl_out\/10_quiver.png\")\r\nplt.close(fig)\r\n\r\n# 11. Violin plot\r\ndata = [rng.normal(50, s, 200) for s in (5, 10, 15)]\r\nfig, ax = plt.subplots()\r\nax.violinplot(data, showmedians=True)\r\nax.set_xticks([1, 2, 3])\r\nax.set_xticklabels([\"Batch A\", \"Batch B\", \"Batch C\"])\r\nax.set_title(\"Violin plot\")\r\nfig.savefig(\"mpl_out\/11_violin.png\")\r\nplt.close(fig)\r\n\r\n# 12. 3D contour\r\nxs = np.linspace(-5, 5, 60)\r\nys = np.linspace(-5, 5, 60)\r\nX, Y = np.meshgrid(xs, ys)\r\nZ = np.sin(np.sqrt(X ** 2 + Y ** 2))\r\nfig = plt.figure()\r\nax = fig.add_subplot(projection=\"3d\")\r\nax.contour3D(X, Y, Z, 50, cmap=\"viridis\")\r\nax.set_title(\"3D contour\")\r\nfig.savefig(\"mpl_out\/12_contour3d.png\")\r\nplt.close(fig)\r\n\r\n# 13. 3D wireframe\r\nfig = plt.figure()\r\nax = fig.add_subplot(projection=\"3d\")\r\nax.plot_wireframe(X, Y, Z, color=\"green\")\r\nax.set_title(\"3D wireframe\")\r\nfig.savefig(\"mpl_out\/13_wireframe.png\")\r\nplt.close(fig)\r\n\r\n# 14. Grids\r\nfig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 3))\r\nax1.plot(x, np.sin(x))\r\nax1.grid(True)\r\nax1.set_title(\"grid on\")\r\nax2.plot(x, np.sin(x))\r\nax2.grid(False)\r\nax2.set_title(\"grid off\")\r\nfig.savefig(\"mpl_out\/14_grid.png\")\r\nplt.close(fig)\r\n\r\n# 15. Styling and saving\r\nplt.plot(x, np.sin(x), color=\"navy\", linestyle=\"--\", marker=\"o\", markersize=3, label=\"sin\")\r\nplt.legend()\r\nplt.xlabel(\"x\")\r\nplt.ylabel(\"sin(x)\")\r\nplt.savefig(\"mpl_out\/15_style.png\", dpi=150, bbox_inches=\"tight\")\r\nplt.close()\r\nprint(\"all plots saved\")<\/pre>\n<\/div>\n<p>Matplotlib looks long, but it comes down to a few habits: pick the right chart for the question, label everything, and run the code yourself. Copy the programs above, change the numbers, and watch the chart change. Once you are comfortable here, try plotting a real dataset with pandas, and then look at Seaborn for faster statistical charts.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python Matplotlib Tutorial for Beginners A table of numbers tells you very little. Put the same numbers on a chart, and in a few seconds you can see the trend, the odd value that does not belong, and the group that is far ahead of the rest. That is why every data analyst, data scientist [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,1676],"tags":[],"class_list":["post-6281","post","type-post","status-publish","format-standard","hentry","category-machine-learning","category-python-data-visualization"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Matplotlib Tutorial for Beginners With Examples | Prwatech<\/title>\n<meta name=\"description\" content=\"Learn Python Matplotlib step by step: line, bar, histogram, pie, scatter, contour, violin and 3D plots, with copyable code you can run today.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prwatech.in\/blog\/python\/python-data-visualization\/python-matplotlib\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Matplotlib Tutorial for Beginners With Examples | Prwatech\" \/>\n<meta property=\"og:description\" content=\"Learn Python Matplotlib step by step: line, bar, histogram, pie, scatter, contour, violin and 3D plots, with copyable code you can run today.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/python\/python-data-visualization\/python-matplotlib\/\" \/>\n<meta property=\"og:site_name\" content=\"Prwatech\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prwatech.in\/\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-19T10:00:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-10-20T05:58:55+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/03\/Screenshot-647.png\" \/>\n<meta name=\"author\" content=\"Prwatech\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:site\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prwatech\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prwatech.in\/blog\/python\/python-data-visualization\/python-matplotlib\/\",\"url\":\"https:\/\/prwatech.in\/blog\/python\/python-data-visualization\/python-matplotlib\/\",\"name\":\"Python Matplotlib Tutorial for Beginners With Examples | Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prwatech.in\/blog\/python\/python-data-visualization\/python-matplotlib\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prwatech.in\/blog\/python\/python-data-visualization\/python-matplotlib\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/03\/Screenshot-647.png\",\"datePublished\":\"2026-10-19T10:00:27+00:00\",\"dateModified\":\"2026-10-20T05:58:55+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Learn Python Matplotlib step by step: line, bar, histogram, pie, scatter, contour, violin and 3D plots, with copyable code you can run today.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/python\/python-data-visualization\/python-matplotlib\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/python\/python-data-visualization\/python-matplotlib\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/python\/python-data-visualization\/python-matplotlib\/#primaryimage\",\"url\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/03\/Screenshot-647.png\",\"contentUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/03\/Screenshot-647.png\",\"width\":433,\"height\":380},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/python\/python-data-visualization\/python-matplotlib\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Matplotlib\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/prwatech.in\/blog\/#website\",\"url\":\"https:\/\/prwatech.in\/blog\/\",\"name\":\"Prwatech\",\"description\":\"Share Ideas, Start Something Good.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/prwatech.in\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\",\"name\":\"Prwatech\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"caption\":\"Prwatech\"},\"url\":\"https:\/\/prwatech.in\/blog\/author\/prwatech123\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Matplotlib Tutorial for Beginners With Examples | Prwatech","description":"Learn Python Matplotlib step by step: line, bar, histogram, pie, scatter, contour, violin and 3D plots, with copyable code you can run today.","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:\/\/prwatech.in\/blog\/python\/python-data-visualization\/python-matplotlib\/","og_locale":"en_US","og_type":"article","og_title":"Python Matplotlib Tutorial for Beginners With Examples | Prwatech","og_description":"Learn Python Matplotlib step by step: line, bar, histogram, pie, scatter, contour, violin and 3D plots, with copyable code you can run today.","og_url":"https:\/\/prwatech.in\/blog\/python\/python-data-visualization\/python-matplotlib\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2026-10-19T10:00:27+00:00","article_modified_time":"2026-10-20T05:58:55+00:00","og_image":[{"url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/03\/Screenshot-647.png","type":"","width":"","height":""}],"author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/python\/python-data-visualization\/python-matplotlib\/","url":"https:\/\/prwatech.in\/blog\/python\/python-data-visualization\/python-matplotlib\/","name":"Python Matplotlib Tutorial for Beginners With Examples | Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prwatech.in\/blog\/python\/python-data-visualization\/python-matplotlib\/#primaryimage"},"image":{"@id":"https:\/\/prwatech.in\/blog\/python\/python-data-visualization\/python-matplotlib\/#primaryimage"},"thumbnailUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/03\/Screenshot-647.png","datePublished":"2026-10-19T10:00:27+00:00","dateModified":"2026-10-20T05:58:55+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Learn Python Matplotlib step by step: line, bar, histogram, pie, scatter, contour, violin and 3D plots, with copyable code you can run today.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/python\/python-data-visualization\/python-matplotlib\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/python\/python-data-visualization\/python-matplotlib\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/python\/python-data-visualization\/python-matplotlib\/#primaryimage","url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/03\/Screenshot-647.png","contentUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/03\/Screenshot-647.png","width":433,"height":380},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/python\/python-data-visualization\/python-matplotlib\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Python Matplotlib"}]},{"@type":"WebSite","@id":"https:\/\/prwatech.in\/blog\/#website","url":"https:\/\/prwatech.in\/blog\/","name":"Prwatech","description":"Share Ideas, Start Something Good.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/prwatech.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3","name":"Prwatech","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","caption":"Prwatech"},"url":"https:\/\/prwatech.in\/blog\/author\/prwatech123\/"}]}},"_links":{"self":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/6281","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/comments?post=6281"}],"version-history":[{"count":52,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/6281\/revisions"}],"predecessor-version":[{"id":12048,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/6281\/revisions\/12048"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=6281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=6281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=6281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}