{"id":542,"date":"2026-04-09T11:59:36","date_gmt":"2026-04-09T03:59:36","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=542"},"modified":"2026-03-23T09:28:18","modified_gmt":"2026-03-23T09:28:18","slug":"how-to-set-legend-using-xlwings","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-set-legend-using-xlwings\/","title":{"rendered":"How To Set Legend Using xlwings?"},"content":{"rendered":"<p>\u3010<b>Method<\/b>\u3011<\/p>\n<p>The legend is represented by the `Legend` object. You can use the `HasLegend` property of the `Chart` object to show or hide the legend, and the `Legend` property to return the `Legend` object. Using the properties and methods of the `Legend` object, you can modify the appearance, font, and position of the legend.<\/p>\n<p>The `Format` property of the `Legend` object returns a `ChartFormat` object, which can be used to set the background and border of the legend. The `Font` property returns a `Font` object to set the font. The `Position` property determines the position of the legend. The values for the `Position` property are shown in the table below.<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<p>Name<\/p>\n<\/td>\n<td>\n<p>Value<\/p>\n<\/td>\n<td>\n<p>Description<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>xlLegendPositionBottom<\/p>\n<\/td>\n<td>\n<p>-4107<\/p>\n<\/td>\n<td>\n<p>Display legend at the bottom<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>xlLegendPositionCorner<\/p>\n<\/td>\n<td>\n<p>2<\/p>\n<\/td>\n<td>\n<p>Display legend at the top-right corner of the chart<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>xlLegendPositionCustom<\/p>\n<\/td>\n<td>\n<p>-4161<\/p>\n<\/td>\n<td>\n<p>Display legend at a custom position<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>xlLegendPositionLeft<\/p>\n<\/td>\n<td>\n<p>-4131<\/p>\n<\/td>\n<td>\n<p>Display legend on the left side<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>xlLegendPositionRight<\/p>\n<\/td>\n<td>\n<p>-4152<\/p>\n<\/td>\n<td>\n<p>Display legend on the right side<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>xlLegendPositionTop<\/p>\n<\/td>\n<td>\n<p>-4160<\/p>\n<\/td>\n<td>\n<p>Display legend at the top<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>sht.api.Range(&#8216;A1:B7&#8217;).Select()<\/p>\n<p>sht.api.Range(&#8216;A1:B7&#8217;).Select()\u00a0\u00a0\u00a0 #Data<\/p>\n<p>cht=sht.api.Shapes.AddChart().Chart\u00a0\u00a0\u00a0 #Add chart<\/p>\n<p>cht.Legend.Font.Italic=True\u00a0\u00a0\u00a0 #Legend font italicized<\/p>\n<p>cht.Legend.Format.Fill.ForeColor.RGB=xw.utils.rgb_to_int((255,255,0))<\/p>\n<p>cht.Legend.Format.Line.ForeColor.RGB=xw.utils.rgb_to_int((0,0,255))<\/p>\n<p>cht.Legend.Position=-4107\u00a0\u00a0\u00a0 #Legend positioned below the chart<\/p>\n<p>\u00a0<\/p>\n<p>\u3010<b>Example<\/b>\u3011<\/p>\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"880\" height=\"756\" src=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/04\/2-07-4.jpg\" alt=\"\" class=\"wp-image-1204\" srcset=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/04\/2-07-4.jpg 880w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/04\/2-07-4-300x258.jpg 300w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/04\/2-07-4-768x660.jpg 768w\" sizes=\"auto, (max-width: 880px) 100vw, 880px\" \/><\/figure>\n\n\n\n<p>\u3010<strong>Code<\/strong>\u3011<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nimport os\n\ndef set_style(cht):\n    cht.ChartArea.Format.Line.Visible=False\n    cht.PlotArea.Format.Fill.Visible=False\n    cht.PlotArea.Format.Line.Visible=True\n    cht.PlotArea.Format.Line.ForeColor.RGB=xw.utils.rgb_to_int((200,200,200))\n    #cht.PlotArea.Format.Line.ForeColor.ObjectThemeColor = msoThemeColorText1\n    ax1=cht.Axes(1)\n    ax2=cht.Axes(2)\n    ax1.HasTitle=True\n    ax1.AxisTitle.Text='Categories'\n    ax1.AxisTitle.Font.Size=10\n    ax1.TickLabels.Font.Size=8\n    #ax1.TickLabels.NumberFormat='0.00'\n    ax1.HasMajorGridlines=False\n    ax2.HasTitle=True\n    ax2.AxisTitle.Text='Values'\n    ax2.AxisTitle.Font.Size=10\n    ax2.TickLabels.Font.Size=8\n    ax2.HasMajorGridlines=False\n    cht.HasTitle=True\n    #cht.ChartTitle.Caption='Plot'\n    #cht.ChartTitle.Font.Size=12\n\nroot=os.getcwd()\napp=xw.App(visible=True, add_book=False)\nwb=app.books.open(root+r'\/data.xlsx',read_only=False)\nsht=wb.sheets('Sheet1')\n\nsht.api.Range('A1:B7').Select()    #\ncht=sht.api.Shapes.AddChart2(-1, \\\n          xw.constants.ChartType.xlColumnClustered,20,20,350,250,True).Chart\ncht.HasLegend=True\nleg=cht.Legend\nleg.Position=xw.constants.LegendPosition.xlLegendPositionBottom\nleg.Format.Fill.ForeColor.RGB=xw.utils.rgb_to_int((255,255,0))\n      \nset_style(cht)\n\ncht.Export(root+'\/cht.jpg')\ncht.Export(root+'\/cht.svg')\ncht.ExportAsFixedFormat(0,root+'\/cht.pdf')\n\n#wb.save()\n#app.kill()<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"880\" height=\"756\" src=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/04\/2-28.jpg\" alt=\"\" class=\"wp-image-1206\" srcset=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/04\/2-28.jpg 880w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/04\/2-28-300x258.jpg 300w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/04\/2-28-768x660.jpg 768w\" sizes=\"auto, (max-width: 880px) 100vw, 880px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Method<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-542","post","type-post","status-publish","format-standard","hentry","category-xlwings-chart"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/542","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/comments?post=542"}],"version-history":[{"count":2,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/542\/revisions"}],"predecessor-version":[{"id":1207,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/542\/revisions\/1207"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}