Quantcast
Viewing all articles
Browse latest Browse all 14069

Vector Coordinates and linear combination as an arrow will not display on the same plot

I am attempting to graph the coordinates of two vectors on a plot (see Part 1 of code snippet) and the linear combination of the two vectors (see Part 2 of code snippet1) on the same plot. However, in Snippet 1, only the arrow is displaying, though I am not sure why.

In Snippet 2, I was able to get both to print, by reversing the order, but the arrow does not reach the end coordinate w = [2, 1], as expected. Can anyone advise how I can fix the arrow so it lands on the coordinate (2, 1), (see Plot Image below)?

Snippet 1: Only Arrow displays:

import matplotlib.pyplot as pltimport numpy as np# Part 1: Plot pointsx = [-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 Vectorv = 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-axisplt.xlim(-3, 3)# Set major ticks for x-axismajor_xticks = np.arange(-3, 4)ax.set_xticks(major_xticks)# Sets limit for plot for y-axisplt.ylim(-2, 2)# Set major ticks for y-axismajor_yticks = np.arange(-2, 3)ax.set_yticks(major_yticks)# Default Gridplt.grid(visible=True, which='major')plt.show()

Code Snippet 2: Both coordinates and arrow display, however, the head of the arrow does not reach the end coordinate w= [2, 1], as expected:

import matplotlib.pyplot as pltimport numpy as np# Part 2: Plot Vectorv = 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-axisplt.xlim(-3, 3)# Set major ticks for x-axismajor_xticks = np.arange(-3, 4)ax.set_xticks(major_xticks)# Sets limit for plot for y-axisplt.ylim(-2, 2)# Set major ticks for y-axismajor_yticks = np.arange(-2, 3)ax.set_yticks(major_yticks)# Part 1: Plot pointsx = [-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 Gridplt.grid(visible=True, which='major')plt.show()

Plot Image:Image may be NSFW.
Clik here to view.
enter image description here


Viewing all articles
Browse latest Browse all 14069

Trending Articles