{"id":625,"date":"2026-05-21T17:48:04","date_gmt":"2026-05-21T09:48:04","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=625"},"modified":"2026-03-24T08:06:14","modified_gmt":"2026-03-24T08:06:14","slug":"how-to-create-stem-chart-using-xlwings","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-create-stem-chart-using-xlwings\/","title":{"rendered":"How To Create Stem Chart 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;\">Example<\/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=\"880\" height=\"756\" src=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-32.jpg\" alt=\"\" class=\"wp-image-1526\" srcset=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-32.jpg 880w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-32-300x258.jpg 300w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-32-768x660.jpg 768w\" sizes=\"auto, (max-width: 880px) 100vw, 880px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u3010<strong>Code<\/strong>\u3011<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nimport numpy as np\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 = 'Values'\n    ax1.AxisTitle.Font.Size = 10\n    ax1.TickLabels.Font.Size = 8\n    #ax1.TickLabels.NumberFormat = '0.00'\n    ax1.HasMajorGridlines = True\n    ax1.HasMinorGridlines = True\n    ax2.HasTitle = True\n    ax2.AxisTitle.Text = 'Categories'\n    ax2.AxisTitle.Font.Size = 10\n    ax2.TickLabels.Font.Size = 8\n    ax2.HasMajorGridlines = True\n    ax2.HasMinorGridlines = True\n    cht.HasTitle = True\n    #cht.ChartTitle.Caption = 'Plot'\n    #cht.ChartTitle.Font.Size = 12\n\ndef draw_rnd_scatter(cht,x,y,r,g,b):\n    '''\u7ed8\u5236\u6563\u70b9\u56fe\n    x: X\u5750\u6807    y(0 to n-1): Y\u5750\u6807\n    '''\n    ser=cht.SeriesCollection().NewSeries()\n    ser.ChartType=xw.constants.ChartType.xlXYScatter\n    ser.XValues=x\n    ser.Values=y\n    ser.Format.Line.ForeColor.RGB=xw.utils.rgb_to_int((0,0,0))\n    ser.Format.Fill.ForeColor.RGB=xw.utils.rgb_to_int((r,g,b))\n    ser.MarkerSize=6    \n\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\nshp.Top=20\nshp.Width=220\nshp.Height=320\ncht=shp.Chart    #\u6dfb\u52a0\u56fe\u8868\ncht.ChartType=xw.constants.ChartType.xlXYScatter\nax1=cht.Axes(1)\nax2=cht.Axes(2)\nax1.MinimumScale=0\nax1.MaximumScale=0.3\nax2.MinimumScale=0\nax2.MaximumScale=16\n\nset_style(cht)\n\ndata=sht.range('B2:C16').value\ndt=np.transpose(data)\ndt1=dt&#91;0]\ndt2=dt&#91;1]\n\n#\u7ed8\u7ebf\u5f62\u56fe\nfor i in range(15):\n    ser=cht.SeriesCollection().NewSeries()\n    ser.ChartType=xw.constants.ChartType.xlXYScatterLinesNoMarkers\n    ser.XValues=&#91;0,dt1&#91;i]]\n    ser.Values=&#91;dt2&#91;i],dt2&#91;i]]\n    ser.Format.Line.ForeColor.RGB=xw.utils.rgb_to_int((0,0,255))\n    ser.Format.Line.Weight=1\n  \n#\u7ed8\u6563\u70b9\u56fe\ndraw_rnd_scatter(cht,dt1,dt2,0,0,255)\n\ncht.HasLegend=False\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\/05\/3-33.jpg\" alt=\"\" class=\"wp-image-1528\" srcset=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-33.jpg 880w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-33-300x258.jpg 300w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-33-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-625","post","type-post","status-publish","format-standard","hentry","category-xlwings-advanced-chart"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/625","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=625"}],"version-history":[{"count":2,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/625\/revisions"}],"predecessor-version":[{"id":1529,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/625\/revisions\/1529"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}