{"id":927614,"date":"2024-12-26T16:15:40","date_gmt":"2024-12-26T08:15:40","guid":{"rendered":""},"modified":"2024-12-26T16:15:42","modified_gmt":"2024-12-26T08:15:42","slug":"python%e5%a6%82%e4%bd%95%e7%94%bb%e5%90%91%e9%87%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/927614.html","title":{"rendered":"python\u5982\u4f55\u753b\u5411\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063641\/8123aed1-b707-4edb-bf99-dc4bef66fda4.webp\" alt=\"python\u5982\u4f55\u753b\u5411\u91cf\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u753b\u5411\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u4e2d\u7684<code>quiver<\/code>\u51fd\u6570\u3001<code>annotate<\/code>\u51fd\u6570\u3001<code>arrow<\/code>\u51fd\u6570\u3002<\/strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u7ed8\u5236\u5411\u91cf\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528Matplotlib\u7684<code>quiver<\/code>\u51fd\u6570<\/p>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u6d41\u884c\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u51fd\u6570\u6765\u7ed8\u5236\u5404\u79cd\u56fe\u5f62\u3002\u5728\u7ed8\u5236\u5411\u91cf\u65f6\uff0c<code>quiver<\/code>\u51fd\u6570\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\u3002<code>quiver<\/code>\u51fd\u6570\u53ef\u4ee5\u5728\u4e8c\u7ef4\u5e73\u9762\u4e0a\u7ed8\u5236\u5411\u91cf\u573a\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5Matplotlib<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5Matplotlib\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528quiver\u7ed8\u5236\u5411\u91cf<\/strong><\/li>\n<\/ol>\n<p><p><code>quiver<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u7ed8\u5236\u4e8c\u7ef4\u5411\u91cf\uff0c\u5b83\u7684\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u5411\u91cf\u7684\u8d77\u59cb\u70b9\u548c\u65b9\u5411<\/strong><\/h2>\n<p>X, Y = np.array([0]), np.array([0])  # \u8d77\u59cb\u70b9<\/p>\n<p>U, V = np.array([1]), np.array([2])  # \u5411\u91cf\u7684x\u548cy\u65b9\u5411<\/p>\n<p>plt.quiver(X, Y, U, V, angles=&#39;xy&#39;, scale_units=&#39;xy&#39;, scale=1)<\/p>\n<p>plt.xlim(-1, 2)<\/p>\n<p>plt.ylim(-1, 3)<\/p>\n<p>plt.xlabel(&#39;X-axis&#39;)<\/p>\n<p>plt.ylabel(&#39;Y-axis&#39;)<\/p>\n<p>plt.title(&#39;Vector using quiver&#39;)<\/p>\n<p>plt.grid()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5411\u91cf\u4ece\u539f\u70b9(0,0)\u5f00\u59cb\uff0c\u6307\u5411(1,2)\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528Matplotlib\u7684<code>annotate<\/code>\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>annotate<\/code>\u51fd\u6570\u5728Matplotlib\u4e2d\u4e3b\u8981\u7528\u4e8e\u6dfb\u52a0\u6ce8\u91ca\uff0c\u4f46\u6211\u4eec\u4e5f\u53ef\u4ee5\u7528\u5b83\u6765\u7ed8\u5236\u7b80\u5355\u7684\u7bad\u5934\u4ee5\u4ee3\u8868\u5411\u91cf\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528annotate\u7ed8\u5236\u5411\u91cf<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u5b9a\u4e49\u8d77\u59cb\u70b9\u548c\u7ec8\u70b9<\/strong><\/h2>\n<p>start = [0, 0]<\/p>\n<p>end = [1, 2]<\/p>\n<p>plt.figure()<\/p>\n<p>plt.annotate(&#39;&#39;, xy=end, xytext=start,<\/p>\n<p>             arrowprops=dict(facecolor=&#39;black&#39;, shrink=0.05))<\/p>\n<p>plt.xlim(-1, 2)<\/p>\n<p>plt.ylim(-1, 3)<\/p>\n<p>plt.xlabel(&#39;X-axis&#39;)<\/p>\n<p>plt.ylabel(&#39;Y-axis&#39;)<\/p>\n<p>plt.title(&#39;Vector using annotate&#39;)<\/p>\n<p>plt.grid()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>annotate<\/code>\u51fd\u6570\u7528\u4e8e\u4ece\u8d77\u59cb\u70b9\u7ed8\u5236\u5230\u7ec8\u70b9\u7684\u7bad\u5934\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528Matplotlib\u7684<code>arrow<\/code>\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>arrow<\/code>\u51fd\u6570\u662fMatplotlib\u4e2d\u7ed8\u5236\u7bad\u5934\u7684\u53e6\u4e00\u79cd\u65b9\u6cd5\uff0c\u867d\u7136\u76f8\u5bf9\u7b80\u5355\uff0c\u4f46\u5728\u7ed8\u5236\u5355\u4e2a\u5411\u91cf\u65f6\u975e\u5e38\u6709\u6548\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528arrow\u7ed8\u5236\u5411\u91cf<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u5b9a\u4e49\u8d77\u59cb\u70b9\u548c\u65b9\u5411<\/strong><\/h2>\n<p>plt.figure()<\/p>\n<p>plt.arrow(0, 0, 1, 2, head_width=0.1, head_length=0.2, fc=&#39;blue&#39;, ec=&#39;blue&#39;)<\/p>\n<p>plt.xlim(-1, 2)<\/p>\n<p>plt.ylim(-1, 3)<\/p>\n<p>plt.xlabel(&#39;X-axis&#39;)<\/p>\n<p>plt.ylabel(&#39;Y-axis&#39;)<\/p>\n<p>plt.title(&#39;Vector using arrow&#39;)<\/p>\n<p>plt.grid()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>arrow<\/code>\u51fd\u6570\u7528\u4e8e\u4ece\u8d77\u59cb\u70b9(0,0)\u7ed8\u5236\u4e00\u4e2a\u6307\u5411(1,2)\u7684\u7bad\u5934\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u7ed8\u5236\u591a\u4e2a\u5411\u91cf\u548c\u5411\u91cf\u573a<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u7ed8\u5236\u591a\u4e2a\u5411\u91cf\u6216\u8005\u5411\u91cf\u573a\u3002\u8fd9\u65f6\uff0c<code>quiver<\/code>\u51fd\u6570\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5b8c\u6210\u8fd9\u4e9b\u4efb\u52a1\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u7ed8\u5236\u591a\u4e2a\u5411\u91cf<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u591a\u4e2a\u5411\u91cf\u7684\u8d77\u59cb\u70b9\u548c\u65b9\u5411<\/strong><\/h2>\n<p>X, Y = np.array([0, 0, 0]), np.array([0, 0, 0])<\/p>\n<p>U, V = np.array([1, 2, 3]), np.array([3, 2, 1])<\/p>\n<p>plt.quiver(X, Y, U, V, angles=&#39;xy&#39;, scale_units=&#39;xy&#39;, scale=1)<\/p>\n<p>plt.xlim(-1, 4)<\/p>\n<p>plt.ylim(-1, 4)<\/p>\n<p>plt.xlabel(&#39;X-axis&#39;)<\/p>\n<p>plt.ylabel(&#39;Y-axis&#39;)<\/p>\n<p>plt.title(&#39;Multiple Vectors using quiver&#39;)<\/p>\n<p>plt.grid()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7ed8\u5236\u5411\u91cf\u573a<\/strong><\/li>\n<\/ol>\n<p><p>\u5411\u91cf\u573a\u662f\u6307\u5728\u4e8c\u7ef4\u5e73\u9762\u4e0a\u6bcf\u4e00\u70b9\u90fd\u6709\u4e00\u4e2a\u5411\u91cf\u7684\u60c5\u51b5\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>meshgrid<\/code>\u51fd\u6570\u751f\u6210\u7f51\u683c\uff0c\u7136\u540e\u4f7f\u7528<code>quiver<\/code>\u7ed8\u5236\u5411\u91cf\u573a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<p>x = np.arange(-2, 2, 0.5)<\/p>\n<p>y = np.arange(-2, 2, 0.5)<\/p>\n<p>X, Y = np.meshgrid(x, y)<\/p>\n<h2><strong>\u5b9a\u4e49\u5411\u91cf\u573a\u7684\u65b9\u5411<\/strong><\/h2>\n<p>U = -Y<\/p>\n<p>V = X<\/p>\n<p>plt.quiver(X, Y, U, V)<\/p>\n<p>plt.xlabel(&#39;X-axis&#39;)<\/p>\n<p>plt.ylabel(&#39;Y-axis&#39;)<\/p>\n<p>plt.title(&#39;Vector Field using quiver&#39;)<\/p>\n<p>plt.grid()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u65cb\u8f6c\u7684\u5411\u91cf\u573a\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5411\u91cf\u7684\u65b9\u5411\u662f\u5782\u76f4\u4e8e\u5b83\u7684\u5750\u6807\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528NumPy\u8fdb\u884c\u5411\u91cf\u8ba1\u7b97<\/p>\n<\/p>\n<p><p>\u5728\u7ed8\u5236\u5411\u91cf\u65f6\uff0c\u901a\u5e38\u9700\u8981\u8fdb\u884c\u4e00\u4e9b\u5411\u91cf\u8ba1\u7b97\u3002NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8fdb\u884c\u8fd9\u4e9b\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5411\u91cf\u7684\u57fa\u672c\u8fd0\u7b97<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528NumPy\u8fdb\u884c\u57fa\u672c\u7684\u5411\u91cf\u8fd0\u7b97\uff0c\u4f8b\u5982\u52a0\u6cd5\u3001\u51cf\u6cd5\u3001\u70b9\u79ef\u548c\u53c9\u79ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u4e24\u4e2a\u5411\u91cf<\/strong><\/h2>\n<p>vector1 = np.array([1, 2])<\/p>\n<p>vector2 = np.array([3, 4])<\/p>\n<h2><strong>\u5411\u91cf\u52a0\u6cd5<\/strong><\/h2>\n<p>sum_vector = vector1 + vector2<\/p>\n<h2><strong>\u5411\u91cf\u51cf\u6cd5<\/strong><\/h2>\n<p>diff_vector = vector1 - vector2<\/p>\n<h2><strong>\u5411\u91cf\u70b9\u79ef<\/strong><\/h2>\n<p>dot_product = np.dot(vector1, vector2)<\/p>\n<h2><strong>\u5411\u91cf\u53c9\u79ef\uff08\u9002\u7528\u4e8e\u4e09\u7ef4\u5411\u91cf\uff09<\/strong><\/h2>\n<p>vector1_3d = np.array([1, 2, 0])<\/p>\n<p>vector2_3d = np.array([3, 4, 0])<\/p>\n<p>cross_product = np.cross(vector1_3d, vector2_3d)<\/p>\n<p>print(&quot;Sum of vectors:&quot;, sum_vector)<\/p>\n<p>print(&quot;Difference of vectors:&quot;, diff_vector)<\/p>\n<p>print(&quot;Dot product of vectors:&quot;, dot_product)<\/p>\n<p>print(&quot;Cross product of vectors:&quot;, cross_product)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5411\u91cf\u7684\u5f52\u4e00\u5316<\/strong><\/li>\n<\/ol>\n<p><p>\u5f52\u4e00\u5316\u662f\u5c06\u5411\u91cf\u7684\u957f\u5ea6\u7f29\u653e\u4e3a1\uff0c\u4f46\u4fdd\u7559\u5176\u65b9\u5411\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def normalize(vector):<\/p>\n<p>    norm = np.linalg.norm(vector)<\/p>\n<p>    if norm == 0:<\/p>\n<p>        return vector<\/p>\n<p>    return vector \/ norm<\/p>\n<p>vector = np.array([3, 4])<\/p>\n<p>normalized_vector = normalize(vector)<\/p>\n<p>print(&quot;Normalized vector:&quot;, normalized_vector)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u57283D\u7a7a\u95f4\u7ed8\u5236\u5411\u91cf<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4e8c\u7ef4\u7a7a\u95f4\uff0cMatplotlib\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5728\u4e09\u7ef4\u7a7a\u95f4\u4e2d\u7ed8\u5236\u5411\u91cf\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u57283D\u7a7a\u95f4\u4e2d\u7ed8\u5236\u5355\u4e2a\u5411\u91cf<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>from mpl_toolkits.mplot3d import Axes3D<\/p>\n<p>fig = plt.figure()<\/p>\n<p>ax = fig.add_subplot(111, projection=&#39;3d&#39;)<\/p>\n<h2><strong>\u5b9a\u4e49\u8d77\u59cb\u70b9\u548c\u65b9\u5411<\/strong><\/h2>\n<p>ax.quiver(0, 0, 0, 1, 1, 1, length=1)<\/p>\n<p>ax.set_xlim([0, 2])<\/p>\n<p>ax.set_ylim([0, 2])<\/p>\n<p>ax.set_zlim([0, 2])<\/p>\n<p>ax.set_xlabel(&#39;X-axis&#39;)<\/p>\n<p>ax.set_ylabel(&#39;Y-axis&#39;)<\/p>\n<p>ax.set_zlabel(&#39;Z-axis&#39;)<\/p>\n<p>ax.set_title(&#39;3D Vector using quiver&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u57283D\u7a7a\u95f4\u4e2d\u7ed8\u5236\u591a\u4e2a\u5411\u91cf<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>from mpl_toolkits.mplot3d import Axes3D<\/p>\n<p>import numpy as np<\/p>\n<p>fig = plt.figure()<\/p>\n<p>ax = fig.add_subplot(111, projection=&#39;3d&#39;)<\/p>\n<h2><strong>\u5b9a\u4e49\u591a\u4e2a\u5411\u91cf\u7684\u8d77\u59cb\u70b9\u548c\u65b9\u5411<\/strong><\/h2>\n<p>X, Y, Z = np.array([0, 0, 0]), np.array([0, 0, 0]), np.array([0, 0, 0])<\/p>\n<p>U, V, W = np.array([1, 1, 0]), np.array([1, 0, 1]), np.array([0, 1, 1])<\/p>\n<p>ax.quiver(X, Y, Z, U, V, W, length=1)<\/p>\n<p>ax.set_xlim([0, 2])<\/p>\n<p>ax.set_ylim([0, 2])<\/p>\n<p>ax.set_zlim([0, 2])<\/p>\n<p>ax.set_xlabel(&#39;X-axis&#39;)<\/p>\n<p>ax.set_ylabel(&#39;Y-axis&#39;)<\/p>\n<p>ax.set_zlabel(&#39;Z-axis&#39;)<\/p>\n<p>ax.set_title(&#39;Multiple 3D Vectors using quiver&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u5e94\u7528\u4e0e\u5b9e\u8df5<\/p>\n<\/p>\n<ol>\n<li><strong>\u7269\u7406\u4e2d\u7684\u5411\u91cf<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u7269\u7406\u5b66\u4e2d\uff0c\u5411\u91cf\u7528\u4e8e\u8868\u793a\u8bb8\u591a\u7269\u7406\u91cf\uff0c\u5982\u901f\u5ea6\u3001\u529b\u548c\u52a0\u901f\u5ea6\u3002\u4f7f\u7528Matplotlib\uff0c\u53ef\u4ee5\u5728\u56fe\u5f62\u5316\u754c\u9762\u4e2d\u76f4\u89c2\u5730\u8868\u793a\u8fd9\u4e9b\u7269\u7406\u91cf\uff0c\u4ece\u800c\u5e2e\u52a9\u7406\u89e3\u548c\u5206\u6790\u7269\u7406\u73b0\u8c61\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6570\u636e\u79d1\u5b66\u4e2d\u7684\u5411\u91cf<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u6570\u636e\u79d1\u5b66\u4e2d\uff0c\u5411\u91cf\u7528\u4e8e\u8868\u793a\u6570\u636e\u70b9\u3001\u7279\u5f81\u548c\u68af\u5ea6\u3002\u5728<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\uff0c\u5411\u91cf\u7684\u53ef\u89c6\u5316\u53ef\u4ee5\u5e2e\u52a9\u7406\u89e3\u6a21\u578b\u7684\u884c\u4e3a\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u6e38\u620f\u5f00\u53d1\u4e2d\u7684\u5411\u91cf<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u6e38\u620f\u5f00\u53d1\u4e2d\uff0c\u5411\u91cf\u7528\u4e8e\u8868\u793a\u7269\u4f53\u7684\u4f4d\u7f6e\u3001\u901f\u5ea6\u548c\u52a0\u901f\u5ea6\u3002\u901a\u8fc7\u53ef\u89c6\u5316\u8fd9\u4e9b\u5411\u91cf\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u8bbe\u8ba1\u548c\u8c03\u8bd5\u6e38\u620f\u4e2d\u7684\u7269\u7406\u7cfb\u7edf\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff0cPython\u4e2d\u7684Matplotlib\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u7ed8\u5236\u5411\u91cf\uff0c\u5305\u62ec<code>quiver<\/code>\u3001<code>annotate<\/code>\u548c<code>arrow<\/code>\u51fd\u6570\u3002\u901a\u8fc7\u8fd9\u4e9b\u5de5\u5177\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u4e8c\u7ef4\u548c\u4e09\u7ef4\u7a7a\u95f4\u4e2d\u7ed8\u5236\u548c\u5206\u6790\u5411\u91cf\u3002\u540c\u65f6\uff0c\u7ed3\u5408NumPy\u8fdb\u884c\u5411\u91cf\u8ba1\u7b97\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u5e94\u7528\u3002\u5728\u7269\u7406\u5b66\u3001\u6570\u636e\u79d1\u5b66\u548c\u6e38\u620f\u5f00\u53d1\u7b49\u9886\u57df\uff0c\u5411\u91cf\u7684\u53ef\u89c6\u5316\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u4e8c\u7ef4\u5411\u91cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u8f7b\u677e\u7ed8\u5236\u4e8c\u7ef4\u5411\u91cf\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86Matplotlib\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7<code>pip install matplotlib<\/code>\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\u3002\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528<code>quiver<\/code>\u51fd\u6570\u6765\u7ed8\u5236\u5411\u91cf\uff0c\u4f20\u5165\u8d77\u70b9\u5750\u6807\u548c\u5411\u91cf\u7684\u5206\u91cf\u5373\u53ef\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n\n# \u8d77\u70b9\u5750\u6807\norigin = [0, 0]\n# \u5411\u91cf\u5206\u91cf\nvector = [2, 3]\n\nplt.quiver(*origin, *vector, angles=&#39;xy&#39;, scale_units=&#39;xy&#39;, scale=1)\nplt.xlim(-1, 5)\nplt.ylim(-1, 5)\nplt.grid()\nplt.show()\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u5728\u5750\u6807\u8f74\u4e0a\u7ed8\u5236\u4ece(0, 0)\u51fa\u53d1\uff0c\u7ec8\u70b9\u4e3a(2, 3)\u7684\u5411\u91cf\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u7528\u6765\u7ed8\u5236\u5411\u91cf\uff1f<\/strong><br \/>\u9664\u4e86Matplotlib\uff0cPython\u4e2d\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u5e93\u53ef\u4ee5\u7528\u6765\u7ed8\u5236\u5411\u91cf\u3002\u4f8b\u5982\uff0cNumPy\u4e0eMatplotlib\u7ed3\u5408\u4f7f\u7528\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u6570\u5b66\u8fd0\u7b97\u548c\u7ed8\u56fe\u529f\u80fd\u3002\u4f7f\u7528SymPy\u53ef\u4ee5\u7ed8\u5236\u7b26\u53f7\u5411\u91cf\u56fe\uff0c\u800cPygame\u5219\u9002\u5408\u4e8e\u52a8\u6001\u7ed8\u5236\u548c\u6e38\u620f\u5f00\u53d1\u4e2d\u7684\u5411\u91cf\u8868\u793a\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u53d6\u51b3\u4e8e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u548c\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u4e09\u7ef4\u7a7a\u95f4\u4e2d\u7ed8\u5236\u5411\u91cf\uff1f<\/strong><br \/>\u5728\u4e09\u7ef4\u7a7a\u95f4\u4e2d\u7ed8\u5236\u5411\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u4e2d\u7684<code>Axes3D<\/code>\u6a21\u5757\u3002\u9996\u5148\uff0c\u9700\u8981\u5bfc\u5165\u76f8\u5173\u6a21\u5757\u5e76\u521b\u5efa\u4e09\u7ef4\u5750\u6807\u7cfb\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">from mpl_toolkits.mplot3d import Axes3D\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nfig = plt.figure()\nax = fig.add_subplot(111, projection=&#39;3d&#39;)\n\n# \u8d77\u70b9\u5750\u6807\norigin = np.array([0, 0, 0])\n# \u5411\u91cf\u5206\u91cf\nvector = np.array([1, 2, 3])\n\nax.quiver(*origin, *vector, color=&#39;r&#39;)\nax.set_xlim([0, 5])\nax.set_ylim([0, 5])\nax.set_zlim([0, 5])\nplt.show()\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5728\u4e09\u7ef4\u7a7a\u95f4\u4e2d\u7ed8\u5236\u4e86\u4e00\u4e2a\u4ece\u539f\u70b9\u51fa\u53d1\u7684\u5411\u91cf\uff0c\u7ec8\u70b9\u4e3a(1, 2, 3)\u3002\u4f7f\u7528\u4e09\u7ef4\u7ed8\u56fe\u53ef\u4ee5\u66f4\u597d\u5730\u53ef\u89c6\u5316\u5411\u91cf\u4e4b\u95f4\u7684\u5173\u7cfb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u753b\u5411\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u4e2d\u7684quiver\u51fd\u6570\u3001annotate\u51fd\u6570\u3001arrow [&hellip;]","protected":false},"author":3,"featured_media":927624,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/927614"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=927614"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/927614\/revisions"}],"predecessor-version":[{"id":927625,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/927614\/revisions\/927625"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/927624"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=927614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=927614"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=927614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}