{"id":8882,"date":"2021-02-10T07:00:31","date_gmt":"2021-02-10T01:30:31","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=8882"},"modified":"2026-07-13T12:32:10","modified_gmt":"2026-07-13T07:02:10","slug":"matplotlib-draw-rectangle","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/","title":{"rendered":"Draw Rectangles in Matplotlib with Rectangle and add_patch"},"content":{"rendered":"<p><strong>Quick answer:<\/strong> Create a matplotlib.patches.Rectangle with an (x, y) anchor, width, and height, then call ax.add_patch(rect). The values use the axes data coordinate system by default. Set limits for standalone examples, use facecolor=&#8217;none&#8217; for an outline, and choose zorder, alpha, and rotation deliberately so the patch explains rather than hides the data.<\/p>\n<figure class=\"pythonpool-article-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/matplotlib-draw-rectangle.png\" alt=\"Python Pool infographic showing Matplotlib Rectangle xy width height add_patch styling and rotation\" width=\"1536\" height=\"1024\" loading=\"lazy\" decoding=\"async\"><figcaption>A Rectangle describes an anchor, width, and height in the axes coordinate system; add it to the axes and set limits when a standalone example needs a stable view.<\/figcaption><\/figure>\n<p>To draw a rectangle in Matplotlib, create a <code>matplotlib.patches.Rectangle<\/code> object and add it to an axes with <code>ax.add_patch()<\/code>. A rectangle is defined by an anchor point <code>xy<\/code>, a <code>width<\/code>, and a <code>height<\/code>.<\/p>\n<p>This guide shows the practical patterns: drawing a rectangle on a blank axes, highlighting points in a scatter plot, placing a box over an image, rotating a rectangle, and avoiding common axis-limit mistakes. Matplotlib is not installed in this local environment, so the examples were syntax-checked and aligned with the official Matplotlib documentation rather than rendered here.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/#Basic_rectangle_with_Rectangle_and_add_patch\" >Basic rectangle with Rectangle and add_patch()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/#Rectangle_parameters_that_matter\" >Rectangle parameters that matter<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/#Draw_a_rectangle_around_scatter_points\" >Draw a rectangle around scatter points<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/#Draw_a_rectangle_on_an_image\" >Draw a rectangle on an image<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/#Draw_a_rotated_rectangle\" >Draw a rotated rectangle<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/#Filled_vs_outline_rectangles\" >Filled vs outline rectangles<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/#Common_mistakes\" >Common mistakes<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/#Official_references\" >Official references<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/#Understand_xy_Width_And_Height\" >Understand xy, Width, And Height<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/#Use_An_Outline_For_Highlighting\" >Use An Outline For Highlighting<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/#Rotate_Around_The_Intended_Point\" >Rotate Around The Intended Point<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/#Keep_Patches_Readable_Above_Data\" >Keep Patches Readable Above Data<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/#Frequently_Asked_Questions\" >Frequently Asked Questions<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Basic_rectangle_with_Rectangle_and_add_patch\"><\/span>Basic rectangle with Rectangle and add_patch()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>xy<\/code> argument gives the rectangle anchor point. With normal x\/y axes, you can think of it as the lower-left corner.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nfrom matplotlib.patches import Rectangle\n\nfig, ax = plt.subplots()\n\nrect = Rectangle(\n    (1, 2),\n    3,\n    1.5,\n    facecolor=\"none\",\n    edgecolor=\"red\",\n    linewidth=2,\n)\n\nax.add_patch(rect)\nax.set_xlim(0, 5)\nax.set_ylim(0, 5)\nax.set_aspect(\"equal\")\nplt.show()<\/code><\/pre>\n<\/div>\n<p><code>Rectangle((1, 2), 3, 1.5)<\/code> starts at <code>(1, 2)<\/code>, extends 3 units along the x-axis, and extends 1.5 units along the y-axis. <code>facecolor=\"none\"<\/code> makes the rectangle transparent inside, while <code>edgecolor<\/code> and <code>linewidth<\/code> style the border.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Rectangle_parameters_that_matter\"><\/span>Rectangle parameters that matter<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The most important constructor parameters are <code>xy<\/code>, <code>width<\/code>, <code>height<\/code>, <code>angle<\/code>, and <code>rotation_point<\/code>. Styling options come from Matplotlib&#8217;s general <code>Patch<\/code> properties, including <code>facecolor<\/code>, <code>edgecolor<\/code>, <code>linewidth<\/code>, <code>linestyle<\/code>, <code>alpha<\/code>, and <code>zorder<\/code>.<\/p>\n<p>If your rectangle does not appear, check the axis limits first. A patch can exist but be outside the visible x\/y range. Explicitly setting <code>ax.set_xlim()<\/code> and <code>ax.set_ylim()<\/code> keeps the example predictable. For more on drawing order, see <a href=\"https:\/\/www.pythonpool.com\/matplotlib-zorder\/\">Matplotlib zorder<\/a>.<\/p>\n<p><!-- Python Pool visual layout repair 2026-07-13 --><\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/rectangle-patch-b150.png\" alt=\"Python Pool infographic showing Matplotlib Rectangle, xy origin, width, height, and axes\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Rectangle patch: Matplotlib Rectangle, xy origin, width, height, and axes.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Draw_a_rectangle_around_scatter_points\"><\/span>Draw a rectangle around scatter points<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Rectangles are often used to highlight a region of interest on an existing plot.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nfrom matplotlib.patches import Rectangle\n\nx = [1, 2, 3, 4]\ny = [2, 3, 1, 4]\n\nfig, ax = plt.subplots()\nax.scatter(x, y)\n\nhighlight = Rectangle(\n    (1.5, 1.5),\n    2.0,\n    2.0,\n    facecolor=\"none\",\n    edgecolor=\"orange\",\n    linewidth=2,\n)\nax.add_patch(highlight)\n\nplt.show()<\/code><\/pre>\n<\/div>\n<p>The rectangle uses the same data coordinates as the scatter plot. If the box should appear above other artists, pass a higher <code>zorder<\/code>. If you need arrows or text labels for the highlighted area, see <a href=\"https:\/\/www.pythonpool.com\/matplotlib-annotate\/\">Matplotlib annotate()<\/a>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Draw_a_rectangle_on_an_image\"><\/span>Draw a rectangle on an image<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>When you show an image with <code>imshow()<\/code>, you can add a rectangle patch to mark a region. This is common for bounding boxes and visual inspection.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nfrom matplotlib.patches import Rectangle\n\nfig, ax = plt.subplots()\nax.imshow(image)\n\nbox = Rectangle(\n    (40, 30),\n    120,\n    80,\n    facecolor=\"none\",\n    edgecolor=\"lime\",\n    linewidth=2,\n)\nax.add_patch(box)\n\nplt.show()<\/code><\/pre>\n<\/div>\n<p>Here, <code>(40, 30)<\/code> is the box anchor in image coordinates, and the width and height are measured in the same coordinate system used by the axes. For image loading basics, see <a href=\"https:\/\/www.pythonpool.com\/matplotlib-imread\/\">Matplotlib imread<\/a>.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/rectangle-geometry-b150.png\" alt=\"Python Pool infographic mapping origin, width, height, angle, center, and bounding box\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Rectangle geometry: Origin, width, height, angle, center, and bounding box.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Draw_a_rotated_rectangle\"><\/span>Draw a rotated rectangle<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Use <code>angle<\/code> to rotate a rectangle. Recent Matplotlib versions also support <code>rotation_point<\/code>, which controls whether rotation happens around the anchor point, the center, or a custom point.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nfrom matplotlib.patches import Rectangle\n\nfig, ax = plt.subplots()\n\nrect = Rectangle(\n    (1, 1),\n    3,\n    1,\n    angle=25,\n    rotation_point=\"center\",\n    facecolor=\"none\",\n    edgecolor=\"purple\",\n)\nax.add_patch(rect)\nax.set_xlim(0, 5)\nax.set_ylim(0, 4)\n\nplt.show()<\/code><\/pre>\n<\/div>\n<p>If you are building diagrams with multiple shapes, keep related axis settings near the patch code. For circles and other patch-like shapes, see <a href=\"https:\/\/www.pythonpool.com\/matplotlib-circle\/\">drawing circles in Matplotlib<\/a>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Filled_vs_outline_rectangles\"><\/span>Filled vs outline rectangles<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>For a filled rectangle, pass a visible <code>facecolor<\/code>. For an outline only, use <code>facecolor=\"none\"<\/code>. Use <code>alpha<\/code> when the rectangle should overlay data without hiding it.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">filled = Rectangle((0, 0), 2, 1, facecolor=\"steelblue\", alpha=0.3)\noutline = Rectangle((0, 1.5), 2, 1, facecolor=\"none\", edgecolor=\"black\")<\/code><\/pre>\n<\/div>\n<p>Matplotlib patches are flexible, but keep the plot readable. Too many filled patches can hide the data you are trying to explain. For layout cleanup after labels and annotations, see <a href=\"https:\/\/www.pythonpool.com\/matplotlib-tightlayout\/\">Matplotlib tight_layout()<\/a>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Common_mistakes\"><\/span>Common mistakes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The most common mistake is creating a <code>Rectangle<\/code> but never adding it to the axes. Always call <code>ax.add_patch(rect)<\/code>.<\/p>\n<p>The second common mistake is forgetting axis limits. If the rectangle coordinates are outside the current view, it will not be visible. Set <code>xlim<\/code> and <code>ylim<\/code> when creating standalone examples.<\/p>\n<p>Also remember that <code>width<\/code> and <code>height<\/code> are in data coordinates. A rectangle on an image, a scatter plot, and a regular x\/y plot all use the coordinate system of the axes. For related axis control, see <a href=\"https:\/\/www.pythonpool.com\/matplotlib-aspect-ratio\/\">Matplotlib aspect ratio<\/a> and <a href=\"https:\/\/www.pythonpool.com\/matplotlib-ylim\/\">Matplotlib ylim<\/a>.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/rectangle-style-b150.png\" alt=\"Python Pool infographic comparing facecolor, edgecolor, linewidth, hatch, alpha, and zorder\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Style rectangle: Facecolor, edgecolor, linewidth, hatch, alpha, and zorder.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Official_references\"><\/span>Official references<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The examples here follow the Matplotlib documentation for <a href=\"https:\/\/matplotlib.org\/stable\/api\/_as_gen\/matplotlib.patches.Rectangle.html\">matplotlib.patches.Rectangle<\/a>, <a href=\"https:\/\/matplotlib.org\/stable\/api\/_as_gen\/matplotlib.axes.Axes.add_patch.html\">Axes.add_patch()<\/a>, <a href=\"https:\/\/matplotlib.org\/stable\/api\/_as_gen\/matplotlib.patches.Patch.html\">Patch properties<\/a>, <a href=\"https:\/\/matplotlib.org\/stable\/api\/_as_gen\/matplotlib.axes.Axes.imshow.html\">Axes.imshow()<\/a>, and <a href=\"https:\/\/matplotlib.org\/stable\/api\/_as_gen\/matplotlib.axes.Axes.set_aspect.html\">Axes.set_aspect()<\/a>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Use <code>Rectangle<\/code> plus <code>ax.add_patch()<\/code> to draw rectangles in Matplotlib. Start with <code>xy<\/code>, <code>width<\/code>, and <code>height<\/code>, style the patch with color and line options, set axis limits when needed, and use <code>angle<\/code> or <code>rotation_point<\/code> for rotated boxes.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Understand_xy_Width_And_Height\"><\/span>Understand xy, Width, And Height<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Rectangle takes its lower-left anchor and dimensions in the current axes coordinate system. For a plot with dates, categories, or image pixels, confirm what the axes coordinates mean before choosing the box dimensions.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nfrom matplotlib.patches import Rectangle\n\nfig, ax = plt.subplots()\nrect = Rectangle((1, 2), width=3, height=1.5)\nax.add_patch(rect)\nax.set_xlim(0, 5)\nax.set_ylim(0, 5)\nplt.show()\n<\/code><\/pre>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Use_An_Outline_For_Highlighting\"><\/span>Use An Outline For Highlighting<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>An outline is often clearer than a translucent fill when the rectangle surrounds existing points. Set facecolor=&#8217;none&#8217;, pick a contrasting edge color, and keep the line width large enough for the final export size.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">from matplotlib.patches import Rectangle\n\nhighlight = Rectangle((1, 1), 2, 2, facecolor=\"none\", edgecolor=\"crimson\", linewidth=2, zorder=3)\nax.add_patch(highlight)\n<\/code><\/pre>\n<\/div>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/rectangle-check-b150.png\" alt=\"Python Pool infographic testing transforms, clipping, limits, multiple patches, and savefig\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Rectangle checks: Transforms, clipping, limits, multiple patches, and savefig.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Rotate_Around_The_Intended_Point\"><\/span>Rotate Around The Intended Point<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The angle is applied to the patch, and supported Matplotlib versions provide rotation_point for choosing the pivot. Keep the axis limits generous enough to include the rotated corners, otherwise a valid rectangle can look clipped or missing.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">from matplotlib.patches import Rectangle\n\nrotated = Rectangle((1, 1), 3, 1, angle=25, rotation_point=\"center\", facecolor=\"none\")\nax.add_patch(rotated)\nax.set_xlim(0, 5)\nax.set_ylim(0, 4)\n<\/code><\/pre>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Keep_Patches_Readable_Above_Data\"><\/span>Keep Patches Readable Above Data<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Use alpha for a filled region that should not hide the plot and zorder when the patch must sit above or below other artists. After adding patches, render the actual PNG or PDF and check that the legend, labels, and rectangle do not overlap.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">from matplotlib.patches import Rectangle\n\nregion = Rectangle((2, 0), 1, 4, facecolor=\"gold\", alpha=0.25, zorder=0)\nax.add_patch(region)\n<\/code><\/pre>\n<\/div>\n<p>The implementation follows the official <a href=\"https:\/\/matplotlib.org\/stable\/api\/_as_gen\/matplotlib.patches.Rectangle.html\">Rectangle reference<\/a> and <a href=\"https:\/\/matplotlib.org\/stable\/api\/_as_gen\/matplotlib.axes.Axes.add_patch.html\">Axes.add_patch reference<\/a>. For labels attached to a highlighted area, see <a href=\"https:\/\/www.pythonpool.com\/matplotlib-annotate\/\">Matplotlib annotate()<\/a>.<\/p>\n<p>For related plot callouts and layering, compare <a href=\"https:\/\/www.pythonpool.com\/matplotlib-annotate\/\">Matplotlib annotate()<\/a>, <a href=\"https:\/\/www.pythonpool.com\/matplotlib-zorder\/\">zorder control<\/a>, and <a href=\"https:\/\/www.pythonpool.com\/matplotlib-circle\/\">circle patches<\/a> when marking a region without hiding the data.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Frequently_Asked_Questions\"><\/span>Frequently Asked Questions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3>How do I draw a rectangle in Matplotlib?<\/h3>\n<p>Import Rectangle from matplotlib.patches, create it with an xy anchor, width, and height, then add it to an axes with ax.add_patch(rect).<\/p>\n<h3>Why is my Matplotlib rectangle not visible?<\/h3>\n<p>The patch may not have been added to the axes, or its data-coordinate bounds may be outside the current x and y limits.<\/p>\n<h3>How do I draw an outline-only rectangle?<\/h3>\n<p>Set facecolor=&#8217;none&#8217; and choose an edgecolor and linewidth so the data underneath remains visible.<\/p>\n<h3>Can Matplotlib rectangles be rotated?<\/h3>\n<p>Yes. Pass angle and, on supported versions, rotation_point to control the orientation and pivot of the patch.<\/p>\n<p><script type=\"application\/ld+json\">{\"@context\":\"https:\/\/schema.org\",\"@type\":\"FAQPage\",\"mainEntity\":[{\"@type\":\"Question\",\"name\":\"How do I draw a rectangle in Matplotlib?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Import Rectangle from matplotlib.patches, create it with an xy anchor, width, and height, then add it to an axes with ax.add_patch(rect).\"}},{\"@type\":\"Question\",\"name\":\"Why is my Matplotlib rectangle not visible?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"The patch may not have been added to the axes, or its data-coordinate bounds may be outside the current x and y limits.\"}},{\"@type\":\"Question\",\"name\":\"How do I draw an outline-only rectangle?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Set facecolor='none' and choose an edgecolor and linewidth so the data underneath remains visible.\"}},{\"@type\":\"Question\",\"name\":\"Can Matplotlib rectangles be rotated?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Yes. Pass angle and, on supported versions, rotation_point to control the orientation and pivot of the patch.\"}}]}<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Draw Matplotlib rectangles with Rectangle, add_patch, data coordinates, styling, rotation, image boxes, zorder, and readable limits.<\/p>\n","protected":false},"author":11,"featured_media":32177,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2071],"tags":[3595,3592,3594,3596,3590,3591,3593],"class_list":["post-8882","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-matplotlib","tag-draw-a-rectangle-in-matplotlib","tag-draw-rectangle-matplotlib","tag-draw-rectangle-over-face-matplotlib","tag-how-to-draw-multiple-rectangle-matplotlib-python","tag-matplotlib-draw-rectangle-on-image","tag-matplotlib-draw-rectangle-overwrite","tag-python-matplotlib-draw-rectangle","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v28.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Draw Rectangles in Matplotlib with Rectangle and add_patch<\/title>\n<meta name=\"description\" content=\"Draw Matplotlib rectangles with Rectangle, add_patch, data coordinates, styling, rotation, image boxes, zorder, and readable limits.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Draw Rectangles in Matplotlib with Rectangle and add_patch\" \/>\n<meta property=\"og:description\" content=\"Draw Matplotlib rectangles with Rectangle, add_patch, data coordinates, styling, rotation, image boxes, zorder, and readable limits.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2021-02-10T01:30:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-07-13T07:02:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/matplotlib-draw-rectangle.png\" \/>\n<meta name=\"author\" content=\"Rohit Kumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"Python Pool\" \/>\n<meta name=\"twitter:description\" content=\"Practical Python tutorials, error fixes, code examples, and project guides.\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/matplotlib-draw-rectangle.png\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rohit Kumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/matplotlib-draw-rectangle\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/matplotlib-draw-rectangle\\\/\"},\"author\":{\"name\":\"Rohit Kumar\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/person\\\/664ced779ebbb8ddfd3c58a60d6f9d66\"},\"headline\":\"Draw Rectangles in Matplotlib with Rectangle and add_patch\",\"datePublished\":\"2021-02-10T01:30:31+00:00\",\"dateModified\":\"2026-07-13T07:02:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/matplotlib-draw-rectangle\\\/\"},\"wordCount\":1027,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/matplotlib-draw-rectangle\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/matplotlib-rectangle-patches-add-patch.png\",\"keywords\":[\"draw a rectangle in matplotlib\",\"draw rectangle matplotlib\",\"draw rectangle over face matplotlib\",\"how to draw multiple rectangle matplotlib python\",\"matplotlib draw rectangle on image\",\"matplotlib draw rectangle overwrite\",\"python matplotlib draw rectangle\"],\"articleSection\":[\"Matplotlib\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pythonpool.com\\\/matplotlib-draw-rectangle\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/matplotlib-draw-rectangle\\\/\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/matplotlib-draw-rectangle\\\/\",\"name\":\"Draw Rectangles in Matplotlib with Rectangle and add_patch\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/matplotlib-draw-rectangle\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/matplotlib-draw-rectangle\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/matplotlib-rectangle-patches-add-patch.png\",\"datePublished\":\"2021-02-10T01:30:31+00:00\",\"dateModified\":\"2026-07-13T07:02:10+00:00\",\"description\":\"Draw Matplotlib rectangles with Rectangle, add_patch, data coordinates, styling, rotation, image boxes, zorder, and readable limits.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/matplotlib-draw-rectangle\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pythonpool.com\\\/matplotlib-draw-rectangle\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/matplotlib-draw-rectangle\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/matplotlib-rectangle-patches-add-patch.png\",\"contentUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/matplotlib-rectangle-patches-add-patch.png\",\"width\":1350,\"height\":650,\"caption\":\"Matplotlib Rectangle patch add_patch xy width height\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/matplotlib-draw-rectangle\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pythonpool.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Draw Rectangles in Matplotlib with Rectangle and add_patch\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/\",\"name\":\"Python Pool\",\"description\":\"Practical Python tutorials, error fixes, code examples, and project guides.\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pythonpool.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/aa.png\",\"contentUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/x.com\\\/pythonpool\",\"https:\\\/\\\/www.youtube.com\\\/c\\\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/person\\\/664ced779ebbb8ddfd3c58a60d6f9d66\",\"name\":\"Rohit Kumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c9a762a12f6bf9093f4c8a7643c0b6a17dd903d9602fae379649d1b050386b1a?s=96&d=wavatar&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c9a762a12f6bf9093f4c8a7643c0b6a17dd903d9602fae379649d1b050386b1a?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c9a762a12f6bf9093f4c8a7643c0b6a17dd903d9602fae379649d1b050386b1a?s=96&d=wavatar&r=g\",\"caption\":\"Rohit Kumar\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Draw Rectangles in Matplotlib with Rectangle and add_patch","description":"Draw Matplotlib rectangles with Rectangle, add_patch, data coordinates, styling, rotation, image boxes, zorder, and readable limits.","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:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/","og_locale":"en_US","og_type":"article","og_title":"Draw Rectangles in Matplotlib with Rectangle and add_patch","og_description":"Draw Matplotlib rectangles with Rectangle, add_patch, data coordinates, styling, rotation, image boxes, zorder, and readable limits.","og_url":"https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/","og_site_name":"Python Pool","article_published_time":"2021-02-10T01:30:31+00:00","article_modified_time":"2026-07-13T07:02:10+00:00","og_image":[{"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/matplotlib-draw-rectangle.png","type":"","width":"","height":""}],"author":"Rohit Kumar","twitter_card":"summary_large_image","twitter_title":"Python Pool","twitter_description":"Practical Python tutorials, error fixes, code examples, and project guides.","twitter_image":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/matplotlib-draw-rectangle.png","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Rohit Kumar","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/"},"author":{"name":"Rohit Kumar","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/664ced779ebbb8ddfd3c58a60d6f9d66"},"headline":"Draw Rectangles in Matplotlib with Rectangle and add_patch","datePublished":"2021-02-10T01:30:31+00:00","dateModified":"2026-07-13T07:02:10+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/"},"wordCount":1027,"commentCount":4,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/02\/matplotlib-rectangle-patches-add-patch.png","keywords":["draw a rectangle in matplotlib","draw rectangle matplotlib","draw rectangle over face matplotlib","how to draw multiple rectangle matplotlib python","matplotlib draw rectangle on image","matplotlib draw rectangle overwrite","python matplotlib draw rectangle"],"articleSection":["Matplotlib"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/","url":"https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/","name":"Draw Rectangles in Matplotlib with Rectangle and add_patch","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/02\/matplotlib-rectangle-patches-add-patch.png","datePublished":"2021-02-10T01:30:31+00:00","dateModified":"2026-07-13T07:02:10+00:00","description":"Draw Matplotlib rectangles with Rectangle, add_patch, data coordinates, styling, rotation, image boxes, zorder, and readable limits.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/02\/matplotlib-rectangle-patches-add-patch.png","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/02\/matplotlib-rectangle-patches-add-patch.png","width":1350,"height":650,"caption":"Matplotlib Rectangle patch add_patch xy width height"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/matplotlib-draw-rectangle\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"Draw Rectangles in Matplotlib with Rectangle and add_patch"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Practical Python tutorials, error fixes, code examples, and project guides.","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/664ced779ebbb8ddfd3c58a60d6f9d66","name":"Rohit Kumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c9a762a12f6bf9093f4c8a7643c0b6a17dd903d9602fae379649d1b050386b1a?s=96&d=wavatar&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c9a762a12f6bf9093f4c8a7643c0b6a17dd903d9602fae379649d1b050386b1a?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c9a762a12f6bf9093f4c8a7643c0b6a17dd903d9602fae379649d1b050386b1a?s=96&d=wavatar&r=g","caption":"Rohit Kumar"}}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/8882","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=8882"}],"version-history":[{"count":24,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/8882\/revisions"}],"predecessor-version":[{"id":41582,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/8882\/revisions\/41582"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/32177"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=8882"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=8882"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=8882"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}