{"id":633,"date":"2026-05-26T11:47:54","date_gmt":"2026-05-26T03:47:54","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=633"},"modified":"2026-03-24T09:07:44","modified_gmt":"2026-03-24T09:07:44","slug":"how-to-create-ridge-plot-using-xlwings-2","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-create-ridge-plot-using-xlwings-2\/","title":{"rendered":"How To Create Ridge Plot 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-43-1.jpg\" alt=\"\" class=\"wp-image-1563\" srcset=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-43-1.jpg 880w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-43-1-300x258.jpg 300w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-43-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 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 = False\n    ax2.HasTitle = True\n    ax2.AxisTitle.Text = 'Probability'\n    ax2.AxisTitle.Font.Size = 10\n    ax2.TickLabels.Font.Size = 8\n    ax2.HasMajorGridlines = False\n    cht.HasTitle = True\n    #cht.ChartTitle.Caption = 'KDE Plot'\n    #cht.ChartTitle.Font.Size = 12\n\ndef draw_kde(cht,data,y,r,g,b,minx,maxx):\n    '''\n    rng: \u6570\u636e\u5355\u5143\u683c\u533a\u57df\n    'cht: \u56fe\u8868\u5bf9\u8c61\n    'dblX: \u6a2a\u5750\u6807\n    '''\n    kdex=&#91;0 for _ in range(180)]\n    kdef=&#91;0 for _ in range(180)]\n    step=(maxx-minx)\/180\n    for i in range(180):\n        kdex&#91;i]=minx+i*step\n        kdef&#91;i]=y+kde(data,kdex&#91;i],1.5)\n\n    #\u753b\u586b\u5145\u591a\u8fb9\u5f62\n    pt=&#91;&#91;0 for _ in range(2)] for _ in range(183)]\n    for 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])\n    pt&#91;180]&#91;0]=pt&#91;179]&#91;0]\n    pt&#91;180]&#91;1]=shape_y(cht,y)\n    pt&#91;181]&#91;0]=pt&#91;0]&#91;0]\n    pt&#91;181]&#91;1]=shape_y(cht,y)\n    pt&#91;182]&#91;0]=pt&#91;0]&#91;0]\n    pt&#91;182]&#91;1]=pt&#91;0]&#91;1]\n\n    shp=cht.Shapes.AddPolyline(pt)\n    shp.Fill.ForeColor.RGB=xw.utils.rgb_to_int((r,g,b))\n    shp.Fill.OneColorGradient(1,1,1)\n    shp.Fill.Transparency=0.1\n    shp.Line.ForeColor.RGB=xw.utils.rgb_to_int((r,g,b))\n    shp.Line.Weight=1\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\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('A1:H100').value\ncm=wb.sheets('colormap').range('A1:C256').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\nshp.Left=300\nshp.Top=20\nshp.Width=350\nshp.Height=400\ncht=shp.Chart  #\u6dfb\u52a0\u56fe\u8868\ncht.ChartType=-4169\nax1=cht.Axes(1)\nax2=cht.Axes(2)\nax1.MinimumScale=-10\nax1.MaximumScale=10\nax2.MinimumScale=0\nax2.MaximumScale=1.8\nax1.CrossesAt=ax1.MinimumScale\nax2.CrossesAt=ax2.MinimumScale\n\nset_style(cht)\n\n#cht.SeriesCollection().NewSeries()\n\ndd=np.transpose(data)\ndt=&#91;0 for _ in range(8)]\nfor i in range(8):\n    dt&#91;i]=list(dd&#91;i]&#91;:])\n\n#\u7ed8\u5236\u6838\u5bc6\u5ea6\u4f30\u8ba1\u66f2\u7ebf\u56fe\nfor i in range(7,-1,-1):\n    count=int(i\/7 * 256)\n    if count==256:\n        r=cm&#91;255]&#91;0]\n        g=cm&#91;255]&#91;1]\n        b=cm&#91;255]&#91;2]\n    else:\n        r=cm&#91;count]&#91;0]\n        g=cm&#91;count]&#91;1]\n        b=cm&#91;count]&#91;2]\n    draw_kde(cht,dt&#91;i],0.2*i,r,g,b,-10,10)\n\n#\u523b\u5ea6\u6807\u7b7e-\u7eb5\u5750\u6807\nlabel_pos=&#91;0 for _ in range(8)]\nfor i in range(8):\n    label_pos&#91;i]=i*0.2\nlabels=&#91;'A','B','C','D','E','F','G','H']\nfor i in range(8):\n    lf=shape_x(cht,-11)\n    tp=shape_y(cht,label_pos&#91;i]+0.08)\n    wd=cht.PlotArea.InsideWidth\/(cht.Axes(1).MaximumScale-cht.Axes(1).MinimumScale)*1.6\n    ht=cht.PlotArea.InsideHeight\/(cht.Axes(2).MaximumScale-cht.Axes(2).MinimumScale)*0.1\n    shp2=cht.Shapes.AddLabel(1,lf,tp,wd,ht)\n    shp2.TextFrame2.TextRange.Characters().Text=labels&#91;i]\n    shp2.TextFrame2.TextRange.Characters().Font.Size=8\n    shp2.TextFrame2.AutoSize=1    #msoAutoSizeTextToFitShape\n\n#\u523b\u5ea6\u6807\u7b7e-\u6a2a\u5750\u6807\nxlabel_pos=&#91;0 for _ in range(11)]\nxlabels=&#91;0 for _ in range(11)]\nfor i in range(11):\n    xlabel_pos&#91;i]=i*2-10\n    xlabels&#91;i]=str(i*2-10)\nfor i in range(11):\n    lf=shape_x(cht,xlabel_pos&#91;i]-0.5)\n    tp=shape_y(cht,-0.03)\n    wd=cht.PlotArea.InsideWidth\/(cht.Axes(1).MaximumScale-cht.Axes(1).MinimumScale)*1.8\n    ht=cht.PlotArea.InsideHeight\/(cht.Axes(2).MaximumScale-cht.Axes(2).MinimumScale)*0.1\n    shp3=cht.Shapes.AddLabel(1,lf,tp,wd,ht)\n    shp3.TextFrame2.TextRange.Characters().Text=xlabels&#91;i]\n    shp3.TextFrame2.TextRange.Characters().Font.Size=8\n    shp3.TextFrame2.AutoSize=1    #msoAutoSizeTextToFitShape\n  \n#\u5916\u6846\nlf=shape_x(cht,-10)\ntp=shape_y(cht,1.8)\nwd=cht.PlotArea.InsideWidth\/(cht.Axes(1).MaximumScale-cht.Axes(1).MinimumScale)*20\nht=cht.PlotArea.InsideHeight\/(cht.Axes(2).MaximumScale-cht.Axes(2).MinimumScale)*1.8\nshp4=cht.Shapes.AddShape(1,lf,tp,wd,ht)\nshp4.Fill.Visible=False\nshp4.Line.Weight=1\nshp4.Line.ForeColor.RGB=xw.utils.rgb_to_int((200,200,200))\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-46.jpg\" alt=\"\" class=\"wp-image-1564\" srcset=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-46.jpg 880w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-46-300x258.jpg 300w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/05\/3-46-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-633","post","type-post","status-publish","format-standard","hentry","category-xlwings-advanced-chart"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/633","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=633"}],"version-history":[{"count":2,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/633\/revisions"}],"predecessor-version":[{"id":1565,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/633\/revisions\/1565"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}