{"id":5254,"date":"2026-04-28T14:11:49","date_gmt":"2026-04-28T05:11:49","guid":{"rendered":"https:\/\/www.ktech.biz\/jp\/?p=5254"},"modified":"2026-04-28T14:11:49","modified_gmt":"2026-04-28T05:11:49","slug":"xlpack-py-mpl-ex1","status":"publish","type":"post","link":"https:\/\/www.ktech.biz\/jp\/manual2\/xlpack-py-mpl-ex1\/","title":{"rendered":"XLPack for Python \u304a\u3088\u3073 XLPack for Matplotlib \u306e\u4f7f\u7528\u4f8b (1) 3D \u8868\u793a (\u30e9\u30d7\u30e9\u30b9\u65b9\u7a0b\u5f0f)"},"content":{"rendered":"\n\\(x = 0 \\sim 1, y = 0 \\sim 1\\) \u306e\u6b63\u65b9\u5f62\u9818\u57df\u306b\u304a\u3044\u3066, \u6b21\u306e\u504f\u5fae\u5206\u65b9\u7a0b\u5f0f\u3092\u89e3\u304f\u3082\u306e\u3068\u3057\u307e\u3059.<br \/>\n\\[<br \/>\n-d^2u\/dx^2 &#8211; d^2u\/dy^2 = 0<br \/>\n\\]\n\u305f\u3060\u3057, \u5883\u754c\u6761\u4ef6\u306f<br \/>\n\\[<br \/>\nu(x, 0) = 0, u(0, y) = 0, u(x, 1) = x, u(1, y) = y \\space (\u30c7\u30a3\u30ea\u30af\u30ec\u6761\u4ef6)<br \/>\n\\]\n\u3068\u3057\u307e\u3059.<\/p>\n<p>\u3053\u306e\u3088\u3046\u306a\u504f\u5fae\u5206\u65b9\u7a0b\u5f0f\u306f, \u4f8b\u3048\u3070\u71b1\u5e73\u8861\u72b6\u614b\u306b\u3042\u308b\u677f\u306e\u6e29\u5ea6\u5206\u5e03\u306e\u8a18\u8ff0\u306a\u3069\u306b\u9069\u7528\u3055\u308c\u307e\u3059.<\/p>\n<p>\u3053\u306e\u65b9\u7a0b\u5f0f\u306e\u89e3\u6cd5\u3068\u3057\u3066\u306f\u5dee\u5206\u6cd5\u304c\u3088\u304f\u4f7f\u7528\u3055\u308c\u307e\u3059. 5 \u70b9\u5dee\u5206\u8fd1\u4f3c\u306b\u3088\u308a\u6570\u5024\u89e3\u3092\u6c42\u3081\u308b\u4f8b\u306f\u4ee5\u4e0b\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044.<\/p>\n<p><a href=\"\/jp\/tutorial\/16-sparse\/\">16. \u758e\u884c\u5217\u306e\u7dda\u5f62\u8a08\u7b97<\/a><\/p>\n<p>\u3053\u3053\u3067\u306f\u6709\u9650\u8981\u7d20\u6cd5\u3092\u7528\u3044\u3066\u89e3\u3092\u6c42\u3081 3D \u8868\u793a\u3059\u308b\u4f8b\u3092\u8aac\u660e\u3057\u307e\u3059.<\/p>\n<p>\\(x, y\\) \u65b9\u5411\u306b\u6b63\u65b9\u5f62\u9818\u57df\u3092\u305d\u308c\u305e\u308c \\(Nx, Ny\\) \u7b49\u5206\u3057\u3066\u9577\u65b9\u5f62\u683c\u5b50\u306b\u5206\u5272\u3057\u307e\u3059. \u3055\u3089\u306b\u305d\u308c\u305e\u308c\u3092\uff12\u5206\u5272\u3057\u3066\u4e09\u89d2\u8981\u7d20\u3068\u3057\u305f\u5358\u7d14\u30e1\u30c3\u30b7\u30e5\u3092\u751f\u6210\u3059\u308b\u3053\u3068\u306b\u3057\u307e\u3059.<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.ktech.biz\/jp\/wp-content\/uploads\/sites\/2\/2026\/04\/Example_PY_MPL_1.png\" alt=\"\" width=\"457\" height=\"457\" class=\"aligncenter size-full wp-image-5258\" srcset=\"https:\/\/www.ktech.biz\/jp\/wp-content\/uploads\/sites\/2\/2026\/04\/Example_PY_MPL_1.png 457w, https:\/\/www.ktech.biz\/jp\/wp-content\/uploads\/sites\/2\/2026\/04\/Example_PY_MPL_1-300x300.png 300w, https:\/\/www.ktech.biz\/jp\/wp-content\/uploads\/sites\/2\/2026\/04\/Example_PY_MPL_1-150x150.png 150w, https:\/\/www.ktech.biz\/jp\/wp-content\/uploads\/sites\/2\/2026\/04\/Example_PY_MPL_1-144x144.png 144w\" sizes=\"auto, (max-width: 457px) 100vw, 457px\" \/><\/p>\n<p>\u3053\u308c\u3092\u7528\u3044\u3066\u6709\u9650\u8981\u7d20\u6cd5\u306b\u3088\u308a\u89e3\u3092\u6c42\u3081\u307e\u3059. \u306a\u304a, \u4e0a\u56f3\u306f 7 \u7b49\u5206 (\u683c\u5b50\u70b9\u6570 8 x 8)\u306e\u4f8b\u3092\u793a\u3057\u307e\u3059\u304c, \u5b9f\u969b\u306e\u8a08\u7b97\u3067\u306f 19 \u7b49\u5206 (\u683c\u5b50\u70b9\u6570 20 x 20) (Nx = 19, Ny = 19) \u3068\u3057\u307e\u3057\u305f.<\/p>\n<h2>1. \u5168\u4f53\u306e\u69cb\u6210<\/h2>\n<p>\u30e1\u30a4\u30f3\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u4ee5\u4e0b\u306e\u3068\u304a\u308a\u3067\u3059.<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-vb\" data-lang=\"VBA\"><code>Sub Main()\r\n    Const Nx = 19, Ny = 19\r\n    Const Nb2 = 0, LdKnc = 4, LdKs = 3\r\n    Const N = (Nx + 1) * (Ny + 1), Ne = 2 * Nx * Ny, Nb = 2 * (Nx + Ny)\r\n    Dim X(N - 1) As Double, Y(N - 1) As Double\r\n    Dim P(N - 1) As Double, Q(N - 1) As Double, F(N - 1) As Double\r\n    Dim Knc(LdKnc - 1, Ne - 1) As Long, Ks(LdKs - 1, Nb - 1) As Long\r\n    Dim Lb(Nb - 1) As Long, Ib(Nb - 1) As Long, Bv(Nb - 1) As Double\r\n    Dim Ks2() As Long, Alpha() As Double, Beta() As Double\r\n    Dim Val(20 * N) As Double, Ptr(N) As Long, Ind(20 * N) As Long\r\n    Dim B(N - 1) As Double, U(N - 1) As Double\r\n    Dim Ux As Double, Err As Double\r\n    Dim Info As Long\r\n    Dim I As Long, J As Long\r\n    '-- Set mesh data\r\n    Call SetData(Nx, Ny, N, Ne, Nb, X(), Y(), Knc(), Ks(), Lb(), P(), Q(), F(), Ib(), Bv())\r\n    '-- Assemble FEM matrix\r\n    Call Fem2p(N, Ne, X(), Y(), Knc(), P(), Q(), F(), Nb, Ib(), Bv(), Nb2, Ks2(), Alpha(), Beta(), Val(), Ptr(), Ind(), B(), Info)\r\n    '-- Solve equation by CG method\r\n    Dim Iter As Long, Res As Double\r\n    Call Cg1(N, Val(), Ptr(), Ind(), B(), U(), Info, Iter, Res)\r\n    Debug.Print \"Info =\" + Str(Info) + \", Iter =\" + Str(Iter) + \", Res =\" + Str(Res)\r\n    If Info &lt;&gt; 0 Then Exit Sub\r\n    '-- Plot solution\r\n    Call Plot(Nx, Ny, U())\r\nEnd Sub<\/code><\/pre>\n<\/div>\n<p>XLPack \u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u30b5\u30d6\u30eb\u30fc\u30c1\u30f3 Fem2p() \u306f\u6709\u9650\u8981\u7d20\u884c\u5217\u306e\u7d44\u307f\u7acb\u3066\u3092\u884c\u3044\u307e\u3059. \u758e\u884c\u5217 (CSR) \u5f62\u5f0f\u306e\u4fc2\u6570\u884c\u5217\u3068\u53f3\u8fba\u30d9\u30af\u30c8\u30eb\u304b\u3089\u306a\u308b\u65b9\u7a0b\u5f0f\u304c\u5f97\u3089\u308c\u307e\u3059. \u4fc2\u6570\u884c\u5217\u306f\u5bfe\u79f0\u884c\u5217\u306a\u306e\u3067, \u65b9\u7a0b\u5f0f\u306f XLPack \u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u30b5\u30d6\u30eb\u30fc\u30c1\u30f3 Cg1() \u3092\u4f7f\u3063\u3066\u5171\u5f79\u52fe\u914d\u6cd5\u306b\u3088\u308a\u89e3\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059.<\/p>\n<p>\u30b5\u30d6\u30eb\u30fc\u30c1\u30f3 SetData() \u306f Fem2p() \u306e\u5165\u529b\u3068\u306a\u308b\u683c\u5b50\u70b9, \u6709\u9650\u8981\u7d20, \u304a\u3088\u3073, \u5883\u754c\u6761\u4ef6\u306e\u30c7\u30fc\u30bf\u3092\u8a2d\u5b9a\u3057\u307e\u3059.<\/p>\n<p>\u5f97\u3089\u308c\u305f\u89e3\u306f\u30b5\u30d6\u30eb\u30fc\u30c1\u30f3 Plot() \u306b\u3088\u308a\u8868\u793a\u3055\u308c\u307e\u3059. Plot() \u3067\u306f XLPack for Python \u307e\u305f\u306f XLPack for Matplotlib \u3092\u4f7f\u7528\u3057\u3066 3D \u8868\u793a\u3092\u884c\u3044\u307e\u3059.<\/p>\n<p>\u5b9f\u884c\u7d50\u679c\u306f\u6b21\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.ktech.biz\/jp\/wp-content\/uploads\/sites\/2\/2026\/04\/Example_FEM.png\" alt=\"\" width=\"482\" height=\"408\" class=\"aligncenter size-full wp-image-5259\" srcset=\"https:\/\/www.ktech.biz\/jp\/wp-content\/uploads\/sites\/2\/2026\/04\/Example_FEM.png 482w, https:\/\/www.ktech.biz\/jp\/wp-content\/uploads\/sites\/2\/2026\/04\/Example_FEM-300x254.png 300w\" sizes=\"auto, (max-width: 482px) 100vw, 482px\" \/><\/p>\n<h2>2. \u5165\u529b\u30c7\u30fc\u30bf\u306e\u8a2d\u5b9a<\/h2>\n<p>\u4e09\u89d2\u8981\u7d20\u306b\u3088\u308b\u5358\u7d14\u30e1\u30c3\u30b7\u30e5\u306f XLPack \u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u30b5\u30d6\u30eb\u30fc\u30c1\u30f3 Mesh23() \u3092\u4f7f\u3063\u3066\u751f\u6210\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059.<\/p>\n<p>\u6b21\u306b, \u30e9\u30d7\u30e9\u30b9\u65b9\u7a0b\u5f0f\u306e\u4fc2\u6570 p(x, y) = 1, q(x, y) = 0, f(x, y) = 0 \u3092\u8a2d\u5b9a\u3057\u307e\u3059. \u307e\u305f, \u30c7\u30a3\u30ea\u30af\u30ec\u5883\u754c\u6761\u4ef6\u3092\u8a2d\u5b9a\u3057\u307e\u3059.<\/p>\n<p>\u3053\u308c\u3089\u3092\u884c\u3046\u306e\u304c\u6b21\u306e\u30b5\u30d6\u30eb\u30fc\u30c1\u30f3\u3067\u3059.<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-vb\" data-lang=\"VBA\"><code>Sub SetData(Nx As Long, Ny As Long, N As Long, Ne As Long, Nb As Long, X() As Double, Y() As Double, Knc() As Long, Ks() As Long, Lb() As Long, P() As Double, Q() As Double, F() As Double, Ib() As Long, Bv() As Double)\r\n    Dim Id() As Long\r\n    Dim I As Long, J As Long, K As Long\r\n    '-- Generates simple rectangular mesh for 2D triangular element\r\n    Call Mesh23(Nx, Ny, X(), Y(), Knc(), Ks(), Lb())\r\n    '-- Set p(x, y) = 1, q(x, y) = 0 and f(x, y) = 0\r\n    For I = 0 To N - 1\r\n        P(I) = 1\r\n        Q(I) = 0\r\n        F(I) = 0\r\n    Next\r\n    '-- Set boundary condition 1\r\n    ReDim Id(N - 1)\r\n    For I = 0 To Nb - 1\r\n        For J = 1 To 2\r\n            Id(Ks(J, I) - 1) = Lb(I)\r\n        Next\r\n    Next\r\n    K = 0\r\n    For I = 0 To N - 1\r\n        If Id(I) = 1 Or Id(I) = 4 Then\r\n            Bv(K) = 0\r\n        ElseIf Id(I) = 2 Then\r\n            Bv(K) = Y(I)\r\n        ElseIf Id(I) = 3 Then\r\n            Bv(K) = X(I)\r\n        Else\r\n            GoTo Continue\r\n        End If\r\n        Ib(K) = I + 1\r\n        K = K + 1\r\nContinue:\r\n    Next\r\nEnd Sub<\/code><\/pre>\n<\/div>\n<h2>3. \u89e3\u306e 3D \u8868\u793a<\/h2>\n<p>\u5f97\u3089\u308c\u305f\u89e3\u3092 \u8868\u793a\u3059\u308b Python \u306e\u30b3\u30fc\u30c9\u3092 XLPack for Python \u3092\u4f7f\u3063\u3066\u76f4\u63a5\u8a18\u8ff0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059.<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-vb\" data-lang=\"VBA\"><code>Sub Plot(Nx As Long, Ny As Long, U() As Double)\r\n    Dim I As Long\r\n    For I = 0 To (Nx + 1) * (Ny + 1) - 1\r\n        Cells(6 + I, 2) = U(I)\r\n    Next\r\n    '-- Python codes\r\n    PY \"import matplotlib.pyplot as plt\"\r\n    PY \"from matplotlib import cm\"\r\n    PY \"import numpy as np\"\r\n    PY \"from XLPackPy import *\"\r\n    PY \"nx =\" + Str(Nx) + \"+ 1\"\r\n    PY \"ny =\" + Str(Ny) + \"+ 1\"\r\n    PY \"U = xlc_get(5, 1, 4 + nx*ny)\"\r\n    PY \"X = np.empty((ny, nx))\"\r\n    PY \"Y = np.empty((ny, nx))\"\r\n    PY \"Z = np.empty((ny, nx))\"\r\n    PY _\r\n      \"for i in range(ny):\" + vbNewLine + _\r\n      \"  for j in range(nx):\" + vbNewLine + _\r\n      \"    X[i][j] = (1.0\/(nx - 1))*j\" + vbNewLine + _\r\n      \"    Y[i][j] = (1.0\/(ny - 1))*i\" + vbNewLine + _\r\n      \"    Z[i][j] = U[nx*i + j]\"\r\n    PY \"fig = plt.figure()\"\r\n    PY \"ax3 = fig.add_subplot(projection='3d')\"\r\n    PY \"surf = ax3.plot_surface(X, Y, Z, cmap=cm.coolwarm)\"\r\n    PY \"fig.colorbar(surf, shrink=0.5, aspect=5)\"\r\n    PY \"plt.show()\"\r\nEnd Sub<\/code><\/pre>\n<\/div>\n<p>XLPack for Matplotlib \u3092\u4f7f\u3046\u3068\u5168\u3066 VBA \u3067\u8a18\u8ff0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059. Fig.Add_subplot_3d() \u306f Python \u306e fig.add_subplot(projection=&#8217;3d&#8217;) \u306b\u76f8\u5f53\u3057\u307e\u3059. \u305d\u306e\u4ed6\u306f Python \u306e\u30b3\u30fc\u30c9\u3068\u307b\u307c\u540c\u3058\u306b\u306a\u308a\u307e\u3059.<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-vb\" data-lang=\"VBA\"><code>Dim Plt As New Pyplot\r\n\r\nSub Plot(Nx As Long, Ny As Long, U() As Double)\r\n    Dim X() As Double, Y() As Double, Z() As Double\r\n    ReDim X(Ny, Nx), Y(Ny, Nx), Z(Ny, Nx)\r\n    Dim I As Long, J As Long\r\n    Dim Fig As Figure, Ax3 As Axs3d, Surf As PyObject\r\n    '-- Prepare data\r\n    For I = 0 To Ny\r\n        For J = 0 To Nx\r\n            X(I, J) = (1# \/ Nx) * I\r\n            Y(I, J) = (1# \/ Ny) * J\r\n            Z(I, J) = U((Nx + 1) * I + J)\r\n        Next\r\n    Next\r\n    '-- Plot by Matplotlib\r\n    Set Fig = Plt.Figure()\r\n    Set Ax3 = Fig.Add_subplot_3d()\r\n    Set Surf = Ax3.Plot_surface(Ny + 1, Nx + 1, X(), Y(), Z(), \"coolwarm\")\r\n    Call Fig.Colorbar(Surf, KwArgs:=\"shrink=0.5, aspect=5\")\r\n    Call Plt.Show\r\nEnd Sub<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\\(x = 0 \\sim 1, y = 0 \\sim 1\\) \u306e\u6b63\u65b9\u5f62\u9818\u57df\u306b\u304a\u3044\u3066, \u6b21\u306e\u504f\u5fae\u5206\u65b9\u7a0b\u5f0f\u3092\u89e3\u304f\u3082\u306e\u3068\u3057\u307e\u3059. \\[ -d^2u\/dx^2 &#8211; d^2u\/dy^2 = 0 \\] \u305f\u3060\u3057, \u5883\u754c\u6761\u4ef6 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[49],"tags":[],"class_list":["post-5254","post","type-post","status-publish","format-standard","hentry","category-manual2"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.ktech.biz\/jp\/wp-json\/wp\/v2\/posts\/5254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ktech.biz\/jp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ktech.biz\/jp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ktech.biz\/jp\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ktech.biz\/jp\/wp-json\/wp\/v2\/comments?post=5254"}],"version-history":[{"count":9,"href":"https:\/\/www.ktech.biz\/jp\/wp-json\/wp\/v2\/posts\/5254\/revisions"}],"predecessor-version":[{"id":5324,"href":"https:\/\/www.ktech.biz\/jp\/wp-json\/wp\/v2\/posts\/5254\/revisions\/5324"}],"wp:attachment":[{"href":"https:\/\/www.ktech.biz\/jp\/wp-json\/wp\/v2\/media?parent=5254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ktech.biz\/jp\/wp-json\/wp\/v2\/categories?post=5254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ktech.biz\/jp\/wp-json\/wp\/v2\/tags?post=5254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}