{"id":600,"date":"2026-05-08T11:44:44","date_gmt":"2026-05-08T03:44:44","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=600"},"modified":"2026-03-24T02:11:47","modified_gmt":"2026-03-24T02:11:47","slug":"how-to-create-a-colorbar-using-xlwings","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-create-a-colorbar-using-xlwings\/","title":{"rendered":"How To Create a Colorbar Using xlwings?"},"content":{"rendered":"<p style=\"margin: 0in; font-size: 16.0pt;\"><span lang=\"zh-CN\" style=\"font-family: 'Microsoft YaHei';\">\u3010<\/span><span lang=\"en-US\" style=\"font-weight: bold; font-family: Calibri;\">Method<\/span><span lang=\"zh-CN\" style=\"font-family: 'Microsoft YaHei';\">\u3011<\/span><\/p>\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"255\" height=\"167\" src=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/image-1.png\" alt=\"\" class=\"wp-image-1436\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"366\" height=\"189\" src=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/image-2.png\" alt=\"\" class=\"wp-image-1437\" srcset=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/image-2.png 366w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/image-2-300x155.png 300w\" sizes=\"auto, (max-width: 366px) 100vw, 366px\" \/><\/figure>\n\n\n\n<p>\u3010<strong>Example<\/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\nshp=sht.api.Shapes.AddChart2()\nshp.Left=20\ncht=shp.Chart\ncht.ChartType=xw.constants.ChartType.xlXYScatter\nax1=cht.Axes(1)\nax2=cht.Axes(2)\nax1.MinimumScale=0\nax1.MaximumScale=300\nax2.MinimumScale=0\nax2.MaximumScale=300\n  \nset_style(cht)\n\ncht.SeriesCollection().NewSeries()\n  \nx=shape_x(cht,50)\ny=shape_y(cht,250)\nw=cht.PlotArea.InsideWidth\/(ax1.MaximumScale-ax1.MinimumScale)*10\nh=cht.PlotArea.InsideHeight\/(ax2.MaximumScale-ax2.MinimumScale)*200\nshp2=cht.Shapes.AddShape(1,x,y,w,h)\nshp2.Fill.ForeColor.RGB=xw.utils.rgb_to_int((255,255,26))\nshp2.Fill.OneColorGradient(1,1,1)\nshp2.Fill.GradientStops.Insert(xw.utils.rgb_to_int((255,204,51)),0.25)\nshp2.Fill.GradientStops.Delete(2)\nshp2.Fill.GradientStops.Insert(xw.utils.rgb_to_int((204,204,51)),0.5)\nshp2.Fill.GradientStops.Insert(xw.utils.rgb_to_int((0,179,179)),0.75)\nshp2.Fill.GradientStops.Insert(xw.utils.rgb_to_int((51,128,255)),0.85)\nshp2.Fill.GradientStops.Insert(xw.utils.rgb_to_int((255,204,51)),1)\nshp2.Line.Weight=1\ncm_labels=&#91;'0','0.5','1']\ncm_label_pos=&#91;50,150,250]\nfor i in range(3):\n    lf=shape_x(cht,57)\n    tp=shape_y(cht,cm_label_pos&#91;i]+20)\n    wd=cht.PlotArea.InsideWidth\/(cht.Axes(1).MaximumScale-cht.Axes(1).MinimumScale)*40\n    ht=cht.PlotArea.InsideHeight\/(cht.Axes(2).MaximumScale-cht.Axes(2).MinimumScale)*30\n    shp6=cht.Shapes.AddLabel(1,lf,tp,wd,ht)\n    shp6.TextFrame2.TextRange.Characters.Text=cm_labels&#91;i]\n    shp6.TextFrame2.TextRange.Characters.Font.Size=8\n    shp6.TextFrame2.AutoSize=1    #msoAutoSizeTextToFitShape\n  \nx=shape_x(cht,110)\ny=shape_y(cht,250)\nw=cht.PlotArea.InsideWidth\/(ax1.MaximumScale-ax1.MinimumScale)*10\nh=cht.PlotArea.InsideHeight\/(ax2.MaximumScale-ax2.MinimumScale)*200\nshp3=cht.Shapes.AddShape(1,x,y,w,h)\nshp3.Fill.ForeColor.RGB=xw.utils.rgb_to_int((255,0,255))\nshp3.Fill.OneColorGradient(1,1,1)\nshp3.Fill.GradientStops.Insert(xw.utils.rgb_to_int((0,255,255)),1)\nshp3.Fill.GradientStops.Delete(2)\nshp3.Line.Weight=1\nfor i in range(3):\n    lf=shape_x(cht,117)\n    tp=shape_y(cht,cm_label_pos&#91;i]+20)\n    wd=cht.PlotArea.InsideWidth\/(cht.Axes(1).MaximumScale-cht.Axes(1).MinimumScale)*40\n    ht=cht.PlotArea.InsideHeight\/(cht.Axes(2).MaximumScale-cht.Axes(2).MinimumScale)*30\n    shp6=cht.Shapes.AddLabel(1,lf,tp,wd,ht)\n    shp6.TextFrame2.TextRange.Characters.Text=cm_labels&#91;i]\n    shp6.TextFrame2.TextRange.Characters.Font.Size=8\n    shp6.TextFrame2.AutoSize=1    #msoAutoSizeTextToFitShape\n  \nx=shape_x(cht,170)\ny=shape_y(cht,250)\nw=cht.PlotArea.InsideWidth\/(ax1.MaximumScale-ax1.MinimumScale)*10\nh=cht.PlotArea.InsideHeight\/(ax2.MaximumScale-ax2.MinimumScale)*200\nshp4=cht.Shapes.AddShape(1,x,y,w,h)\nshp4.Fill.ForeColor.RGB=xw.utils.rgb_to_int((255,255,102))\nshp4.Fill.OneColorGradient(1,1,1)\nshp4.Fill.GradientStops.Insert(xw.utils.rgb_to_int((0,128,102)),1)\nshp4.Fill.GradientStops.Delete(2)\nshp4.Line.Weight=1\nfor i in range(3):\n    lf=shape_x(cht,177)\n    tp=shape_y(cht,cm_label_pos&#91;i]+20)\n    wd=cht.PlotArea.InsideWidth\/(cht.Axes(1).MaximumScale-cht.Axes(1).MinimumScale)*40\n    ht=cht.PlotArea.InsideHeight\/(cht.Axes(2).MaximumScale-cht.Axes(2).MinimumScale)*30\n    shp6=cht.Shapes.AddLabel(1,lf,tp,wd,ht)\n    shp6.TextFrame2.TextRange.Characters.Text=cm_labels&#91;i]\n    shp6.TextFrame2.TextRange.Characters.Font.Size=8\n    shp6.TextFrame2.AutoSize=1    #msoAutoSizeTextToFitShape\n  \nx=shape_x(cht,230)\ny=shape_y(cht,250)\nw=cht.PlotArea.InsideWidth\/(ax1.MaximumScale-ax1.MinimumScale)*10\nh=cht.PlotArea.InsideHeight\/(ax2.MaximumScale-ax2.MinimumScale)*200\nshp5=cht.Shapes.AddShape(1,x,y,w,h)\nshp5.Fill.ForeColor.RGB=xw.utils.rgb_to_int((128,0,0))\nshp5.Fill.OneColorGradient(1,1,1)\nshp5.Fill.GradientStops.Insert(xw.utils.rgb_to_int((255,0,0)),0.1)\nshp5.Fill.GradientStops.Delete(2)\nshp5.Fill.GradientStops.Insert(xw.utils.rgb_to_int((255,255,0)),0.39)\nshp5.Fill.GradientStops.Insert(xw.utils.rgb_to_int((0,255,255)),0.65)\nshp5.Fill.GradientStops.Insert(xw.utils.rgb_to_int((0,0,255)),0.89)\nshp5.Fill.GradientStops.Insert(xw.utils.rgb_to_int((0,0,128)),1)\nshp5.Line.Weight=1\nfor i in range(3):\n    lf=shape_x(cht,237)\n    tp=shape_y(cht,cm_label_pos&#91;i]+20)\n    wd=cht.PlotArea.InsideWidth\/(cht.Axes(1).MaximumScale-cht.Axes(1).MinimumScale)*40\n    ht=cht.PlotArea.InsideHeight\/(cht.Axes(2).MaximumScale-cht.Axes(2).MinimumScale)*30\n    shp6=cht.Shapes.AddLabel(1,lf,tp,wd,ht)\n    shp6.TextFrame2.TextRange.Characters.Text=cm_labels&#91;i]\n    shp6.TextFrame2.TextRange.Characters.Font.Size=8\n    shp6.TextFrame2.AutoSize=1    #msoAutoSizeTextToFitShape\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\/05\/3-04.jpg\" alt=\"\" class=\"wp-image-1438\" srcset=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-04.jpg 880w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-04-300x258.jpg 300w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-04-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":[24],"tags":[],"class_list":["post-600","post","type-post","status-publish","format-standard","hentry","category-xlwings-advanced-chart"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/600","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=600"}],"version-history":[{"count":2,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/600\/revisions"}],"predecessor-version":[{"id":1439,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/600\/revisions\/1439"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=600"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}