{"id":909,"date":"2023-07-28T08:34:13","date_gmt":"2023-07-28T08:34:13","guid":{"rendered":"https:\/\/statorials.org\/it\/barre-di-errore-python\/"},"modified":"2023-07-28T08:34:13","modified_gmt":"2023-07-28T08:34:13","slug":"barre-di-errore-python","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/barre-di-errore-python\/","title":{"rendered":"Come aggiungere barre di errore ai grafici in python"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Spesso potresti voler aggiungere <strong>barre di errore<\/strong> ai grafici in Python per catturare l&#8217;incertezza attorno alle misurazioni o ai valori calcolati. Fortunatamente, questo \u00e8 facile da fare utilizzando la libreria matplotlib.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Questo tutorial spiega come aggiungere barre di errore ai grafici a barre e ai grafici a linee in Python.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Barre di errore nei grafici a barre<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Supponiamo di avere il seguente set di dati di 10 valori in Python:<\/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;\">#define dataset<\/span>\ndata = [4, 6, 6, 8, 9, 14, 16, 16, 17, 20]<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Per creare un grafico a barre con barre di errore per questo set di dati, possiamo impostare la larghezza delle barre di errore come <strong>errore standard<\/strong> , che viene calcolato a<\/span><strong><br \/><\/strong><\/p>\n<p> <span style=\"color: #000000;\"><strong>Errore standard = s \/ \u221an<\/strong><\/span><\/p>\n<p> <span style=\"color: #000000;\">Oro:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\"><strong>s:<\/strong> deviazione standard campionaria<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>n:<\/strong> dimensione del campione<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come calcolare l&#8217;errore standard per questo esempio:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#calculate standard error<\/span>\nstd_error = np.std(data, ddof=1) \/ np.sqrt(len(data))\n\n<span style=\"color: #008080;\">#view standard error<\/span>\nstd_error\n\n1.78\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Infine, possiamo creare il grafico a barre utilizzando barre di errore che hanno una larghezza pari all&#8217;errore standard:<\/span> <\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#define chart<\/span> \nfig, ax = plt.subplots()\n\n<span style=\"color: #008080;\">#create chart<\/span>\nax.bar(x=np.arange(len(data)), <span style=\"color: #008080;\">#x-coordinates of bars<\/span>\n       height=data, <span style=\"color: #008080;\">#height of bars<\/span>\n       yerr=std_error, <span style=\"color: #008080;\">#error bar width<\/span>\n       capsize=4) <span style=\"color: #008080;\">#length of error bar caps<\/span><\/strong> <\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-9718 \" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/erreur_bar_python1.png\" alt=\"Grafico a barre con barre di errore in Python\" width=\"438\" height=\"297\" srcset=\"\" sizes=\"\"><\/p>\n<p> <span style=\"color: #000000;\">L&#8217;errore standard si \u00e8 rivelato essere <strong>1,78<\/strong> . Questa \u00e8 la larghezza della barra di errore che si estende in entrambe le direzioni dalle stime puntuali sul grafico. Ad esempio, il valore della prima barra nel grafico \u00e8 4, quindi ha una barra di errore che si estende da:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\">Estremit\u00e0 inferiore: 4 \u2013 178 = <strong>2,22<\/strong><\/span><\/li>\n<li> <span style=\"color: #000000;\">Estremit\u00e0 superiore: 4 + 1,78 = <strong>5,78<\/strong><\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Ogni barra di errore nel grafico ha la stessa larghezza.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Barre di errore nei grafici a linee<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come creare un grafico a linee con barre di errore per lo stesso set di dati:<\/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;\">#define data<\/span>\ndata = [4, 6, 6, 8, 9, 14, 16, 16, 17, 20]\n\n<span style=\"color: #008080;\">#define x and y coordinates\n<\/span>x = np.arange(len(data))\ny = data\n\n<span style=\"color: #008080;\">#create line chart with error bars\n<\/span>fig, ax = plt.subplots()\n\nax.errorbar(x, y,\n            yerr=std_error,\n            capsize=4)<\/strong> <\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-9720 \" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/erreur_bar_python2.png\" alt=\"Grafico a linee con barre di errore in Python\" width=\"397\" height=\"266\" srcset=\"\" sizes=\"\"><\/p>\n<p> <span style=\"color: #000000;\">Nota che l&#8217;argomento <strong>yerr<\/strong> dice a Python di creare barre di errore verticali. Potremmo invece utilizzare barre verticali orizzontali utilizzando l&#8217;argomento <strong>xerr<\/strong> :<\/span> <\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#create line chart with horizontal error bars\n<\/span>fig, ax = plt.subplots()\n\nax.errorbar(x, y,\n            <span style=\"color: #3366ff;\">xerr<\/span> =std_error,\n            capsize=4)<\/strong> <\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-9721 \" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/erreur_bar_python3.png\" alt=\"Barre di errore nel grafico Python\" width=\"418\" height=\"289\" srcset=\"\" sizes=\"\"><\/p>\n<p> <span style=\"color: #000000;\"><em>Puoi trovare altri tutorial su Python qui .<\/em><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spesso potresti voler aggiungere barre di errore ai grafici in Python per catturare l&#8217;incertezza attorno alle misurazioni o ai valori calcolati. Fortunatamente, questo \u00e8 facile da fare utilizzando la libreria matplotlib. Questo tutorial spiega come aggiungere barre di errore ai grafici a barre e ai grafici a linee in Python. Barre di errore nei grafici [&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 aggiungere barre di errore ai grafici in Python - Statorials<\/title>\n<meta name=\"description\" content=\"Una semplice spiegazione su come aggiungere barre di errore ai grafici in Python.\" \/>\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\/barre-di-errore-python\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come aggiungere barre di errore ai grafici in Python - Statorials\" \/>\n<meta property=\"og:description\" content=\"Una semplice spiegazione su come aggiungere barre di errore ai grafici in Python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/barre-di-errore-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-28T08:34:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/erreur_bar_python1.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\/barre-di-errore-python\/\",\"url\":\"https:\/\/statorials.org\/it\/barre-di-errore-python\/\",\"name\":\"Come aggiungere barre di errore ai grafici in Python - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-28T08:34:13+00:00\",\"dateModified\":\"2023-07-28T08:34:13+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Una semplice spiegazione su come aggiungere barre di errore ai grafici in Python.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/barre-di-errore-python\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/barre-di-errore-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/barre-di-errore-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come aggiungere barre di errore ai grafici in python\"}]},{\"@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 aggiungere barre di errore ai grafici in Python - Statorials","description":"Una semplice spiegazione su come aggiungere barre di errore ai grafici in Python.","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\/barre-di-errore-python\/","og_locale":"it_IT","og_type":"article","og_title":"Come aggiungere barre di errore ai grafici in Python - Statorials","og_description":"Una semplice spiegazione su come aggiungere barre di errore ai grafici in Python.","og_url":"https:\/\/statorials.org\/it\/barre-di-errore-python\/","og_site_name":"Statorials","article_published_time":"2023-07-28T08:34:13+00:00","og_image":[{"url":"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/erreur_bar_python1.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\/barre-di-errore-python\/","url":"https:\/\/statorials.org\/it\/barre-di-errore-python\/","name":"Come aggiungere barre di errore ai grafici in Python - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-28T08:34:13+00:00","dateModified":"2023-07-28T08:34:13+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Una semplice spiegazione su come aggiungere barre di errore ai grafici in Python.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/barre-di-errore-python\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/barre-di-errore-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/barre-di-errore-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come aggiungere barre di errore ai grafici in python"}]},{"@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\/909"}],"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=909"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/909\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=909"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=909"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=909"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}