{"id":630,"date":"2026-05-24T17:55:08","date_gmt":"2026-05-24T09:55:08","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=630"},"modified":"2026-03-24T08:28:14","modified_gmt":"2026-03-24T08:28:14","slug":"how-to-create-kernel-density-estimation-curve-with-color-fill-using-xlwings-2","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-create-kernel-density-estimation-curve-with-color-fill-using-xlwings-2\/","title":{"rendered":"How To Create Kernel Density Estimation Curve with Color Fill Using xlwings? 2"},"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-39-2.jpg\" alt=\"\" class=\"wp-image-1550\" srcset=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-39-2.jpg 880w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-39-2-300x258.jpg 300w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-39-2-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    ax2.HasTitle = True\n    ax2.AxisTitle.Text = 'Probability'\n    ax2.AxisTitle.Font.Size = 10\n    ax2.TickLabels.Font.Size = 8\n    ax2.HasMajorGridlines = True\n    cht.HasTitle = True\n    cht.ChartTitle.Caption = 'KDE Plot'\n    cht.ChartTitle.Font.Size = 12\n\ndef kde(data,x,h):\n  #\u4e00\u5143\u6838\u5bc6\u5ea6\u4f30\u8ba1\n  my_sum=0\n  count=0\n  for i in data:\n      my_sum+=(1\/np.sqrt(2*3.1415926))*\\\n      np.exp(-0.5*((x-i)\/h)*((x-i)\/h))\n      count+=1\n  return my_sum\/count\/h\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')\ndata=sht.range('A2:A61').value\napp.kill()\n\n#\u4ececomtypes\u5305\u4e2d\u5bfc\u5165CreateObject\u51fd\u6570\nfrom comtypes.client import CreateObject\napp2=CreateObject(\"Excel.Application\")  #\u521b\u5efaExcel\u5e94\u7528\napp2.Visible=True  #\u5e94\u7528\u7a97\u53e3\u53ef\u89c1\napp2.ScreenUpdating=False\nwb2=app2.Workbooks.Open(root+r'\/data.xlsx')  #\u6dfb\u52a0\u5de5\u4f5c\u7c3f\nsht2=wb2.Sheets('Sheet1')  #\u83b7\u53d6\u7b2c1\u4e2a\u5de5\u4f5c\u8868\n\nshp=sht2.Shapes.AddChart2()\nshp.Left=20\ncht=shp.Chart  #\u6dfb\u52a0\u56fe\u8868\ncht.ChartType=-4169\nax1=cht.Axes(1)\nax2=cht.Axes(2)\nax1.MinimumScale=-5.9\nax1.MaximumScale=12\nax2.MinimumScale=0\nax2.MaximumScale=0.15\nax1.CrossesAt=ax1.MinimumScale\nax2.CrossesAt=ax2.MinimumScale\n\nset_style(cht)\n\nkdex=&#91;0 for _ in range(180)]\nkdef=&#91;0 for _ in range(180)]\n\nfor i in range(180):\n    kdex&#91;i]=(i-59)\/10\n    kdef&#91;i]=kde(data,(i-59)\/10,1.5)\n\ncht.SeriesCollection().NewSeries()\n\n#\u753b\u586b\u5145\u591a\u8fb9\u5f62\npt=&#91;&#91;0 for _ in range(2)] for _ in range(183)]\nfor i in range(180):\n    pt&#91;i]&#91;0]=shape_x(cht,kdex&#91;179-i])\n    pt&#91;i]&#91;1]=shape_y(cht,kdef&#91;179-i])\npt&#91;180]&#91;0]=pt&#91;179]&#91;0]\npt&#91;180]&#91;1]=shape_y(cht,0)\npt&#91;181]&#91;0]=pt&#91;0]&#91;0]\npt&#91;181]&#91;1]=shape_y(cht,0)\npt&#91;182]&#91;0]=pt&#91;0]&#91;0]\npt&#91;182]&#91;1]=pt&#91;0]&#91;1]\n\nshp2=cht.Shapes.AddPolyline(pt)\nshp2.Fill.Transparency=0.5\nshp2.Fill.ForeColor.RGB=xw.utils.rgb_to_int((0,0,255))\nshp2.Fill.OneColorGradient(1,1,1)\nshp2.Line.ForeColor.RGB=xw.utils.rgb_to_int((0,0,255))\nshp2.Line.Weight=1.5\n  \napp2.ScreenUpdating=True\n\ncht.Export(root+'\/cht.jpg')\ncht.Export(root+'\/cht.svg')\ncht.ExportAsFixedFormat(0,root+'\/cht.pdf')\n\n#wb2.Save()\n#os.system('taskkill \/f \/im EXCEL.EXE')<\/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-42.jpg\" alt=\"\" class=\"wp-image-1552\" srcset=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-42.jpg 880w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-42-300x258.jpg 300w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-42-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-630","post","type-post","status-publish","format-standard","hentry","category-xlwings-advanced-chart"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/630","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=630"}],"version-history":[{"count":2,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/630\/revisions"}],"predecessor-version":[{"id":1553,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/630\/revisions\/1553"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}