{"id":627,"date":"2026-05-22T17:52:12","date_gmt":"2026-05-22T09:52:12","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=627"},"modified":"2026-03-24T08:14:20","modified_gmt":"2026-03-24T08:14:20","slug":"how-to-create-univariate-histogram-using-xlwings","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-create-univariate-histogram-using-xlwings\/","title":{"rendered":"How To Create Univariate 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.jpg\" alt=\"\" class=\"wp-image-1534\" srcset=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-36.jpg 880w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-36-300x258.jpg 300w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-36-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    #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\ndef draw_hist(sht,n):\n    #\u9891\u6570\u5206\u6790\n    x=sht.range('A1:A1000').value\n    xi=&#91;0 for _ in range(11)]\n    xi2=&#91;0 for _ in range(10)]\n    count=&#91;0 for _ in range(10)]\n    bx=10\n    minx=9999\n    maxx=-9999\n    for i in range(n):\n        if minx>x&#91;i]:\n            minx=x&#91;i]\n        if maxx&lt;x&#91;i]:\n            maxx=x&#91;i]\n    difx=maxx-minx\n    stepx=difx\/bx\n    for i in range(10):\n        count&#91;i]=0\n    xi&#91;0]=minx\n    xi2&#91;0]=minx+stepx\/2\n    for 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 \n    for i in range(n):\n        for j in range(10):\n            if x&#91;i]>=xi&#91;j] and x&#91;i]&lt;xi&#91;j+1]:\n                count&#91;j]+=1\n\n    #\u6839\u636e\u9891\u6570\u7ed8\u5236\u76f4\u65b9\u56fe\n    sht.api.Range('D3').Select()\n    shp=sht.api.Shapes.AddChart2()\n    shp.Left=20\n    cht=shp.Chart\n    for i in range(cht.SeriesCollection().Count,0,-1):\n        cht.SeriesCollection(i).Delete()\n\n    cht.SeriesCollection().NewSeries()\n    cht.SeriesCollection(1).ChartType=xw.constants.ChartType.xlColumnClustered\n    cht.SeriesCollection(1).XValues=xi2\n    cht.SeriesCollection(1).Values=count\n    cht.ChartGroups(1).GapWidth=0\n    cht.GapDepth=0\n    \n    fl=cht.SeriesCollection(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(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  \n    return cht\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')\ncht=draw_hist(sht,1000)\nset_style(cht)\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-37.jpg\" alt=\"\" class=\"wp-image-1536\" srcset=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-37.jpg 880w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-37-300x258.jpg 300w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-37-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-627","post","type-post","status-publish","format-standard","hentry","category-xlwings-advanced-chart"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/627","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=627"}],"version-history":[{"count":2,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/627\/revisions"}],"predecessor-version":[{"id":1537,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/627\/revisions\/1537"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}