矢量坐标和线性组合作为箭头不会显示在同一绘图上

问题描述 投票:0回答:1

我正在尝试在绘图上绘制两个向量的坐标(请参阅代码片段的第 1 部分),并在同一绘图上绘制两个向量的线性组合(请参阅代码片段 1 的第 2 部分)。然而,在片段 1 中,仅显示箭头,但我不确定为什么。

在代码片段 2 中,我可以通过颠倒顺序来打印两者,但箭头没有按预期到达结束坐标 w = [2, 1]。谁能告诉我如何修复箭头,使其落在坐标 (2, 1) 上(参见下面的绘图)?

片段 1:仅显示箭头:

import matplotlib.pyplot as plt
import numpy as np

# Part 1: Plot points
x = [-2, 2, 2]
y = [-1, -1, 1]

plt.plot(x, y, color='r', linestyle='dashed', linewidth=2, marker='o', markerfacecolor='blue', markeredgecolor='blue')
plt.xlim(-3, 3)
plt.ylim(-2, 2)
y_range = np.arange(-2, 3)
x_range = np.arange(-3, 4)
plt.yticks(y_range)
plt.xticks(x_range)

# Part 2: Plot Vector
v = np.array([-2, -1])
w = np.array([2, 1])

# Creates axes of plot referenced 'ax'
ax = plt.axes()

# Plots red dot at origin (0,0)
ax.plot(*v, 'or')

ax.arrow(*v, *w, color='b', linewidth=2.5, head_width=0.30, head_length=0.35)

# Sets limit for plot for x-axis
plt.xlim(-3, 3)

# Set major ticks for x-axis
major_xticks = np.arange(-3, 4)
ax.set_xticks(major_xticks)

# Sets limit for plot for y-axis
plt.ylim(-2, 2)

# Set major ticks for y-axis
major_yticks = np.arange(-2, 3)
ax.set_yticks(major_yticks)

# Default Grid
plt.grid(visible=True, which='major')

plt.show()

代码片段2:坐标和箭头都显示,但是箭头头没有到达结束坐标w= [2, 1],正如预期的那样:

import matplotlib.pyplot as plt
import numpy as np

# Part 2: Plot Vector
v = np.array([-2, -1])
w = np.array([2, 1])

# Creates axes of plot referenced 'ax'
ax = plt.axes()

# Plots red dot at origin (0,0)
ax.plot(*v, 'or')

ax.arrow(*v, *w, color='b', linewidth=2.5, head_width=0.30, head_length=0.35)

# Sets limit for plot for x-axis
plt.xlim(-3, 3)

# Set major ticks for x-axis
major_xticks = np.arange(-3, 4)
ax.set_xticks(major_xticks)

# Sets limit for plot for y-axis
plt.ylim(-2, 2)

# Set major ticks for y-axis
major_yticks = np.arange(-2, 3)
ax.set_yticks(major_yticks)

# Part 1: Plot points
x = [-2, 2, 2]
y = [-1, -1, 1]

plt.plot(x, y, color='r', linestyle='dashed', linewidth=2, marker='o', markerfacecolor='blue', markeredgecolor='blue')
plt.xlim(-3, 3)
plt.ylim(-2, 2)
y_range = np.arange(-2, 3)
x_range = np.arange(-3, 4)
plt.yticks(y_range)
plt.xticks(x_range)



# Default Grid
plt.grid(visible=True, which='major')

plt.show()

绘图图像:

python numpy matplotlib linear-algebra
1个回答
0
投票

查看文档后,我能够使用以下代码进行修复。感谢@Ruthc 指出了这一点。

 ax.arrow(*v, 4, 2, color='b', linewidth=2.5, head_width=0.30, head_length=0.35)
© www.soinside.com 2019 - 2024. All rights reserved.