{"id":634,"date":"2026-05-26T18:52:43","date_gmt":"2026-05-26T10:52:43","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=634"},"modified":"2026-03-24T09:14:02","modified_gmt":"2026-03-24T09:14:02","slug":"how-to-create-bivariate-kernel-density-estimation-surface-plot-using-xlwings","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-create-bivariate-kernel-density-estimation-surface-plot-using-xlwings\/","title":{"rendered":"How To Create Bivariate Kernel Density Estimation Surface Plot 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-36-3.jpg\" alt=\"\" class=\"wp-image-1566\" srcset=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-36-3.jpg 880w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-36-3-300x258.jpg 300w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-36-3-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 kde2(dt1,dt2,x,y,w):\n    #\u4e8c\u5143\u6838\u5bc6\u5ea6\u4f30\u8ba1\n    my_sum=0\n    count=0\n    for i in dt1:\n        for j in dt2:\n            #Gaussian\u6838\u51fd\u6570(2D)\n            my_sum+=np.exp(-((x-i)**2+(y-j)**2)\/(2*w**2))\/\\\n            (2*3.1416*w**2)\n            count+=1\n    return my_sum\/count\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')\ndt1=sht.range('A1:A200').value\ndt2=sht.range('B1:B200').value\n\n#\u7528Shapes\u521b\u5efa\u56fe\u8868\nkdex=&#91;0 for _ in range(40)]\nkdey=&#91;0 for _ in range(40)]\nkdef=&#91;&#91;0 for _ in range(40)] for _ in range(40)]\nfor i in range(40):\n    kdex&#91;i]=(i- 20)\/2\n    kdey&#91;i]=(i- 20)\/2\nfor i in range(40):\n    for j in range(40):\n        kdef&#91;i]&#91;j]=kde2(dt1,dt2,kdex&#91;i],kdey&#91;j],1.5)\n\nsht2=wb.sheets.add()\nsht2.name='plot'\nfor i in range(40):\n    sht2.api.Cells(1, i+2).Value=kdex&#91;i]\n    sht2.api.Cells(i+2, 1).Value=kdey&#91;i]\n    for j in range(40):\n        sht2.api.Cells(i+2,j+2).Value=kdef&#91;i]&#91;j]\n  \n#\u521b\u5efa\u66f2\u9762\u56fe\u8868\nshp=sht2.api.Shapes.AddChart2()\nshp.Left=20\nshp.Top=50\nshp.Width=500\nshp.Height=400\ncht=shp.Chart\n#\u8bbe\u7f6e\u56fe\u8868\u6570\u636e\u8303\u56f4\ncht.SetSourceData(sht2.api.Range(sht2.api.Cells(2, 2),sht2.api.Cells(41, 41)))\n#\u8bbe\u7f6e\u56fe\u8868\u7c7b\u578b\u4e3a\u4e09\u7ef4\u66f2\u9762\u56fe\ncht.ChartType=xw.constants.ChartType.xlSurface\n#\u8bbe\u7f6e\u56fe\u8868\u6807\u9898\ncht.HasTitle=True\ncht.ChartTitle.Text='Surface'\n#\u8bbe\u7f6e\u8f74\u6807\u9898\ncht.Axes(1,1).HasTitle=True\ncht.Axes(1,1).AxisTitle.Text='X Axis'\ncht.Axes(3,1).HasTitle=True\ncht.Axes(3,1).AxisTitle.Text='Y Axis'\ncht.Axes(2,1).HasTitle=True\ncht.Axes(2,1).AxisTitle.Text='Z Axis'\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-47.jpg\" alt=\"\" class=\"wp-image-1568\" srcset=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-47.jpg 880w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-47-300x258.jpg 300w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-47-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-634","post","type-post","status-publish","format-standard","hentry","category-xlwings-advanced-chart"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/634","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=634"}],"version-history":[{"count":2,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/634\/revisions"}],"predecessor-version":[{"id":1569,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/634\/revisions\/1569"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}