{"id":628,"date":"2026-05-23T11:42:10","date_gmt":"2026-05-23T03:42:10","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=628"},"modified":"2026-03-24T08:17:54","modified_gmt":"2026-03-24T08:17:54","slug":"how-to-create-bivariate-histogram-using-xlwings","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-create-bivariate-histogram-using-xlwings\/","title":{"rendered":"How To Create  Bivariate Histogram 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-1.jpg\" alt=\"\" class=\"wp-image-1538\" srcset=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-36-1.jpg 880w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-36-1-300x258.jpg 300w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-36-1-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 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    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')\nx=sht.range('A1:A1000').value\ny=sht.range('B1:B1000').value\n\n#\u9891\u6570\u5206\u6790\nbx=10\nby=10\nminx=9999\nmaxx=-9999\nminy=9999\nmaxy=-9999\nfor i in range(1000):\n    if minx>x&#91;i]: minx=x&#91;i]\n    if maxx&lt;x&#91;i]: maxx=x&#91;i]\n    if miny>y&#91;i]: miny=y&#91;i]\n    if maxy&lt;y&#91;i]: maxy=y&#91;i]\ndifx=maxx-minx\ndify=maxy-miny\nstepx=difx\/bx\nstepy=dify\/by\ncount=&#91;&#91;0 for _ in range(10)] for _ in range(10)]\nxi=&#91;0 for _ in range(11)]\nxi2=&#91;0 for _ in range(11)]\nxi&#91;0]=minx\nxi2&#91;0]=minx+stepx\/2\nfor i in range(1,11):\n    xi&#91;i]=xi&#91;i-1]+stepx\n    if i!=10:\n        xi2&#91;i]=xi&#91;i]+stepx\/2\nyi=&#91;0 for _ in range(11)]\nyi2=&#91;0 for _ in range(11)]\nyi&#91;0]=miny\nyi2&#91;0]=miny+stepy\/2\nfor i in range(1,11):\n    yi&#91;i]=yi&#91;i-1]+stepy\n    if i!=10:\n        yi2&#91;i]=yi&#91;i]+stepy\/2\nfor k in range(1000):\n    for i in range(10):\n        if x&#91;k]>=xi&#91;i] and x&#91;k]&lt;xi&#91;i+1]:\n            for j in range(10):\n                if y&#91;k]>=yi&#91;j] and y&#91;k]&lt;yi&#91;j+1]:\n                    count&#91;i]&#91;j]+=1\n\n#\u8f93\u51fa\u9891\u6570\u5230sheet2\nsht2=wb.sheets.add()\nfor i in range(1,11):\n    for j in range(1,11):\n        sht2.api.Cells(i+1,j+1).Value=count&#91;i-1]&#91;j-1]\n\n#\u6839\u636e\u9891\u6570\u7ed8\u5236\u4e8c\u5143\u76f4\u65b9\u56fe\nshp=sht2.api.Shapes.AddChart2(286,xw.constants.ChartType.xl3DColumn)\nshp.Left=20\ncht=shp.Chart\nif cht.SeriesCollection().Count>0:\n    for i in range(cht.SeriesCollection().Count,0,-1):\n        cht.SeriesCollection(i).Delete()\ncht.Legend.Delete()\n\ncountj=&#91;0 for _ in range(10)]\nfor i in range(10):\n    countj&#91;i]=count&#91;i]&#91;:]\n    cht.SeriesCollection().NewSeries()\n    cht.SeriesCollection(i+1).Name=str(yi2&#91;i])    #\u5e8f\u5217\u8f74\u523b\u5ea6\u6807\u7b7e\n    cht.SeriesCollection(i+1).XValues=xi2    #\u5206\u7c7b\u8f74\u523b\u5ea6\u6807\u7b7e\n    cht.SeriesCollection(i+1).Values=countj&#91;i]    #Z\u8f74\n  \ncht.ChartGroups(1).GapWidth=0\ncht.GapDepth=0\nfor i in range(10):\n    fl=cht.SeriesCollection(i+1).Format.Fill\n    fl.ForeColor.ObjectThemeColor=5    #msoThemeColorAccent1\n    #fl.ForeColor.TintAndShade = 0\n    fl.ForeColor.Brightness=0\n    fl.Solid()\n\n    ln=cht.SeriesCollection(i+1).Format.Line\n    ln.Visible=True\n    ln.ForeColor.ObjectThemeColor=13    #msoThemeColorText1\n    #ln.ForeColor.TintAndShade = 0\n    ln.ForeColor.Brightness=0.0500000007\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-38.jpg\" alt=\"\" class=\"wp-image-1540\" srcset=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-38.jpg 880w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-38-300x258.jpg 300w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-38-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-628","post","type-post","status-publish","format-standard","hentry","category-xlwings-advanced-chart"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/628","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=628"}],"version-history":[{"count":2,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/628\/revisions"}],"predecessor-version":[{"id":1541,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/628\/revisions\/1541"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}