{"id":3861,"date":"2023-07-15T03:11:10","date_gmt":"2023-07-15T03:11:10","guid":{"rendered":"https:\/\/statorials.org\/it\/barplot-a-colori-seaborn\/"},"modified":"2023-07-15T03:11:10","modified_gmt":"2023-07-15T03:11:10","slug":"barplot-a-colori-seaborn","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/barplot-a-colori-seaborn\/","title":{"rendered":"Come impostare il colore delle barre in un barplot seaborn"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">\u00c8 possibile utilizzare i seguenti metodi per impostare il colore delle barre in un grafico a barre <a href=\"https:\/\/seaborn.pydata.org\/\" target=\"_blank\" rel=\"noopener\">marino<\/a> :<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Metodo 1: imposta il colore per tutte le barre<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#use steelblue for the color of all bars<\/span>\nsns. <span style=\"color: #3366ff;\">barplot<\/span> (x=xvar, y=yvar, color=' <span style=\"color: #ff0000;\">steelblue<\/span> ')<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><strong>Metodo 2: imposta il colore della barra con il valore massimo<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#use orange for bar with max value and gray for all other bars\n<\/span>cols = [' <span style=\"color: #ff0000;\">gray<\/span> ' <span style=\"color: #008000;\">if<\/span> (x &lt; <span style=\"color: #008000;\">max<\/span> (df. <span style=\"color: #3366ff;\">yvar<\/span> )) <span style=\"color: #008000;\">else<\/span> ' <span style=\"color: #ff0000;\">orange<\/span> ' <span style=\"color: #008000;\">for<\/span> x <span style=\"color: #008000;\">in<\/span> df. <span style=\"color: #3366ff;\">yvar<\/span> ]\n\n<span style=\"color: #008080;\">#create barplot using specified colors\n<\/span>sns. <span style=\"color: #3366ff;\">barplot<\/span> (x=df. <span style=\"color: #3366ff;\">xvar<\/span> , y=df. <span style=\"color: #3366ff;\">yvar<\/span> , palette=cols)<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><strong>Metodo 3: imposta il colore della barra in base allo stato<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#use red for bars with value less than 10 and green for all other bars<\/span>\ncols = [' <span style=\"color: #ff0000;\">red<\/span> ' <span style=\"color: #008000;\">if<\/span> x &lt; 10 <span style=\"color: #008000;\">else<\/span> ' <span style=\"color: #ff0000;\">green<\/span> ' <span style=\"color: #008000;\">for<\/span> x <span style=\"color: #008000;\">in<\/span> df. <span style=\"color: #3366ff;\">yvar<\/span> ]\n\n<span style=\"color: #008080;\">#create barplot using specified colors\n<\/span>sns. <span style=\"color: #3366ff;\">barplot<\/span> (x=df. <span style=\"color: #3366ff;\">xvar<\/span> , y=df. <span style=\"color: #3366ff;\">yvar<\/span> , palette=cols)<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">I seguenti esempi mostrano come utilizzare ciascun metodo nella pratica con i seguenti DataFrame panda:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\"><span style=\"color: #000000;\"><span style=\"color: #008000;\">import<\/span> pandas <span style=\"color: #008000;\">as<\/span> pd\n\n<span style=\"color: #008080;\">#createDataFrame\n<\/span>df = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">employee<\/span> ': ['Andy', 'Bert', 'Chad', 'Doug', 'Eric', 'Frank'],\n                   ' <span style=\"color: #ff0000;\">sales<\/span> ': [22, 14, 9, 7, 29, 20]})\n\n<span style=\"color: #008080;\">#view DataFrame\n<\/span><span style=\"color: #008000;\">print<\/span> (df)\n\n  employee sales\n0 Andy 22\n1 Bert 14\n2 Chad 9\n3 Doug 7\n4 Eric 29\n5 Frank 20\n<\/span><\/span><\/strong><\/pre>\n<h2> <span style=\"color: #000000;\"><strong>Esempio 1: imposta il colore per tutte le barre<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come creare un grafico a barre in Seaborn e utilizzare il colore &#8220;steelblue&#8221; per tutte le barre del grafico:<\/span> <\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">import<\/span> seaborn <span style=\"color: #008000;\">as<\/span> sns\n\n<span style=\"color: #008080;\">#create barplot using steelblue as color for each bar\n<\/span>sns. <span style=\"color: #3366ff;\">barplot<\/span> (x= <span style=\"color: #3366ff;\">df.employee<\/span> ,y= <span style=\"color: #3366ff;\">df.sales<\/span> ,color=' <span style=\"color: #ff0000;\">steelblue<\/span> ')\n<\/strong><\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\" wp-image-31177 aligncenter\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/couleur1.jpg\" alt=\"\" width=\"528\" height=\"409\" srcset=\"\" sizes=\"\"><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Esempio 2: imposta il colore della barra con il valore massimo<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come utilizzare l&#8217;arancione per la barra con il valore massimo nel barplot e il grigio per tutte le altre barre:<\/span> <\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">import<\/span> seaborn <span style=\"color: #008000;\">as<\/span> sns\n\n<span style=\"color: #008080;\">#use orange for bar with max value and gray for all other bars\n<\/span>cols = [' <span style=\"color: #ff0000;\">gray<\/span> ' <span style=\"color: #008000;\">if<\/span> (x &lt; <span style=\"color: #008000;\">max<\/span> (df. <span style=\"color: #3366ff;\">sales<\/span> )) <span style=\"color: #008000;\">else<\/span> ' <span style=\"color: #ff0000;\">orange<\/span> ' <span style=\"color: #008000;\">for<\/span> x <span style=\"color: #008000;\">in<\/span> df. <span style=\"color: #3366ff;\">dirty<\/span> ]\n\n<span style=\"color: #008080;\">#create barplot with custom colors\n<\/span>sns. <span style=\"color: #3366ff;\">barplot<\/span> (x=df. <span style=\"color: #3366ff;\">employee<\/span> , y=df. <span style=\"color: #3366ff;\">sales<\/span> , palette=cols)<\/strong> <\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-31178\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/couleur2.jpg\" alt=\"barplot seaborn imposta il colore della barra con il valore massimo\" width=\"528\" height=\"407\" srcset=\"\" sizes=\"\"><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Esempio 3: imposta il colore della barra con il valore massimo<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come utilizzare l&#8217;arancione per la barra con il valore massimo nel barplot e il grigio per tutte le altre barre:<\/span> <\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">import<\/span> seaborn <span style=\"color: #008000;\">as<\/span> sns\n\n<span style=\"color: #008080;\">#use red for bars with value less than 10 and green for all other bars\n<\/span>cols = [' <span style=\"color: #ff0000;\">red<\/span> ' <span style=\"color: #008000;\">if<\/span> x &lt; 10 <span style=\"color: #008000;\">else<\/span> ' <span style=\"color: #ff0000;\">green<\/span> ' <span style=\"color: #008000;\">for<\/span> x <span style=\"color: #008000;\">in<\/span> df. <span style=\"color: #3366ff;\">dirty<\/span> ]\n\n<span style=\"color: #008080;\">#create barplot with custom colors\n<\/span>sns. <span style=\"color: #3366ff;\">barplot<\/span> (x=df. <span style=\"color: #3366ff;\">employee<\/span> , y=df. <span style=\"color: #3366ff;\">sales<\/span> , palette=cols)<\/strong> <\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-31179\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/couleur3.jpg\" alt=\"Trama della barra Seaborn con colori in base alle condizioni\" width=\"537\" height=\"414\" srcset=\"\" sizes=\"\"><\/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 funzioni comuni in Seaborn:<\/span><\/p>\n<p> Come creare un barplot raggruppato in Seaborn<br \/> <a href=\"https:\/\/statorials.org\/it\/camembert-marino\/\" target=\"_blank\" rel=\"noopener\">Come creare un grafico a torta in Seaborn<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/multi-appezzamenti-seaborn\/\" target=\"_blank\" rel=\"noopener\">Come creare pi\u00f9 grafici Seaborn in un&#8217;unica figura<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00c8 possibile utilizzare i seguenti metodi per impostare il colore delle barre in un grafico a barre marino : Metodo 1: imposta il colore per tutte le barre #use steelblue for the color of all bars sns. barplot (x=xvar, y=yvar, color=&#8217; steelblue &#8216;) Metodo 2: imposta il colore della barra con il valore massimo #use [&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 impostare il colore delle barre in un Barplot Seaborn - Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come impostare il colore delle barre in un barplot marino, 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\/barplot-a-colori-seaborn\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come impostare il colore delle barre in un Barplot Seaborn - Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come impostare il colore delle barre in un barplot marino, con diversi esempi.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/barplot-a-colori-seaborn\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-15T03:11:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/couleur1.jpg\" \/>\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\/barplot-a-colori-seaborn\/\",\"url\":\"https:\/\/statorials.org\/it\/barplot-a-colori-seaborn\/\",\"name\":\"Come impostare il colore delle barre in un Barplot Seaborn - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-15T03:11:10+00:00\",\"dateModified\":\"2023-07-15T03:11:10+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come impostare il colore delle barre in un barplot marino, con diversi esempi.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/barplot-a-colori-seaborn\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/barplot-a-colori-seaborn\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/barplot-a-colori-seaborn\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come impostare il colore delle barre in un barplot seaborn\"}]},{\"@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 impostare il colore delle barre in un Barplot Seaborn - Statorials","description":"Questo tutorial spiega come impostare il colore delle barre in un barplot marino, 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\/barplot-a-colori-seaborn\/","og_locale":"it_IT","og_type":"article","og_title":"Come impostare il colore delle barre in un Barplot Seaborn - Statorials","og_description":"Questo tutorial spiega come impostare il colore delle barre in un barplot marino, con diversi esempi.","og_url":"https:\/\/statorials.org\/it\/barplot-a-colori-seaborn\/","og_site_name":"Statorials","article_published_time":"2023-07-15T03:11:10+00:00","og_image":[{"url":"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/couleur1.jpg"}],"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\/barplot-a-colori-seaborn\/","url":"https:\/\/statorials.org\/it\/barplot-a-colori-seaborn\/","name":"Come impostare il colore delle barre in un Barplot Seaborn - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-15T03:11:10+00:00","dateModified":"2023-07-15T03:11:10+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come impostare il colore delle barre in un barplot marino, con diversi esempi.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/barplot-a-colori-seaborn\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/barplot-a-colori-seaborn\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/barplot-a-colori-seaborn\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come impostare il colore delle barre in un barplot seaborn"}]},{"@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\/3861"}],"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=3861"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/3861\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=3861"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=3861"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=3861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}