{"id":998,"date":"2023-07-28T01:23:17","date_gmt":"2023-07-28T01:23:17","guid":{"rendered":"https:\/\/statorials.org\/it\/grafico-a-barre-in-pila-matplotlib\/"},"modified":"2023-07-28T01:23:17","modified_gmt":"2023-07-28T01:23:17","slug":"grafico-a-barre-in-pila-matplotlib","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/grafico-a-barre-in-pila-matplotlib\/","title":{"rendered":"Come creare grafici a barre in pila in matplotlib (con esempi)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Un <strong>grafico a barre in pila<\/strong> \u00e8 un tipo di grafico che utilizza le barre per visualizzare le frequenze di diverse categorie.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Possiamo creare questo tipo di grafico in Matplotlib utilizzando la funzione <a href=\"https:\/\/matplotlib.org\/stable\/api\/_as_gen\/matplotlib.pyplot.bar.html\" target=\"_blank\" rel=\"noopener noreferrer\">matplotlib.pyplot.bar()<\/a> .<\/span><\/p>\n<p> <span style=\"color: #000000;\">Questo tutorial mostra come utilizzare questa funzione nella pratica.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Crea un grafico a barre in pila di base<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come creare un grafico a barre in pila per visualizzare le vendite totali di due prodotti in quattro trimestri di vendita diversi:<\/span> <\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #107d3f;\">import<\/span> numpy <span style=\"color: #107d3f;\">as<\/span> np\n<span style=\"color: #107d3f;\">import<\/span> matplotlib.pyplot <span style=\"color: #107d3f;\">as<\/span> plt\n\n<span style=\"color: #008080;\">#createdata<\/span>\nquarter = ['Q1', 'Q2', 'Q3', 'Q4']\nproduct_A = [14, 17, 12, 9]\nproduct_B = [7, 15, 24, 18]\n\n<span style=\"color: #008080;\">#define chart parameters\n<\/span>N = 4 \nbarWidth = .5\nxloc = np. <span style=\"color: #3366ff;\">orange<\/span> (N)\n\n<span style=\"color: #008080;\">#display stacked bar chart\n<\/span>p1 = plt. <span style=\"color: #3366ff;\">bar<\/span> (xloc, product_A, width=barWidth)\np2 = plt. <span style=\"color: #3366ff;\">bar<\/span> (xloc, product_B, bottom=product_A, width=barWidth)\nplt. <span style=\"color: #3366ff;\">show<\/span> ()\n<\/strong><\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-10353 \" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/stackedbar1.png\" alt=\"Grafico a barre in pila in Matplotlib\" width=\"381\" height=\"258\" srcset=\"\" sizes=\"\"><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Aggiungi un titolo, etichette e didascalia<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Possiamo anche aggiungere un titolo, etichette, segni di graduazione e legenda per rendere il grafico pi\u00f9 facile da leggere:<\/span> <\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #107d3f;\">import<\/span> numpy <span style=\"color: #107d3f;\">as<\/span> np\n<span style=\"color: #107d3f;\">import<\/span> matplotlib.pyplot <span style=\"color: #107d3f;\">as<\/span> plt\n\n<span style=\"color: #008080;\">#create data for two teams<\/span>\nquarter = ['Q1', 'Q2', 'Q3', 'Q4']\nproduct_A = [14, 17, 12, 9]\nproduct_B = [7, 15, 24, 18]\n\n<span style=\"color: #008080;\">#define chart parameters\n<\/span>N = 4 \nbarWidth = .5\nxloc = np. <span style=\"color: #3366ff;\">orange<\/span> (N)\n\n<span style=\"color: #008080;\">#create stacked bar chart\n<\/span>p1 = plt. <span style=\"color: #3366ff;\">bar<\/span> (xloc, product_A, width=barWidth)\np2 = plt. <span style=\"color: #3366ff;\">bar<\/span> (xloc, product_B, bottom=product_A, width=barWidth)\n\n<span style=\"color: #008080;\">#add labels, title, tick marks, and legend\n<\/span>plt. <span style=\"color: #3366ff;\">ylabel<\/span> ('Sales')\nplt. <span style=\"color: #3366ff;\">xlabel<\/span> ('Quarter')\nplt. <span style=\"color: #3366ff;\">title<\/span> ('Sales by Product &amp; Quarter')\nplt. <span style=\"color: #3366ff;\">xticks<\/span> (xloc, ('Q1', 'Q2', 'Q3', 'Q4'))\nplt. <span style=\"color: #3366ff;\">yticks<\/span> (np. <span style=\"color: #3366ff;\">arange<\/span> (0, 41, 5))\nplt. <span style=\"color: #3366ff;\">legend<\/span> ((p1[0], p2[0]), ('A', 'B'))\n\n<span style=\"color: #008080;\">#displaychart\n<\/span>plt. <span style=\"color: #3366ff;\">show<\/span> ()<\/strong> <\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-10354 \" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/stackedbar2.png\" alt=\"Grafico a barre in pila in Matplotlib\" width=\"400\" height=\"285\" srcset=\"\" sizes=\"\"><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Personalizza i colori del grafico<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Infine, possiamo personalizzare i colori utilizzati nel grafico con l&#8217;argomento <strong>colori()<\/strong> in <strong>plt.bar()<\/strong> :<\/span> <\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #107d3f;\">import<\/span> numpy <span style=\"color: #107d3f;\">as<\/span> np\n<span style=\"color: #107d3f;\">import<\/span> matplotlib.pyplot <span style=\"color: #107d3f;\">as<\/span> plt\n\n<span style=\"color: #008080;\">#create data for two teams<\/span>\nquarter = ['Q1', 'Q2', 'Q3', 'Q4']\nproduct_A = [14, 17, 12, 9]\nproduct_B = [7, 15, 24, 18]\n\n<span style=\"color: #008080;\">#define chart parameters\n<\/span>N = 4 \nbarWidth = .5\nxloc = np. <span style=\"color: #3366ff;\">orange<\/span> (N)\n\n<span style=\"color: #008080;\">#create stacked bar chart\n<\/span>p1 = plt. <span style=\"color: #3366ff;\">bar<\/span> (xloc, product_A, width=barWidth, color=' <span style=\"color: #008000;\">springgreen<\/span> ')\np2 = plt. <span style=\"color: #3366ff;\">bar<\/span> (xloc, product_B, bottom=product_A, width=barWidth, color=' <span style=\"color: #008000;\">coral<\/span> ')\n\n<span style=\"color: #008080;\">#add labels, title, tick marks, and legend\n<\/span>plt. <span style=\"color: #3366ff;\">ylabel<\/span> ('Sales')\nplt. <span style=\"color: #3366ff;\">xlabel<\/span> ('Quarter')\nplt. <span style=\"color: #3366ff;\">title<\/span> ('Sales by Product &amp; Quarter')\nplt. <span style=\"color: #3366ff;\">xticks<\/span> (xloc, ('Q1', 'Q2', 'Q3', 'Q4'))\nplt. <span style=\"color: #3366ff;\">yticks<\/span> (np. <span style=\"color: #3366ff;\">arange<\/span> (0, 41, 5))\nplt. <span style=\"color: #3366ff;\">legend<\/span> ((p1[0], p2[0]), ('A', 'B'))\n\n<span style=\"color: #008080;\">#displaychart\n<\/span>plt. <span style=\"color: #3366ff;\">show<\/span> ()<\/strong> <\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-10355\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/stackedbar3.png\" alt=\"Grafico a barre in pila Matplotlib con colori personalizzati\" width=\"410\" height=\"294\" srcset=\"\" sizes=\"\"><\/p>\n<p> <span style=\"color: #000000;\">Puoi trovare un elenco completo dei colori disponibili nella <a href=\"https:\/\/matplotlib.org\/examples\/color\/named_colors.html\" target=\"_blank\" rel=\"noopener noreferrer\">documentazione<\/a> di Matplotlib.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Risorse addizionali<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">I seguenti tutorial spiegano come eseguire altre attivit\u00e0 comuni in Matplotlib:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/it\/cambia-la-dimensione-del-carattere-matplotlib\/\" target=\"_blank\" rel=\"noopener noreferrer\">Come modificare la dimensione del carattere su un grafico Matplotlib<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/matplotlib-rimuove-i-segni-di-spunta\/\" target=\"_blank\" rel=\"noopener noreferrer\">Come rimuovere i segni di spunta dai grafici Matplotlib<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/griglia-di-visualizzazione-matplotlib\/\" target=\"_blank\" rel=\"noopener noreferrer\">Come mostrare le griglie sui grafici Matplotlib<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Un grafico a barre in pila \u00e8 un tipo di grafico che utilizza le barre per visualizzare le frequenze di diverse categorie. Possiamo creare questo tipo di grafico in Matplotlib utilizzando la funzione matplotlib.pyplot.bar() . Questo tutorial mostra come utilizzare questa funzione nella pratica. Crea un grafico a barre in pila di base Il codice [&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":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Come creare grafici a barre in pila in Matplotlib (con esempi) - Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come creare grafici a barre in pila in Matplotlib, con diversi esempi.\" \/>\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\/it\/grafico-a-barre-in-pila-matplotlib\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come creare grafici a barre in pila in Matplotlib (con esempi) - Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come creare grafici a barre in pila in Matplotlib, con diversi esempi.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/grafico-a-barre-in-pila-matplotlib\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-28T01:23:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/stackedbar1.png\" \/>\n<meta name=\"author\" content=\"Benjamin anderson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Benjamin anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minuti\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/it\/grafico-a-barre-in-pila-matplotlib\/\",\"url\":\"https:\/\/statorials.org\/it\/grafico-a-barre-in-pila-matplotlib\/\",\"name\":\"Come creare grafici a barre in pila in Matplotlib (con esempi) - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-28T01:23:17+00:00\",\"dateModified\":\"2023-07-28T01:23:17+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come creare grafici a barre in pila in Matplotlib, con diversi esempi.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/grafico-a-barre-in-pila-matplotlib\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/grafico-a-barre-in-pila-matplotlib\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/grafico-a-barre-in-pila-matplotlib\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come creare grafici a barre in pila in matplotlib (con esempi)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/it\/#website\",\"url\":\"https:\/\/statorials.org\/it\/\",\"name\":\"Statorials\",\"description\":\"La tua guida all&#039;alfabetizzazione statistica!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/it\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"it-IT\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\",\"name\":\"Benjamin anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"it-IT\",\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"caption\":\"Benjamin anderson\"},\"description\":\"Ciao, sono Benjamin, un professore di statistica in pensione diventato insegnante dedicato di Statorials. Con una vasta esperienza e competenza nel campo della statistica, sono ansioso di condividere le mie conoscenze per potenziare gli studenti attraverso Statorials. Scopri di pi\u00f9\",\"sameAs\":[\"https:\/\/statorials.org\/it\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Come creare grafici a barre in pila in Matplotlib (con esempi) - Statorials","description":"Questo tutorial spiega come creare grafici a barre in pila in Matplotlib, con diversi esempi.","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\/it\/grafico-a-barre-in-pila-matplotlib\/","og_locale":"it_IT","og_type":"article","og_title":"Come creare grafici a barre in pila in Matplotlib (con esempi) - Statorials","og_description":"Questo tutorial spiega come creare grafici a barre in pila in Matplotlib, con diversi esempi.","og_url":"https:\/\/statorials.org\/it\/grafico-a-barre-in-pila-matplotlib\/","og_site_name":"Statorials","article_published_time":"2023-07-28T01:23:17+00:00","og_image":[{"url":"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/stackedbar1.png"}],"author":"Benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Benjamin anderson","Est. reading time":"2 minuti"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/it\/grafico-a-barre-in-pila-matplotlib\/","url":"https:\/\/statorials.org\/it\/grafico-a-barre-in-pila-matplotlib\/","name":"Come creare grafici a barre in pila in Matplotlib (con esempi) - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-28T01:23:17+00:00","dateModified":"2023-07-28T01:23:17+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come creare grafici a barre in pila in Matplotlib, con diversi esempi.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/grafico-a-barre-in-pila-matplotlib\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/grafico-a-barre-in-pila-matplotlib\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/grafico-a-barre-in-pila-matplotlib\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come creare grafici a barre in pila in matplotlib (con esempi)"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/it\/#website","url":"https:\/\/statorials.org\/it\/","name":"Statorials","description":"La tua guida all&#039;alfabetizzazione statistica!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/it\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"it-IT"},{"@type":"Person","@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae","name":"Benjamin anderson","image":{"@type":"ImageObject","inLanguage":"it-IT","@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/image\/","url":"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","contentUrl":"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","caption":"Benjamin anderson"},"description":"Ciao, sono Benjamin, un professore di statistica in pensione diventato insegnante dedicato di Statorials. Con una vasta esperienza e competenza nel campo della statistica, sono ansioso di condividere le mie conoscenze per potenziare gli studenti attraverso Statorials. Scopri di pi\u00f9","sameAs":["https:\/\/statorials.org\/it"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/998"}],"collection":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/comments?post=998"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/998\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}