{"id":3568,"date":"2023-07-16T19:12:36","date_gmt":"2023-07-16T19:12:36","guid":{"rendered":"https:\/\/statorials.org\/pt\/legenda-do-manual-matplotlib\/"},"modified":"2023-07-16T19:12:36","modified_gmt":"2023-07-16T19:12:36","slug":"legenda-do-manual-matplotlib","status":"publish","type":"post","link":"https:\/\/statorials.org\/pt\/legenda-do-manual-matplotlib\/","title":{"rendered":"Como criar uma legenda manual no matplotlib (com exemplo)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Voc\u00ea pode usar as fun\u00e7\u00f5es dos subm\u00f3dulos <strong>matplotlib.lines<\/strong> e <strong>matplotlib.patches<\/strong> para criar uma legenda manual em um gr\u00e1fico matplotlib.<\/span><\/p>\n<p> <span style=\"color: #000000;\">O exemplo a seguir mostra como fazer isso.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Exemplo: crie uma legenda manual no Matplotlib<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">O c\u00f3digo a seguir mostra como criar um gr\u00e1fico de dispers\u00e3o no matplotlib com uma legenda padr\u00e3o:<\/span> <\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #000000;\"><span style=\"color: #008000;\">import<\/span> matplotlib. <span style=\"color: #3366ff;\">pyplot<\/span> <span style=\"color: #008000;\">as<\/span> plt\n\n<span style=\"color: #008080;\">#define data to plot\n<\/span>x = [1, 2, 3, 4, 5, 6, 7]\ny = [2, 3, 5, 8, 12, 18, 27]\n\n<span style=\"color: #008080;\">#create scatter plot of x vs. y\n<\/span>plt. <span style=\"color: #3366ff;\">scatter<\/span> (x, y, label=' <span style=\"color: #ff0000;\">Original Data<\/span> ', color=' <span style=\"color: #ff0000;\">steelblue<\/span> ')\n\n<span style=\"color: #008080;\">#add legend\n<\/span>plt. <span style=\"color: #3366ff;\">legend<\/span> ()\n\n<span style=\"color: #008080;\">#displayplot\n<\/span>plt. <span style=\"color: #3366ff;\">show<\/span> ()<\/span><\/strong> <\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\" wp-image-29488 aligncenter\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/manuel1.jpg\" alt=\"\" width=\"482\" height=\"367\" srcset=\"\" sizes=\"auto, \"><\/p>\n<p> <span style=\"color: #000000;\">Para criar uma legenda manual com linhas e quadrados personalizados, precisamos importar os subm\u00f3dulos <strong>matplotlib.lines<\/strong> e <strong>matplotlib.patches<\/strong> .<\/span><\/p>\n<p> <span style=\"color: #000000;\">O c\u00f3digo a seguir mostra como usar esses subm\u00f3dulos para criar uma legenda manual:<\/span> <\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #000000;\"><span style=\"color: #008000;\">import<\/span> matplotlib. <span style=\"color: #3366ff;\">pyplot<\/span> <span style=\"color: #008000;\">as<\/span> plt\n<span style=\"color: #008000;\">from<\/span> matplotlib. <span style=\"color: #3366ff;\">lines<\/span> <span style=\"color: #008000;\">import<\/span> Line2D\n<span style=\"color: #008000;\">import<\/span> matplotlib. <span style=\"color: #3366ff;\">patches<\/span> <span style=\"color: #008000;\">as<\/span> mpatches\n\n<span style=\"color: #008080;\">#define data to plot\n<\/span>x = [1, 2, 3, 4, 5, 6, 7]\ny = [2, 3, 5, 8, 12, 18, 27]\n\n<span style=\"color: #008080;\">#create scatter plot of x vs. y\n<\/span>plt. <span style=\"color: #3366ff;\">scatter<\/span> (x, y, label=' <span style=\"color: #ff0000;\">Original Data<\/span> ', color=' <span style=\"color: #ff0000;\">steelblue<\/span> ')\n\n<span style=\"color: #008080;\">#define handles and labels that will get added to legend\n<\/span>handles, labels = plt. <span style=\"color: #3366ff;\">gca<\/span> (). <span style=\"color: #3366ff;\">get_legend_handles_labels<\/span> ()\n\n<span style=\"color: #008080;\">#define patches and lines to add to legend\n<\/span>patch1 = mpatches. <span style=\"color: #3366ff;\">Patch<\/span> (color=' <span style=\"color: #ff0000;\">orange<\/span> ', label=' <span style=\"color: #ff0000;\">First Manual Patch<\/span> ')\npatch2 = mpatches. <span style=\"color: #3366ff;\">Patch<\/span> (color=' <span style=\"color: #ff0000;\">orange<\/span> ', label=' <span style=\"color: #ff0000;\">First Manual Patch<\/span> ')   \nline1 = Line2D([0], [0], label=' <span style=\"color: #ff0000;\">First Manual Line<\/span> ', color=' <span style=\"color: #ff0000;\">purple<\/span> ')\nline2 = Line2D([0], [0], label=' <span style=\"color: #ff0000;\">Second Manual Line<\/span> ', color=' <span style=\"color: #ff0000;\">red<\/span> ')\n\n<span style=\"color: #008080;\">#add handles\n<\/span>handles. <span style=\"color: #3366ff;\">extend<\/span> ([patch1, line1, line2])\n\n<span style=\"color: #008080;\">#add legend\n<\/span>plt. <span style=\"color: #3366ff;\">legend<\/span> (handles=handles)\n\n<span style=\"color: #008080;\">#displayplot\n<\/span>plt. <span style=\"color: #3366ff;\">show<\/span> ()<\/span><\/strong> <\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-29490\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/manuel2.jpg\" alt=\"Legenda manual do Matplotlib\" width=\"530\" height=\"400\" srcset=\"\" sizes=\"auto, \"><\/p>\n<p> <span style=\"color: #000000;\">Observe que esta legenda inclui o r\u00f3tulo dos dados originais, mas tamb\u00e9m os r\u00f3tulos e formas dos elementos que adicionamos manualmente.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Para alterar os r\u00f3tulos ou cores de qualquer um dos elementos, basta alterar os valores dos argumentos <strong>r\u00f3tulo<\/strong> e <strong>cor<\/strong> no trecho de c\u00f3digo anterior.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Nota<\/strong> : Consulte <a href=\"https:\/\/statorials.org\/pt\/posicao-da-legenda-matplotlib\/\" target=\"_blank\" rel=\"noopener\">este tutorial<\/a> para saber como alterar a posi\u00e7\u00e3o da legenda no gr\u00e1fico.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Recursos adicionais<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Os tutoriais a seguir explicam como realizar outras tarefas comuns no Matplotlib:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/pt\/matplotlib-aumenta-o-tamanho-do-rastreamento\/\" target=\"_blank\" rel=\"noopener\">Como aumentar o tamanho do gr\u00e1fico no Matplotlib<\/a><br \/> <a href=\"https:\/\/statorials.org\/pt\/posicao-do-titulo-matplotlib\/\" target=\"_blank\" rel=\"noopener\">Como ajustar a posi\u00e7\u00e3o do t\u00edtulo no Matplotlib<\/a><br \/> <a href=\"https:\/\/statorials.org\/pt\/intervalo-de-eixo-definido-matplotlib\/\" target=\"_blank\" rel=\"noopener\">Como definir intervalos de eixos no Matplotlib<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Voc\u00ea pode usar as fun\u00e7\u00f5es dos subm\u00f3dulos matplotlib.lines e matplotlib.patches para criar uma legenda manual em um gr\u00e1fico matplotlib. O exemplo a seguir mostra como fazer isso. Exemplo: crie uma legenda manual no Matplotlib O c\u00f3digo a seguir mostra como criar um gr\u00e1fico de dispers\u00e3o no matplotlib com uma legenda padr\u00e3o: import matplotlib. pyplot as [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-3568","post","type-post","status-publish","format-standard","hentry","category-guia"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Como criar uma legenda manual no Matplotlib (com exemplo) - Statorials<\/title>\n<meta name=\"description\" content=\"Este tutorial explica como criar uma legenda manual no matplotlib, com um exemplo.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/statorials.org\/pt\/legenda-do-manual-matplotlib\/\" \/>\n<meta property=\"og:locale\" content=\"pt_PT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Como criar uma legenda manual no Matplotlib (com exemplo) - Statorials\" \/>\n<meta property=\"og:description\" content=\"Este tutorial explica como criar uma legenda manual no matplotlib, com um exemplo.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/pt\/legenda-do-manual-matplotlib\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-16T19:12:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/manuel1.jpg\" \/>\n<meta name=\"author\" content=\"Dr. benjamim anderson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Escrito por\" \/>\n\t<meta name=\"twitter:data1\" content=\"Dr. benjamim anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Tempo estimado de leitura\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/pt\/legenda-do-manual-matplotlib\/\",\"url\":\"https:\/\/statorials.org\/pt\/legenda-do-manual-matplotlib\/\",\"name\":\"Como criar uma legenda manual no Matplotlib (com exemplo) - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/pt\/#website\"},\"datePublished\":\"2023-07-16T19:12:36+00:00\",\"dateModified\":\"2023-07-16T19:12:36+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666\"},\"description\":\"Este tutorial explica como criar uma legenda manual no matplotlib, com um exemplo.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/pt\/legenda-do-manual-matplotlib\/#breadcrumb\"},\"inLanguage\":\"pt-PT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/pt\/legenda-do-manual-matplotlib\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/pt\/legenda-do-manual-matplotlib\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Lar\",\"item\":\"https:\/\/statorials.org\/pt\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Como criar uma legenda manual no matplotlib (com exemplo)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/pt\/#website\",\"url\":\"https:\/\/statorials.org\/pt\/\",\"name\":\"Statorials\",\"description\":\"O seu guia para a literacia estat\u00edstica!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/pt\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"pt-PT\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666\",\"name\":\"Dr. benjamim anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"pt-PT\",\"@id\":\"https:\/\/statorials.org\/pt\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/statorials.org\/pt\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"https:\/\/statorials.org\/pt\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"caption\":\"Dr. benjamim anderson\"},\"description\":\"Ol\u00e1, sou Benjamin, um professor aposentado de estat\u00edstica que se tornou professor dedicado na Statorials. Com vasta experi\u00eancia e conhecimento na \u00e1rea de estat\u00edstica, estou empenhado em compartilhar meu conhecimento para capacitar os alunos por meio de Statorials. Saber mais\",\"sameAs\":[\"https:\/\/statorials.org\/pt\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Como criar uma legenda manual no Matplotlib (com exemplo) - Statorials","description":"Este tutorial explica como criar uma legenda manual no matplotlib, com um exemplo.","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:\/\/statorials.org\/pt\/legenda-do-manual-matplotlib\/","og_locale":"pt_PT","og_type":"article","og_title":"Como criar uma legenda manual no Matplotlib (com exemplo) - Statorials","og_description":"Este tutorial explica como criar uma legenda manual no matplotlib, com um exemplo.","og_url":"https:\/\/statorials.org\/pt\/legenda-do-manual-matplotlib\/","og_site_name":"Statorials","article_published_time":"2023-07-16T19:12:36+00:00","og_image":[{"url":"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/manuel1.jpg"}],"author":"Dr. benjamim anderson","twitter_card":"summary_large_image","twitter_misc":{"Escrito por":"Dr. benjamim anderson","Tempo estimado de leitura":"2 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/pt\/legenda-do-manual-matplotlib\/","url":"https:\/\/statorials.org\/pt\/legenda-do-manual-matplotlib\/","name":"Como criar uma legenda manual no Matplotlib (com exemplo) - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/pt\/#website"},"datePublished":"2023-07-16T19:12:36+00:00","dateModified":"2023-07-16T19:12:36+00:00","author":{"@id":"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666"},"description":"Este tutorial explica como criar uma legenda manual no matplotlib, com um exemplo.","breadcrumb":{"@id":"https:\/\/statorials.org\/pt\/legenda-do-manual-matplotlib\/#breadcrumb"},"inLanguage":"pt-PT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/pt\/legenda-do-manual-matplotlib\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/pt\/legenda-do-manual-matplotlib\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Lar","item":"https:\/\/statorials.org\/pt\/"},{"@type":"ListItem","position":2,"name":"Como criar uma legenda manual no matplotlib (com exemplo)"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/pt\/#website","url":"https:\/\/statorials.org\/pt\/","name":"Statorials","description":"O seu guia para a literacia estat\u00edstica!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/pt\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"pt-PT"},{"@type":"Person","@id":"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666","name":"Dr. benjamim anderson","image":{"@type":"ImageObject","inLanguage":"pt-PT","@id":"https:\/\/statorials.org\/pt\/#\/schema\/person\/image\/","url":"https:\/\/statorials.org\/pt\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","contentUrl":"https:\/\/statorials.org\/pt\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","caption":"Dr. benjamim anderson"},"description":"Ol\u00e1, sou Benjamin, um professor aposentado de estat\u00edstica que se tornou professor dedicado na Statorials. Com vasta experi\u00eancia e conhecimento na \u00e1rea de estat\u00edstica, estou empenhado em compartilhar meu conhecimento para capacitar os alunos por meio de Statorials. Saber mais","sameAs":["https:\/\/statorials.org\/pt"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/posts\/3568","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/comments?post=3568"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/posts\/3568\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/media?parent=3568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/categories?post=3568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/tags?post=3568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}