{"id":2370,"date":"2023-07-22T14:05:30","date_gmt":"2023-07-22T14:05:30","guid":{"rendered":"https:\/\/statorials.org\/id\/urutan-legenda-matplotlib\/"},"modified":"2023-07-22T14:05:30","modified_gmt":"2023-07-22T14:05:30","slug":"urutan-legenda-matplotlib","status":"publish","type":"post","link":"https:\/\/statorials.org\/id\/urutan-legenda-matplotlib\/","title":{"rendered":"Bagaimana mengubah urutan elemen dalam legenda matplotlib"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Anda dapat menggunakan potongan kode berikut untuk mengubah urutan elemen dalam legenda Matplotlib:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#get handles and labels<\/span>\nhandles, labels = plt. <span style=\"color: #3366ff;\">gca<\/span> (). <span style=\"color: #3366ff;\">get_legend_handles_labels<\/span> ()\n\n<span style=\"color: #008080;\">#specify order of items in legend<\/span>\norder = [1,2,0]\n\n<span style=\"color: #008080;\">#add legend to plot\n<\/span>plt. <span style=\"color: #3366ff;\">legend<\/span> ([handles[idx] <span style=\"color: #008000;\">for<\/span> idx <span style=\"color: #008000;\">in<\/span> order],[labels[idx] <span style=\"color: #008000;\">for<\/span> idx <span style=\"color: #008000;\">in<\/span> order])\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Contoh berikut menunjukkan cara menggunakan sintaksis ini dalam praktiknya.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Contoh: mengubah urutan elemen pada legenda Matplotlib<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Misalkan kita membuat diagram garis berikut di Matplotlib:<\/span> <\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">import<\/span> pandas <span style=\"color: #008000;\">as<\/span> pd<span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008000;\">\nimport<\/span> matplotlib. <span style=\"color: #3366ff;\">pyplot<\/span> <span style=\"color: #008000;\">as<\/span> plt\n\n<span style=\"color: #008080;\">#create data\n<\/span>df = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({'points': [11, 17, 16, 18, 22, 25, 26, 24, 29],\n                   'assists': [5, 7, 7, 9, 12, 9, 9, 4, 8],\n                   'rebounds': [6, 8, 8, 10, 14, 12, 12, 10, 11]})\n\n<span style=\"color: #008080;\">#add lines to plot\n<\/span>plt. <span style=\"color: #3366ff;\">plot<\/span> (df['points'], label='Points', color='green')\nplt. <span style=\"color: #3366ff;\">plot<\/span> (df['assists'], label='Assists', color='steelblue')\nplt. <span style=\"color: #3366ff;\">plot<\/span> (df['rebounds'], label='Rebounds', color='purple')\n\n<span style=\"color: #008080;\">#add legend\n<\/span>plt. <span style=\"color: #3366ff;\">legend<\/span> ()<\/span><\/span><\/strong> <\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\" wp-image-21422 aligncenter\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/tapis1-1.png\" alt=\"\" width=\"509\" height=\"333\" srcset=\"\" sizes=\"\"><\/p>\n<p> <span style=\"color: #000000;\">Elemen legenda ditempatkan sesuai urutan kita menambahkan baris ke plot.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Namun, kita dapat menggunakan sintaks berikut untuk menyesuaikan urutan elemen dalam legenda:<\/span> <\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">import<\/span> pandas <span style=\"color: #008000;\">as<\/span> pd<span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008000;\">\nimport<\/span> matplotlib. <span style=\"color: #3366ff;\">pyplot<\/span> <span style=\"color: #008000;\">as<\/span> plt\n\n<span style=\"color: #008080;\">#create data\n<\/span>df = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({'points': [11, 17, 16, 18, 22, 25, 26, 24, 29],\n                   'assists': [5, 7, 7, 9, 12, 9, 9, 4, 8],\n                   'rebounds': [6, 8, 8, 10, 14, 12, 12, 10, 11]})\n\n<span style=\"color: #008080;\">#add lines to plot\n<\/span>plt. <span style=\"color: #3366ff;\">plot<\/span> (df['points'], label='Points', color='green')\nplt. <span style=\"color: #3366ff;\">plot<\/span> (df['assists'], label='Assists', color='steelblue')\nplt. <span style=\"color: #3366ff;\">plot<\/span> (df['rebounds'], label='Rebounds', color='purple')\n\n<span style=\"color: #008080;\">#get handles and labels<\/span>\nhandles, labels = plt. <span style=\"color: #3366ff;\">gca<\/span> (). <span style=\"color: #3366ff;\">get_legend_handles_labels<\/span> ()\n\n<span style=\"color: #008080;\">#specify order of items in legend<\/span>\norder = [1,2,0]\n\n<span style=\"color: #008080;\">#add legend to plot<\/span>\nplt. <span style=\"color: #3366ff;\">legend<\/span> ([handles[idx] <span style=\"color: #008000;\">for<\/span> idx <span style=\"color: #008000;\">in<\/span> order],[labels[idx] <span style=\"color: #008000;\">for<\/span> idx <span style=\"color: #008000;\">in<\/span> order]) \n<\/span><\/span><\/strong><\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-21423 \" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/tapis2-1.png\" alt=\"Urutan legenda Matplotlib\" width=\"507\" height=\"341\" srcset=\"\" sizes=\"\"><\/p>\n<p> <span style=\"color: #000000;\">Perhatikan bahwa kami menentukan:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\">urutan = [1, 2, 0]<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Itu berarti:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\">Item pertama dalam legenda haruslah label yang awalnya berada pada posisi indeks <strong>1<\/strong> dari legenda lama (\u201cMembantu\u201d).<\/span><\/li>\n<li> <span style=\"color: #000000;\">Elemen kedua dari legenda haruslah label yang awalnya berada pada posisi indeks <strong>2<\/strong> dari legenda lama (\u201cMemantul\u201d)<\/span><\/li>\n<li> <span style=\"color: #000000;\">Elemen ketiga pada legenda harus berupa label yang semula berada pada posisi indeks <strong>0<\/strong> dari legenda lama (\u201cPoin\u201d)<\/span><\/li>\n<\/ul>\n<h3> <span style=\"color: #000000;\"><strong>Sumber daya tambahan<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Tutorial berikut menjelaskan cara melakukan operasi umum lainnya di Matplotlib:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/id\/posisi-legenda-matplotlib\/\" target=\"_blank\" rel=\"noopener\">Bagaimana mengubah posisi legenda di Matplotlib<\/a><br \/><a href=\"https:\/\/statorials.org\/id\/legenda-matplotlib-di-luar-plot\/\" target=\"_blank\" rel=\"noopener\">Bagaimana menempatkan legenda di luar plot Matplotlib<\/a><br \/> <a href=\"https:\/\/statorials.org\/id\/ukuran-font-legenda-matplotlib\/\" target=\"_blank\" rel=\"noopener\">Bagaimana mengubah ukuran font legenda di Matplotlib<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Anda dapat menggunakan potongan kode berikut untuk mengubah urutan elemen dalam legenda Matplotlib: #get handles and labels handles, labels = plt. gca (). get_legend_handles_labels () #specify order of items in legend order = [1,2,0] #add legend to plot plt. legend ([handles[idx] for idx in order],[labels[idx] for idx in order]) Contoh berikut menunjukkan cara menggunakan sintaksis [&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>Cara mengubah urutan elemen di Matplotlib Legend \u2013 Statorials<\/title>\n<meta name=\"description\" content=\"Tutorial ini menjelaskan cara mengubah urutan elemen dalam legenda Matplotlib, dengan beberapa contoh.\" \/>\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\/id\/urutan-legenda-matplotlib\/\" \/>\n<meta property=\"og:locale\" content=\"id_ID\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Cara mengubah urutan elemen di Matplotlib Legend \u2013 Statorials\" \/>\n<meta property=\"og:description\" content=\"Tutorial ini menjelaskan cara mengubah urutan elemen dalam legenda Matplotlib, dengan beberapa contoh.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/id\/urutan-legenda-matplotlib\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-22T14:05:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/tapis1-1.png\" \/>\n<meta name=\"author\" content=\"Benjamin anderson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Ditulis oleh\" \/>\n\t<meta name=\"twitter:data1\" content=\"Benjamin anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimasi waktu membaca\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 menit\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/id\/urutan-legenda-matplotlib\/\",\"url\":\"https:\/\/statorials.org\/id\/urutan-legenda-matplotlib\/\",\"name\":\"Cara mengubah urutan elemen di Matplotlib Legend \u2013 Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/id\/#website\"},\"datePublished\":\"2023-07-22T14:05:30+00:00\",\"dateModified\":\"2023-07-22T14:05:30+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/id\/#\/schema\/person\/3d17a1160dd2d052b7c78e502cb9ec81\"},\"description\":\"Tutorial ini menjelaskan cara mengubah urutan elemen dalam legenda Matplotlib, dengan beberapa contoh.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/id\/urutan-legenda-matplotlib\/#breadcrumb\"},\"inLanguage\":\"id\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/id\/urutan-legenda-matplotlib\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/id\/urutan-legenda-matplotlib\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/statorials.org\/id\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Bagaimana mengubah urutan elemen dalam legenda matplotlib\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/id\/#website\",\"url\":\"https:\/\/statorials.org\/id\/\",\"name\":\"Statorials\",\"description\":\"Panduan anda untuk kompetensi statistik!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/id\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"id\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/id\/#\/schema\/person\/3d17a1160dd2d052b7c78e502cb9ec81\",\"name\":\"Benjamin anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"id\",\"@id\":\"https:\/\/statorials.org\/id\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/statorials.org\/id\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"http:\/\/statorials.org\/id\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"caption\":\"Benjamin anderson\"},\"description\":\"Halo, saya Benjamin, pensiunan profesor statistika yang menjadi guru Statorial yang berdedikasi. Dengan pengalaman dan keahlian yang luas di bidang statistika, saya ingin berbagi ilmu untuk memberdayakan mahasiswa melalui Statorials. Baca selengkapnya\",\"sameAs\":[\"http:\/\/statorials.org\/id\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Cara mengubah urutan elemen di Matplotlib Legend \u2013 Statorials","description":"Tutorial ini menjelaskan cara mengubah urutan elemen dalam legenda Matplotlib, dengan beberapa contoh.","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\/id\/urutan-legenda-matplotlib\/","og_locale":"id_ID","og_type":"article","og_title":"Cara mengubah urutan elemen di Matplotlib Legend \u2013 Statorials","og_description":"Tutorial ini menjelaskan cara mengubah urutan elemen dalam legenda Matplotlib, dengan beberapa contoh.","og_url":"https:\/\/statorials.org\/id\/urutan-legenda-matplotlib\/","og_site_name":"Statorials","article_published_time":"2023-07-22T14:05:30+00:00","og_image":[{"url":"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/tapis1-1.png"}],"author":"Benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Ditulis oleh":"Benjamin anderson","Estimasi waktu membaca":"2 menit"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/id\/urutan-legenda-matplotlib\/","url":"https:\/\/statorials.org\/id\/urutan-legenda-matplotlib\/","name":"Cara mengubah urutan elemen di Matplotlib Legend \u2013 Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/id\/#website"},"datePublished":"2023-07-22T14:05:30+00:00","dateModified":"2023-07-22T14:05:30+00:00","author":{"@id":"https:\/\/statorials.org\/id\/#\/schema\/person\/3d17a1160dd2d052b7c78e502cb9ec81"},"description":"Tutorial ini menjelaskan cara mengubah urutan elemen dalam legenda Matplotlib, dengan beberapa contoh.","breadcrumb":{"@id":"https:\/\/statorials.org\/id\/urutan-legenda-matplotlib\/#breadcrumb"},"inLanguage":"id","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/id\/urutan-legenda-matplotlib\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/id\/urutan-legenda-matplotlib\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/statorials.org\/id\/"},{"@type":"ListItem","position":2,"name":"Bagaimana mengubah urutan elemen dalam legenda matplotlib"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/id\/#website","url":"https:\/\/statorials.org\/id\/","name":"Statorials","description":"Panduan anda untuk kompetensi statistik!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/id\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"id"},{"@type":"Person","@id":"https:\/\/statorials.org\/id\/#\/schema\/person\/3d17a1160dd2d052b7c78e502cb9ec81","name":"Benjamin anderson","image":{"@type":"ImageObject","inLanguage":"id","@id":"https:\/\/statorials.org\/id\/#\/schema\/person\/image\/","url":"http:\/\/statorials.org\/id\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","contentUrl":"http:\/\/statorials.org\/id\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","caption":"Benjamin anderson"},"description":"Halo, saya Benjamin, pensiunan profesor statistika yang menjadi guru Statorial yang berdedikasi. Dengan pengalaman dan keahlian yang luas di bidang statistika, saya ingin berbagi ilmu untuk memberdayakan mahasiswa melalui Statorials. Baca selengkapnya","sameAs":["http:\/\/statorials.org\/id"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/id\/wp-json\/wp\/v2\/posts\/2370"}],"collection":[{"href":"https:\/\/statorials.org\/id\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/id\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/id\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/id\/wp-json\/wp\/v2\/comments?post=2370"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/id\/wp-json\/wp\/v2\/posts\/2370\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/id\/wp-json\/wp\/v2\/media?parent=2370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/id\/wp-json\/wp\/v2\/categories?post=2370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/id\/wp-json\/wp\/v2\/tags?post=2370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}