{"id":1659,"date":"2023-07-25T11:56:30","date_gmt":"2023-07-25T11:56:30","guid":{"rendered":"https:\/\/statorials.org\/it\/serie-temporali-matplotlib\/"},"modified":"2023-07-25T11:56:30","modified_gmt":"2023-07-25T11:56:30","slug":"serie-temporali-matplotlib","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/serie-temporali-matplotlib\/","title":{"rendered":"Come tracciare una serie temporale in matplotlib (con esempi)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">\u00c8 possibile utilizzare la seguente sintassi per tracciare una serie temporale in Matplotlib:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">import<\/span> matplotlib. <span style=\"color: #3366ff;\">pyplot<\/span> <span style=\"color: #008000;\">as<\/span> plt\n\nplt. <span style=\"color: #3366ff;\">plot<\/span> (df. <span style=\"color: #3366ff;\">x<\/span> , df. <span style=\"color: #3366ff;\">y<\/span> )\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Ci\u00f2 presuppone che la variabile x appartenga alla classe <strong>datetime.datetime()<\/strong> .<\/span><\/p>\n<p> <span style=\"color: #000000;\">Gli esempi seguenti mostrano come utilizzare questa sintassi per tracciare dati di serie temporali in Python.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Esempio 1: tracciare una serie temporale di base in Matplotlib<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come tracciare una serie temporale in Matplotlib che mostri le vendite totali effettuate da un&#8217;azienda per 12 giorni consecutivi:<\/span> <\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><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;\">import<\/span> datetime\n<span style=\"color: #008000;\">import<\/span> numpy <span style=\"color: #008000;\">as<\/span> np\n<span style=\"color: #008000;\">import<\/span> pandas <span style=\"color: #008000;\">as<\/span> pd\n\n<span style=\"color: #008080;\">#define data\n<\/span>df = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">date<\/span> ': np. <span style=\"color: #3366ff;\">array<\/span> ([datetime. <span style=\"color: #3366ff;\">datetime<\/span> (2020, 1, i+1)\n<span style=\"color: #008000;\">for<\/span> i <span style=\"color: #008000;\">in<\/span> range(12)]),\n                   ' <span style=\"color: #ff0000;\">sales<\/span> ': [3, 4, 4, 7, 8, 9, 14, 17, 12, 8, 8, 13]})\n\n<span style=\"color: #008080;\">#plot time series\n<\/span>plt. <span style=\"color: #3366ff;\">plot<\/span> (df. <span style=\"color: #3366ff;\">date<\/span> , df. <span style=\"color: #3366ff;\">sales<\/span> , linewidth= <span style=\"color: #008000;\">3<\/span> )<\/strong> <\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\" wp-image-16349 aligncenter\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/heure1.png\" alt=\"\" width=\"556\" height=\"388\" srcset=\"\" sizes=\"\"><\/p>\n<p> <span style=\"color: #000000;\">L&#8217;asse X mostra la data e l&#8217;asse Y mostra le vendite totali in ciascuna data.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Esempio 2: personalizzare le etichette del titolo e degli assi<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">\u00c8 possibile utilizzare il codice seguente per aggiungere un titolo e le etichette degli assi alla trama:<\/span> <\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><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;\">import<\/span> datetime\n<span style=\"color: #008000;\">import<\/span> numpy <span style=\"color: #008000;\">as<\/span> np\n<span style=\"color: #008000;\">import<\/span> pandas <span style=\"color: #008000;\">as<\/span> pd\n\n<span style=\"color: #008080;\">#define data\n<\/span>df = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">date<\/span> ': np. <span style=\"color: #3366ff;\">array<\/span> ([datetime. <span style=\"color: #3366ff;\">datetime<\/span> (2020, 1, i+1)\n<span style=\"color: #008000;\">for<\/span> i <span style=\"color: #008000;\">in<\/span> range(12)]),\n                   ' <span style=\"color: #ff0000;\">sales<\/span> ': [3, 4, 4, 7, 8, 9, 14, 17, 12, 8, 8, 13]})\n\n<span style=\"color: #008080;\">#plot time series\n<\/span>plt. <span style=\"color: #3366ff;\">plot<\/span> (df. <span style=\"color: #3366ff;\">date<\/span> , df. <span style=\"color: #3366ff;\">sales<\/span> , linewidth= <span style=\"color: #008000;\">3<\/span> )\n\n<span style=\"color: #008080;\">#add title and axis labels\n<\/span>plt. <span style=\"color: #3366ff;\">title<\/span> (' <span style=\"color: #ff0000;\">Sales by Date<\/span> ')\nplt. <span style=\"color: #3366ff;\">xlabel<\/span> (' <span style=\"color: #ff0000;\">Date<\/span> ')\nplt. <span style=\"color: #3366ff;\">ylabel<\/span> (' <span style=\"color: #ff0000;\">Sales<\/span> ')\n<\/strong><\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\" wp-image-16352 aligncenter\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/heure2.png\" alt=\"\" width=\"558\" height=\"401\" srcset=\"\" sizes=\"\"><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Esempio 3: traccia pi\u00f9 serie temporali in Matplotlib<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come tracciare pi\u00f9 serie temporali in un unico grafico in Matplotlib:<\/span> <\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><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;\">import<\/span> datetime\n<span style=\"color: #008000;\">import<\/span> numpy <span style=\"color: #008000;\">as<\/span> np\n<span style=\"color: #008000;\">import<\/span> pandas <span style=\"color: #008000;\">as<\/span> pd\n\n<span style=\"color: #008080;\">#define data\n<\/span>df = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">date<\/span> ': np. <span style=\"color: #3366ff;\">array<\/span> ([datetime. <span style=\"color: #3366ff;\">datetime<\/span> (2020, 1, i+1)\n                                     <span style=\"color: #008000;\">for<\/span> i <span style=\"color: #008000;\">in<\/span> range(12)]),\n                   ' <span style=\"color: #ff0000;\">sales<\/span> ': [3, 4, 4, 7, 8, 9, 14, 17, 12, 8, 8, 13]})\n\ndf2 = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">date<\/span> ': np. <span style=\"color: #3366ff;\">array<\/span> ([datetime. <span style=\"color: #3366ff;\">datetime<\/span> (2020, 1, i+1)\n                                      <span style=\"color: #008000;\">for<\/span> i <span style=\"color: #008000;\">in<\/span> range(12)]),\n                   ' <span style=\"color: #ff0000;\">returns<\/span> ': [1, 1, 2, 3, 3, 3, 4, 3, 2, 3, 4, 7]})\n\n<span style=\"color: #008080;\">#plot both time series\n<\/span>plt. <span style=\"color: #3366ff;\">plot<\/span> ( <span style=\"color: #3366ff;\">df.date<\/span> , <span style=\"color: #3366ff;\">df.sales<\/span> , label=' <span style=\"color: #ff0000;\">sales<\/span> ', linewidth= <span style=\"color: #008000;\">3<\/span> )\nplt. <span style=\"color: #3366ff;\">plot<\/span> ( <span style=\"color: #3366ff;\">df2.date<\/span> , <span style=\"color: #3366ff;\">df2.returns<\/span> , color=' <span style=\"color: #ff0000;\">red<\/span> ', label=' <span style=\"color: #ff0000;\">returns<\/span> ', linewidth= <span style=\"color: #008000;\">3<\/span> )\n\n<span style=\"color: #008080;\">#add title and axis labels\n<\/span>plt. <span style=\"color: #3366ff;\">title<\/span> (' <span style=\"color: #ff0000;\">Sales by Date<\/span> ')\nplt. <span style=\"color: #3366ff;\">xlabel<\/span> (' <span style=\"color: #ff0000;\">Date<\/span> ')\nplt. <span style=\"color: #3366ff;\">ylabel<\/span> (' <span style=\"color: #ff0000;\">Sales<\/span> ')\n\n<span style=\"color: #008080;\">#add legend<\/span>\nplt. <span style=\"color: #3366ff;\">legend<\/span> ()\n\n<span style=\"color: #008080;\">#displayplot\n<\/span>plt. <span style=\"color: #3366ff;\">show<\/span> ()<\/strong><\/span> <\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-16353 \" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/temps3.png\" alt=\"Serie temporali multiple in Matplotlib\" width=\"578\" height=\"415\" srcset=\"\" sizes=\"\"><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Risorse addizionali<\/strong><\/span><\/h3>\n<p> <a href=\"https:\/\/statorials.org\/it\/boxplot-matplotlib-per-gruppo\/\" target=\"_blank\" rel=\"noopener\">Matplotlib: come creare boxplot per gruppo<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/grafico-a-barre-in-pila-matplotlib\/\" target=\"_blank\" rel=\"noopener\">Matplotlib: come creare grafici a barre in pila<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00c8 possibile utilizzare la seguente sintassi per tracciare una serie temporale in Matplotlib: import matplotlib. pyplot as plt plt. plot (df. x , df. y ) Ci\u00f2 presuppone che la variabile x appartenga alla classe datetime.datetime() . Gli esempi seguenti mostrano come utilizzare questa sintassi per tracciare dati di serie temporali in Python. Esempio 1: [&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 tracciare una serie temporale in Matplotlib (con esempi)<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come tracciare una serie temporale 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\/serie-temporali-matplotlib\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come tracciare una serie temporale in Matplotlib (con esempi)\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come tracciare una serie temporale in Matplotlib, con diversi esempi.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/serie-temporali-matplotlib\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-25T11:56:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/heure1.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\/serie-temporali-matplotlib\/\",\"url\":\"https:\/\/statorials.org\/it\/serie-temporali-matplotlib\/\",\"name\":\"Come tracciare una serie temporale in Matplotlib (con esempi)\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-25T11:56:30+00:00\",\"dateModified\":\"2023-07-25T11:56:30+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come tracciare una serie temporale in Matplotlib, con diversi esempi.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/serie-temporali-matplotlib\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/serie-temporali-matplotlib\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/serie-temporali-matplotlib\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come tracciare una serie temporale 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 tracciare una serie temporale in Matplotlib (con esempi)","description":"Questo tutorial spiega come tracciare una serie temporale 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\/serie-temporali-matplotlib\/","og_locale":"it_IT","og_type":"article","og_title":"Come tracciare una serie temporale in Matplotlib (con esempi)","og_description":"Questo tutorial spiega come tracciare una serie temporale in Matplotlib, con diversi esempi.","og_url":"https:\/\/statorials.org\/it\/serie-temporali-matplotlib\/","og_site_name":"Statorials","article_published_time":"2023-07-25T11:56:30+00:00","og_image":[{"url":"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/heure1.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\/serie-temporali-matplotlib\/","url":"https:\/\/statorials.org\/it\/serie-temporali-matplotlib\/","name":"Come tracciare una serie temporale in Matplotlib (con esempi)","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-25T11:56:30+00:00","dateModified":"2023-07-25T11:56:30+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come tracciare una serie temporale in Matplotlib, con diversi esempi.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/serie-temporali-matplotlib\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/serie-temporali-matplotlib\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/serie-temporali-matplotlib\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come tracciare una serie temporale 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\/1659"}],"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=1659"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/1659\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=1659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=1659"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=1659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}