{"id":12981,"date":"2024-08-15T15:36:36","date_gmt":"2024-08-15T18:36:36","guid":{"rendered":"https:\/\/king.host\/blog\/?p=12981"},"modified":"2025-06-11T10:44:23","modified_gmt":"2025-06-11T13:44:23","slug":"visualizacao-de-dados-matplotlib","status":"publish","type":"post","link":"https:\/\/king.host\/blog\/tecnologia\/visualizacao-de-dados-matplotlib\/","title":{"rendered":"Matplotlib \u2014 Como Usar essa Ferramenta de Visualiza\u00e7\u00e3o de Dados em Python?"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">A visualiza\u00e7\u00e3o de dados em Python tem se tornado cada vez mais essencial para an\u00e1lise e compreens\u00e3o de informa\u00e7\u00f5es em diversas \u00e1reas, desde neg\u00f3cios at\u00e9 ci\u00eancia de dados. Ferramentas como o Matplotlib s\u00e3o extremamente populares, j\u00e1 que permitem a cria\u00e7\u00e3o de gr\u00e1ficos ricos e informativos com poucos comandos.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Quer saber mais sobre o assunto? Ent\u00e3o continue a leitura para conferir:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">o que \u00e9 Matplotlib;<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">como instalar e configurar o Matplotlib;<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">quais s\u00e3o os tipos de gr\u00e1ficos Matplotlib;<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">como personalizar os gr\u00e1ficos com Matplotlib.<\/span><\/li>\n<\/ul>\n<h2><b>O que \u00e9 Matplotlib?<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">O Matplotlib \u00e9 uma biblioteca de visualiza\u00e7\u00e3o de dados para <\/span><a href=\"https:\/\/king.host\/blog\/tecnologia\/python\/\"><span style=\"font-weight: 400;\">programa\u00e7\u00e3o Python<\/span><\/a><span style=\"font-weight: 400;\">, amplamente utilizada para a cria\u00e7\u00e3o de gr\u00e1ficos em 2D. Sua popularidade se deve \u00e0 simplicidade de uso e \u00e0 sua ampla gama de funcionalidades, que permitem a cria\u00e7\u00e3o de gr\u00e1ficos de linha, barras, pizza, histogramas, dispers\u00e3o e muito mais.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Al\u00e9m disso, ele \u00e9 extremamente flex\u00edvel, possibilitando a personaliza\u00e7\u00e3o completa dos gr\u00e1ficos. A biblioteca tamb\u00e9m \u00e9 bastante compat\u00edvel com outras ferramentas e <\/span><a href=\"https:\/\/king.host\/blog\/casos-de-sucesso\/falando-sobre-python-com-henrique-bastos\/\"><span style=\"font-weight: 400;\">pacotes de Python<\/span><\/a><span style=\"font-weight: 400;\">, como NumPy e Pandas, o que facilita o uso de grandes conjuntos de dados.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Seu m\u00f3dulo PyPlot fornece uma interface de alto n\u00edvel para a cria\u00e7\u00e3o de gr\u00e1ficos de maneira r\u00e1pida, fazendo com que seja uma das primeiras escolhas para quem est\u00e1 come\u00e7ando a explorar a visualiza\u00e7\u00e3o de dados em Python.<\/span><\/p>\n<h2><b>Como instalar e configurar o Matplotlib?<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">O Matplotlib pode ser facilmente instalado em qualquer ambiente Python. Para quem utiliza a distribui\u00e7\u00e3o Anaconda, a biblioteca j\u00e1 vem inclu\u00edda, facilitando ainda mais o processo. Caso contr\u00e1rio, \u00e9 poss\u00edvel instal\u00e1-lo manualmente com o seguinte comando:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">pip install matplotlib<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Depois de instalado, voc\u00ea pode importar o m\u00f3dulo PyPlot e come\u00e7ar a gerar gr\u00e1ficos com algumas poucas linhas de c\u00f3digo. Um exemplo b\u00e1sico para criar um gr\u00e1fico de linha seria:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">import matplotlib.pyplot as plt<\/span><\/p>\n<p><span style=\"font-weight: 400;\">x = [1, 2, 3]<\/span><\/p>\n<p><span style=\"font-weight: 400;\">y = [2, 4, 6]<\/span><\/p>\n<p><span style=\"font-weight: 400;\">plt.plot(x, y)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">plt.show()<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Esse simples c\u00f3digo gera um gr\u00e1fico de linha que exibe os dados fornecidos. A partir da\u00ed, \u00e9 poss\u00edvel adicionar mais personaliza\u00e7\u00f5es, como r\u00f3tulos, t\u00edtulos, cores e muito mais.<\/span><\/p>\n<h2><b>Quais s\u00e3o os tipos de gr\u00e1ficos Matplotlib?<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">O Matplotlib suporta uma grande variedade de gr\u00e1ficos, cada um com seu prop\u00f3sito espec\u00edfico. Confira os principais tipos que podem ser utilizados para visualizar seus dados de forma clara e eficiente:<\/span><\/p>\n<h3><b>Gr\u00e1ficos de linha<\/b><\/h3>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-12999 size-full\" src=\"https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/03\/imagem02.png\" alt=\"\" width=\"404\" height=\"284\" title=\"\" srcset=\"https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/03\/imagem02.png 404w, https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/03\/imagem02-300x211.png 300w, https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/03\/imagem02-400x281.png 400w\" sizes=\"(max-width: 404px) 100vw, 404px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">Os gr\u00e1ficos de linha s\u00e3o uma das formas mais comuns de visualizar dados em s\u00e9ries temporais ou rela\u00e7\u00f5es cont\u00ednuas. No Matplotlib, \u00e9 poss\u00edvel cri\u00e1-los com o comando plt.plot(). Um exemplo de gr\u00e1fico de linha seria:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">x = [1, 2, 3, 4]<\/span><\/p>\n<p><span style=\"font-weight: 400;\">y = [1, 4, 9, 16]<\/span><\/p>\n<p><span style=\"font-weight: 400;\">plt.plot(x, y)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">plt.title(&#8220;Gr\u00e1fico de Linha&#8221;)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">plt.xlabel(&#8220;Eixo X&#8221;)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">plt.ylabel(&#8220;Eixo Y&#8221;)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">plt.show()<\/span><\/p>\n<h3><b>Gr\u00e1ficos de barras<\/b><\/h3>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-13000 size-full\" src=\"https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/03\/imagem03.png\" alt=\"\" width=\"374\" height=\"251\" title=\"\" srcset=\"https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/03\/imagem03.png 374w, https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/03\/imagem03-300x201.png 300w\" sizes=\"(max-width: 374px) 100vw, 374px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">Para comparar diferentes categorias, os gr\u00e1ficos de barras s\u00e3o uma excelente escolha. Com o Matplotlib, voc\u00ea pode criar op\u00e7\u00f5es verticais e horizontais. Confira um exemplo:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">x = [&#8216;A&#8217;, &#8216;B&#8217;, &#8216;C&#8217;]<\/span><\/p>\n<p><span style=\"font-weight: 400;\">y = [3, 7, 2]<\/span><\/p>\n<p><span style=\"font-weight: 400;\">plt.bar(x, y, color=&#8217;blue&#8217;)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">plt.title(&#8220;Gr\u00e1fico de Barras&#8221;)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">plt.show()<\/span><\/p>\n<h3><b>Gr\u00e1ficos de pizza<\/b><\/h3>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-13001 size-full\" src=\"https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/03\/imagem04.png\" alt=\"\" width=\"341\" height=\"212\" title=\"\" srcset=\"https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/03\/imagem04.png 341w, https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/03\/imagem04-300x187.png 300w\" sizes=\"(max-width: 341px) 100vw, 341px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">Os gr\u00e1ficos de pizza s\u00e3o usados para mostrar propor\u00e7\u00f5es em um conjunto de dados. No Matplotlib, o comando plt.pie() facilita a cria\u00e7\u00e3o desse tipo de gr\u00e1fico:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">fatias = [35, 25, 40]<\/span><\/p>\n<p><span style=\"font-weight: 400;\">atividades = [&#8216;Dormir&#8217;, &#8216;Trabalhar&#8217;, &#8216;Lazer&#8217;]<\/span><\/p>\n<p><span style=\"font-weight: 400;\">plt.pie(fatias, labels=atividades, autopct=&#8217;%1.1f%%&#8217;, startangle=90)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">plt.title(&#8220;Gr\u00e1fico de Pizza&#8221;)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">plt.show()<\/span><\/p>\n<h3><b>Histogramas<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Os histogramas s\u00e3o usados para mostrar a distribui\u00e7\u00e3o de um conjunto de dados. No Matplotlib, \u00e9 poss\u00edvel cri\u00e1-los com o comando plt.hist():<\/span><\/p>\n<p><span style=\"font-weight: 400;\">dados = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5]<\/span><\/p>\n<p><span style=\"font-weight: 400;\">plt.hist(dados, bins=5, color=&#8217;green&#8217;)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">plt.title(&#8220;Histograma&#8221;)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">plt.show()<\/span><\/p>\n<p><b>Gr\u00e1ficos de dispers\u00e3o (<\/b><b><i>Scatterplot<\/i><\/b><b>)<\/b><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-13002 size-full\" src=\"https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/03\/imagem05.png\" alt=\"\" width=\"369\" height=\"252\" title=\"\" srcset=\"https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/03\/imagem05.png 369w, https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/03\/imagem05-300x205.png 300w\" sizes=\"(max-width: 369px) 100vw, 369px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">Os gr\u00e1ficos de dispers\u00e3o s\u00e3o usados para mostrar a rela\u00e7\u00e3o entre duas vari\u00e1veis e podem ser criados com plt.scatter():<\/span><\/p>\n<p><span style=\"font-weight: 400;\">x = [1, 2, 3, 4, 5]<\/span><\/p>\n<p><span style=\"font-weight: 400;\">y = [5, 2, 4, 6, 8]<\/span><\/p>\n<p><span style=\"font-weight: 400;\">plt.scatter(x, y, color=&#8217;red&#8217;)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">plt.title(&#8220;Gr\u00e1fico de Dispers\u00e3o&#8221;)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">plt.show()<\/span><\/p>\n<p><b><i>Leia tamb\u00e9m: <\/i><\/b><a href=\"https:\/\/king.host\/blog\/tecnologia\/graficos-3d-em-python\/\"><b><i>Gr\u00e1ficos 3D em Python: veja tr\u00eas exemplos de visualiza\u00e7\u00f5es<\/i><\/b><\/a><\/p>\n<h2><b>Como personalizar os gr\u00e1ficos com Matplotlib?<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">Um dos grandes diferenciais do Matplotlib \u00e9 a sua capacidade de personaliza\u00e7\u00e3o. \u00c9 poss\u00edvel ajustar praticamente todos os aspectos visuais dos gr\u00e1ficos para torn\u00e1-los mais informativos e visualmente agrad\u00e1veis. Confira:<\/span><\/p>\n<h3><b>T\u00edtulos e r\u00f3tulos<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Adicionar t\u00edtulos e r\u00f3tulos aos eixos \u00e9 essencial para que o gr\u00e1fico seja compreendido. Use os comandos plt.title(), plt.xlabel() e plt.ylabel() para isso.<\/span><\/p>\n<h3><b>Legendas<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Em gr\u00e1ficos com m\u00faltiplas linhas ou barras, as legendas ajudam a identificar cada conjunto de dados. Use plt.legend() para adicionar legendas.<\/span><\/p>\n<h3><b>Cores e estilos<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">O Matplotlib permite escolher cores, estilos de linha e marcadores. Por exemplo, color=&#8217;red&#8217; ou marker=&#8217;o&#8217; podem ser usados para modificar o estilo dos gr\u00e1ficos.<\/span><\/p>\n<p><b><i>Leia tamb\u00e9m: <\/i><\/b><a href=\"https:\/\/king.host\/blog\/tecnologia\/aplicacao-web\/\"><b><i>O que \u00e9 uma aplica\u00e7\u00e3o web? Entenda como funciona!<\/i><\/b><\/a><\/p>\n<p><span style=\"font-weight: 400;\">Como vimos at\u00e9 aqui, a visualiza\u00e7\u00e3o de dados em Python \u00e9 fundamental para transformar n\u00fameros e informa\u00e7\u00f5es complexas em representa\u00e7\u00f5es gr\u00e1ficas compreens\u00edveis. Isso faz com que o Matplotlib seja uma ferramenta indispens\u00e1vel, permitindo criar gr\u00e1ficos variados com facilidade.<\/span><\/p>\n<p><a href=\"https:\/\/king.host\/blog\/empreendedorismo\/palestras-online-conexao-kinghost\/\"><span style=\"font-weight: 400;\">Continue aprofundando seus conhecimentos com os recursos gratuitos do Conex\u00e3o KingHost<\/span><\/a><span style=\"font-weight: 400;\">! L\u00e1 voc\u00ea encontra tudo o que precisa para impulsionar sua carreira no digital.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Descubra o poder do Matplotlib para a visualiza\u00e7\u00e3o de dados em Python e aprenda sobre os principais tipos de gr\u00e1ficos da ferramenta.<\/p>\n","protected":false},"author":277,"featured_media":13005,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1179,8],"tags":[1357],"class_list":["post-12981","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-desenvolvimento","category-tecnologia","tag-python"],"_links":{"self":[{"href":"https:\/\/king.host\/blog\/wp-json\/wp\/v2\/posts\/12981","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/king.host\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/king.host\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/king.host\/blog\/wp-json\/wp\/v2\/users\/277"}],"replies":[{"embeddable":true,"href":"https:\/\/king.host\/blog\/wp-json\/wp\/v2\/comments?post=12981"}],"version-history":[{"count":16,"href":"https:\/\/king.host\/blog\/wp-json\/wp\/v2\/posts\/12981\/revisions"}],"predecessor-version":[{"id":41809,"href":"https:\/\/king.host\/blog\/wp-json\/wp\/v2\/posts\/12981\/revisions\/41809"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/king.host\/blog\/wp-json\/wp\/v2\/media\/13005"}],"wp:attachment":[{"href":"https:\/\/king.host\/blog\/wp-json\/wp\/v2\/media?parent=12981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/king.host\/blog\/wp-json\/wp\/v2\/categories?post=12981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/king.host\/blog\/wp-json\/wp\/v2\/tags?post=12981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}