{"id":5265,"date":"2026-04-28T14:10:46","date_gmt":"2026-04-28T05:10:46","guid":{"rendered":"https:\/\/www.ktech.biz\/jp\/?p=5265"},"modified":"2026-04-28T14:10:46","modified_gmt":"2026-04-28T05:10:46","slug":"xlpack-py-mpl-ex2","status":"publish","type":"post","link":"https:\/\/www.ktech.biz\/jp\/manual2\/xlpack-py-mpl-ex2\/","title":{"rendered":"XLPack for Python \u304a\u3088\u3073 XLPack for Matplotlib \u306e\u4f7f\u7528\u4f8b (2) \u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3 (\u5358\u632f\u308a\u5b50)"},"content":{"rendered":"\n\u4e0b\u56f3\u306e\u3088\u3046\u306b, \u9577\u3055 \\(l\\) \u306e\u7cf8 (\u91cd\u3055\u306f\u7121\u8996\u3067\u304d\u308b\u3082\u306e\u3068\u3057\u307e\u3059) \u3067\u540a\u308a\u4e0b\u3052\u3089\u308c\u305f\u91cd\u3055 \\(m\\) \u306e\u91cd\u308a\u304c\u5de6\u53f3\u306b\u632f\u308c\u3066\u3044\u308b\u3082\u306e\u3068\u3057\u307e\u3059. \u7cf8\u304c\u5782\u76f4\u306a\u72b6\u614b\u304b\u3089\u306e\u89d2\u5ea6\u3092 \\(\\theta\\) \u3067\u8868\u3057\u307e\u3059. \u3053\u308c\u3092\u5358\u632f\u308a\u5b50\u3068\u3044\u3044\u307e\u3059.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.ktech.biz\/jp\/wp-content\/uploads\/sites\/2\/2026\/04\/Example_PY_MPL_2.png\" alt=\"\" width=\"486\" height=\"364\" class=\"aligncenter size-full wp-image-5268\" srcset=\"https:\/\/www.ktech.biz\/jp\/wp-content\/uploads\/sites\/2\/2026\/04\/Example_PY_MPL_2.png 486w, https:\/\/www.ktech.biz\/jp\/wp-content\/uploads\/sites\/2\/2026\/04\/Example_PY_MPL_2-300x225.png 300w\" sizes=\"auto, (max-width: 486px) 100vw, 486px\" \/><\/p>\n<p>\u3053\u306e\u904b\u52d5\u306f, \u6642\u9593\u3092 \\(t\\) \u3068\u3057\u3066\u6b21\u306e\u5fae\u5206\u65b9\u7a0b\u5f0f\u3067\u8868\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059. \u306a\u304a, \\(g\\) \u306f\u91cd\u529b\u52a0\u901f\u5ea6 \\((9.81 m\/s^2)\\) \u3067\u3059.<\/p>\n<p>\\(d^2\\theta\/dt^2 = -(mg\/l)sin\\theta\\)<\/p>\n<p>\u3053\u3053\u3067\u306f\u7c21\u5358\u306e\u305f\u3081\u306b \\(m = 1\\) \u3068\u3057, \u521d\u671f\u6761\u4ef6\u3092 \\(\\theta = \\pi\/4, d\\theta\/dt = 0\\) \u3068\u3057\u3066 \\(t = 0 \\sim 10\\) \u306b\u304a\u3051\u308b\u6570\u5024\u89e3\u3092\u6c42\u3081, \u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u3067\u8868\u793a\u3059\u308b\u3053\u3068\u306b\u3057\u307e\u3059.<\/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    Dim N As Long, T As Double, Y() As Double, Yp() As Double\r\n    Dim Nt As Long, Tend As Double\r\n    Dim X1() As Double, Y1() As Double\r\n    Dim Info As Long\r\n    N = 1\r\n    ReDim Y(N - 1), Yp(N - 1)\r\n    '-- Initial values\r\n    T = 0\r\n    Y(0) = Atn(1)   ' Pi() \/ 4\r\n    Yp(0) = 0\r\n    '-- Solve ODE\r\n    Tend = 10\r\n    Nt = 200\r\n    ReDim X1(Nt - 1), Y1(Nt - 1)\r\n    Call ODE_2(N, T, Y(), Yp(), Nt, Tend, X1(), Y1(), Info)\r\n    If Info <> 0 Then Exit Sub\r\n    '-- Display animation\r\n    Call Animate(Nt, X1(), Y1(), Tend \/ Nt)\r\nEnd Sub<\/code><\/pre>\n<\/div>\n<p>\u30b5\u30d6\u30eb\u30fc\u30c1\u30f3 ODE_2() \u306f\u5fae\u5206\u65b9\u7a0b\u5f0f\u306e\u89e3\u3092\u6c42\u3081\u307e\u3059. \u5f97\u3089\u308c\u305f\u89e3\u306f\u30b5\u30d6\u30eb\u30fc\u30c1\u30f3 Animate() \u306b\u3088\u308a\u8868\u793a\u3055\u308c\u307e\u3059. Animate() \u3067\u306f XLPack for Python \u307e\u305f\u306f XLPack for Matplotlib \u3092\u4f7f\u7528\u3057\u3066\u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u3067\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><video width=\"640\" height=\"480\" controls><source src=\"\/jp\/wp-content\/uploads\/sites\/2\/2026\/04\/Example_Pend.mp4\" type=\"video\/mp4\">Your browser does not support the video tag.<\/video><\/p>\n<h2>2. \u5fae\u5206\u65b9\u7a0b\u5f0f\u306e\u89e3<\/h2>\n<p>\u5358\u632f\u308a\u5b50\u306e\u5fae\u5206\u65b9\u7a0b\u5f0f\u306f 2 \u968e\u5e38\u5fae\u5206\u65b9\u7a0b\u5f0f\u306a\u306e\u3067, \u901a\u5e38\u306f\u6b21\u306e\u3088\u3046\u306b 1 \u968e\u306e\u9023\u7acb\u5e38\u5fae\u5206\u65b9\u7a0b\u5f0f\u306e\u5f62\u306b\u3057\u3066\u89e3\u304d\u307e\u3059.<\/p>\n<p>\\(d\\theta_1\/dt = \\theta_2\\)<br \/>\n\\(d\\theta_2\/dt = -(mg\/l)sin\\theta_1\\)<\/p>\n<p>\u3057\u304b\u3057, \u3053\u306e\u5834\u5408\u306f 1 \u968e\u306e\u9805\u306b\u4f9d\u5b58\u3057\u306a\u3044\u7279\u5225\u306a\u5f62\u3092\u3057\u3066\u3044\u308b\u305f\u3081 XLPack \u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u30b5\u30d6\u30eb\u30fc\u30c1\u30f3 Dopn43() (\u30eb\u30f3\u30b2\u30fb\u30af\u30c3\u30bf\u30fb\u30cb\u30e5\u30b9\u30c8\u30ec\u30e0\u6cd5) \u3092\u4f7f\u3063\u3066\u305d\u306e\u307e\u307e\u306e\u5f62\u3067\u89e3\u304f\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>Const L = 1, M = 1, G = 9.81\r\nConst Mode = 3\r\n\r\nSub F_2(N As Long, T As Double, Y() As Double, Ypp() As Double)\r\n    Ypp(0) = -(M * G \/ L) * Sin(Y(0))\r\nEnd Sub\r\n\r\nSub ODE_2(N As Long, T As Double, Y() As Double, Yp() As Double, Nt As Long, Tend As Double, X1() As Double, Y1() As Double, Info As Long)\r\n    Dim RTol(0) As Double, ATol(0) As Double\r\n    Dim Dt As Double, Tout As Double, I As Long\r\n    RTol(0) = 0.0000000001 '1e-10\r\n    ATol(0) = RTol(0)\r\n    Dt = Tend \/ Nt\r\n    Info = 0\r\n    For I = 0 To Nt - 1\r\n        Tout = (I + 1) * Dt\r\n        Call Dopn43(N, AddressOf F_2, T, Y(), Yp(), Tout, Tend, RTol(), ATol(), Mode, Info)\r\n        If Info < 0 Or Info > 10 Then\r\n            MsgBox (\"Error in Dopn43: Info =\" + Str(Info))\r\n            Exit Sub\r\n        End If\r\n        X1(I) = L * Sin(Y(0))\r\n        Y1(I) = -L * Cos(Y(0))\r\n    Next\r\nEnd Sub<\/code><\/pre>\n<\/div>\n<p>\u5f97\u3089\u308c\u305f\u89e3\u306f, XY \u5ea7\u6a19\u306b\u5909\u63db\u3057\u3066 X1(), Y1() \u306b\u8fd4\u3057\u307e\u3059.<\/p>\n<h2>3. \u89e3\u306e\u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\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<p>\u914d\u5217\u30c7\u30fc\u30bf\u306f\u305d\u306e\u307e\u307e\u6e21\u3059\u3053\u3068\u304c\u3067\u304d\u306a\u3044\u306e\u3067, \u3044\u3063\u305f\u3093\u30ef\u30fc\u30af\u30b7\u30fc\u30c8\u306b\u66f8\u304d\u51fa\u3057\u3066\u304b\u3089 Python \u5074\u3067 xlc_get() \u3092\u4f7f\u3063\u3066\u8aad\u307f\u8fbc\u3093\u3067\u3044\u307e\u3059.<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-vb\" data-lang=\"VBA\"><code>Sub Animate(Nt As Long, X1() As Double, Y1() As Double, Dt As Double)\r\n    Dim I As Long\r\n    For I = 0 To Nt - 1\r\n        Cells(6 + I, 2) = X1(I)\r\n        Cells(6 + I, 3) = Y1(I)\r\n    Next\r\n    '-- Python codes\r\n    PY \"import matplotlib.pyplot as plt\"\r\n    PY \"import matplotlib.animation as animation\"\r\n    PY \"from XLPackPy import *\"\r\n    PY \"nt =\" + Str(Nt)\r\n    PY \"dt =\" + Str(Dt)\r\n    PY \"x1 = xlc_get(5, 1, 4 + nt)\"\r\n    PY \"y1 = xlc_get(5, 2, 4 + nt)\"\r\n    PY \"fig = plt.figure()\"\r\n    PY \"ax = fig.gca()\"\r\n    PY \"ax.set_xlim(-1.0, 1.0)\"\r\n    PY \"ax.set_ylim(-1.5, 0.5)\"\r\n    PY \"ax.set_aspect('equal')\"\r\n    PY \"ax.grid()\"\r\n    PY \"ax.set_title('Animation (Pendulum)')\"\r\n    PY \"xt = []\"\r\n    PY \"yt = []\"\r\n    PY \"artist = []\"\r\n    PY _\r\n      \"for i in range(nt):\" + vbNewLine + _\r\n      \"  pend, = ax.plot([0, x1[i]], [0, y1[i]], 'o-', color = 'b', lw=2)\" + vbNewLine + _\r\n      \"  xt.append(x1[i])\" + vbNewLine + _\r\n      \"  yt.append(y1[i])\" + vbNewLine + _\r\n      \"  trace, = ax.plot(xt, yt, ':c', lw=1)\" + vbNewLine + _\r\n      \"  artist.append((pend, trace))\"\r\n    PY \"ani = animation.ArtistAnimation(fig, artist, interval = dt * 1000)\"\r\n    PY \"plt.show()\"\r\nEnd Sub<\/code><\/pre>\n<\/div>\n<p>XLPack for Matplotlib \u3092\u4f7f\u3046\u3068\u540c\u3058\u51e6\u7406\u3092\u5168\u3066 VBA \u3067\u8a18\u8ff0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059. \u5185\u90e8\u7684\u306b\u306f Python \u3092\u547c\u3073\u51fa\u3057\u3066\u51e6\u7406\u3057\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\nDim Animation As New Animation\r\n\r\nSub Animate(Nt As Long, X1() As Double, Y1() As Double, Dt As Double)\r\n    Dim Fig As Figure, Ax As Axs, Artist() As PyObject, Anim As Animation\r\n    Dim X(1) As Double, Y(1) As Double\r\n    Dim I As Long\r\n    ReDim Artist(Nt - 1, 1)\r\n    Set Fig = Plt.Figure()\r\n    Set Ax = Fig.Gca()\r\n    Call Ax.Set_xlim(-1, 1)\r\n    Call Ax.Set_ylim(-1.5, 0.5)\r\n    Call Ax.Set_aspect(1)\r\n    Call Ax.Grid\r\n    Call Ax.Set_title(\"Animation (Pendulum)\")\r\n    X(0) = 0: Y(0) = 0\r\n    For I = 0 To Nt - 1\r\n        X(1) = X1(I): Y(1) = Y1(I)\r\n        Set Artist(I, 0) = Ax.Plot(2, X(), Y(), \"o-\", \"color=b,lw=2\")\r\n        Set Artist(I, 1) = Ax.Plot(I + 1, X1(), Y1(), \":c\", \"lw=1\")\r\n    Next\r\n    Set Anim = Animation.ArtistAnimation(Fig, Nt, 2, Artist(), \"interval =\" + Str(Dt * 1000))\r\n    Call Plt.Show\r\nEnd Sub<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u56f3\u306e\u3088\u3046\u306b, \u9577\u3055 \\(l\\) \u306e\u7cf8 (\u91cd\u3055\u306f\u7121\u8996\u3067\u304d\u308b\u3082\u306e\u3068\u3057\u307e\u3059) \u3067\u540a\u308a\u4e0b\u3052\u3089\u308c\u305f\u91cd\u3055 \\(m\\) \u306e\u91cd\u308a\u304c\u5de6\u53f3\u306b\u632f\u308c\u3066\u3044\u308b\u3082\u306e\u3068\u3057\u307e\u3059. \u7cf8\u304c\u5782\u76f4\u306a\u72b6\u614b\u304b\u3089\u306e\u89d2\u5ea6\u3092 \\(\\theta\\) \u3067\u8868\u3057\u307e\u3059. \u3053\u308c\u3092\u5358 [&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-5265","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\/5265","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=5265"}],"version-history":[{"count":10,"href":"https:\/\/www.ktech.biz\/jp\/wp-json\/wp\/v2\/posts\/5265\/revisions"}],"predecessor-version":[{"id":5323,"href":"https:\/\/www.ktech.biz\/jp\/wp-json\/wp\/v2\/posts\/5265\/revisions\/5323"}],"wp:attachment":[{"href":"https:\/\/www.ktech.biz\/jp\/wp-json\/wp\/v2\/media?parent=5265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ktech.biz\/jp\/wp-json\/wp\/v2\/categories?post=5265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ktech.biz\/jp\/wp-json\/wp\/v2\/tags?post=5265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}