Plotting a 2d trajectory with Matplotlib

Click For Summary
SUMMARY

The discussion focuses on plotting a 2D trajectory using Matplotlib, specifically addressing issues with visualizing a rotating path represented by an array of coordinates. The user initially attempted to plot the trajectory using the code snippet involving lists for x and y coordinates but encountered problems with the expected rotation appearance. A solution was provided, suggesting the use of plt.gca().set_aspect("equal") to ensure equal scaling on both axes, along with generating test data to verify the plot's accuracy.

PREREQUISITES
  • Understanding of 2D plotting with Matplotlib
  • Familiarity with Python programming
  • Basic knowledge of coordinate systems
  • Experience with generating and manipulating arrays
NEXT STEPS
  • Learn how to use plt.gca().set_aspect("equal") for accurate aspect ratios in Matplotlib
  • Explore generating test data for plotting, such as using trigonometric functions
  • Investigate the use of PDEs in trajectory generation
  • Study advanced Matplotlib features for customizing plots
USEFUL FOR

Data scientists, engineers, and anyone involved in visualizing mathematical trajectories or simulations using Python and Matplotlib.

Avatrin
Messages
242
Reaction score
6
Hi

I have a 2d trajectory of a center of mass which I have saved in an array such that r=[[x(t_1),y(t_1)],[x(t_2),y(t_2)],...,[x(t_n),y(t_n)]] where x is x coordinate of the position and y is the y-coordinate of the position.

I have tried this:

xx = [t[0] for t in r]
xy = [t[1] for t in r]
plt.plot(xx,xy)
plt.show()

It produced a plot, but I cannot make it look right. It's supposed to be rotating, but no matter how I change the initial conditions (it's a PDE), the resulting plot doesn't look like a rotation.

How should I go about to plotting this trajectory?
 
Technology news on Phys.org
That appears to be correct code to plot your x value horizontally and your y value vertically, although you may want to set the x and y axes to the same scale:
Python:
plt.plot(xx,xy)
plt.gca().set_aspect("equal")
plt.show()

You can always check this kind of thing by generating some test data and checking that the plot looks like you expect. For example the following should generate most of a circle.
Python:
r=[[math.cos(0.1*p),math.sin(0.1*p)] for p in range(60)]

So presumably either the path doesn't look like you believe it should, or you've made an error generating it.
 
Last edited:
  • Like
Likes   Reactions: Avatrin

Similar threads

  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
10K
  • · Replies 21 ·
Replies
21
Views
6K
Replies
4
Views
5K