'Matplotlib quiver: axis lim, legend and color
I'm trying to represent a reference system (triad) or simply three unitary vectors in a matplotib-generated figure in Python. I'm using the following code based on quiver:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([0,0,0]).astype(float)
y = np.array([0,0,0]).astype(float)
z = np.array([0,0,0]).astype(float)
u = np.array([1,0,0]).astype(float)
v = np.array([0,1,0]).astype(float)
w = np.array([0,0,1]).astype(float)
ax = plt.figure().add_subplot(projection='3d')
ax.quiver(x,y,z,u,v,w)
plt.show()
I would like to set axis limits to (-1,+1) (I will plot only unitary vectors), a legend and represent each axis/vector with a different color. Any suggestion?
Solution 1:[1]
Thank you a lot. This was for sure a quite good starting point. Now, I would like to add a second reference frame into the same figure. I'm using the following code. But unfortunately, the command to plot the second reference frame with dashed/dotted arrows doesn't works. On the contrary, the legend recognizes the command and represent the arrays correctly. Why?
import numpy as np
import matplotlib.pyplot as plt
ax = plt.figure(1).add_subplot(projection='3d')
############## FIRST REFERENCE FRAME ###############
u = ax.quiver(0, 0, 0, 1, 0, 0, color="r")
v = ax.quiver(0, 0, 0, 0, 1, 0, color="g")
w = ax.quiver(0, 0, 0, 0, 0, 1, color="b")
# set empty line plots with colors associate to the
# quivers. Doing so we can show a legend.
ax.plot([], [], [], color="r", label="X")
ax.plot([], [], [], color="g", label="Y")
ax.plot([], [], [], color="b", label="Z")
############## FIRST REFERENCE FRAME ###############
############## SECOND REFERENCE FRAME ###############
u1 = ax.quiver(0, 0, 0, 0, 0, -1, color="r")
v1 = ax.quiver(0, 0, 0, -1, 0, 0, color="g")
w1 = ax.quiver(0, 0, 0, 0, -1, 0, color="b")
# set empty line plots with colors associate to the
# quivers. Doing so we can show a legend.
ax.plot([], [], [], 'r--', label="X1")
ax.plot([], [], [], 'g--', label="Y1")
ax.plot([], [], [], 'b--', label="Z1")
############## SECOND REFERENCE FRAME ###############
ax.set_xlim(-1, 1); ax.set_ylim(-1, 1); ax.set_zlim(-1, 1)
ax.set_xlabel("X_AXIS"); ax.set_ylabel("Y_AXIS"); ax.set_zlabel("Z_AXIS")
ax.legend(); plt.show()
Solution 2:[2]
This might be a good starting point:
import numpy as np
import matplotlib.pyplot as plt
ax = plt.figure().add_subplot(projection='3d')
u = ax.quiver(0, 0, 0, 1, 0, 0, color="r")
v = ax.quiver(0, 0, 0, 0, 1, 0, color="g")
w = ax.quiver(0, 0, 0, 0, 0, 1, color="b")
# set empty line plots with colors associate to the
# quivers. Doing so we can show a legend.
ax.plot([], [], [], color="r", label="u")
ax.plot([], [], [], color="g", label="v")
ax.plot([], [], [], color="b", label="w")
ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
ax.set_zlim(-1, 1)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
ax.legend()
plt.show()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | eljamba |
Solution 2 | Davide_sd |