Plotting a 2d trajectory with Matplotlib

In summary, the conversation discusses the process of plotting a 2d trajectory of a center of mass using an array. The code provided produces a plot, but it does not accurately show the rotation of the trajectory. Suggestions for improvement include setting the x and y axes to the same scale and generating test data to check the accuracy of the plot.
  • #1
Avatrin
245
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
  • #2
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 Avatrin

1. How do I plot a 2d trajectory with Matplotlib?

To plot a 2d trajectory with Matplotlib, you will first need to import the matplotlib library and create a figure and axes object. Then, use the plot() method to plot the x and y coordinates of your trajectory. Finally, use the show() method to display the plot.

2. Can I customize the appearance of my trajectory plot?

Yes, you can customize the appearance of your trajectory plot by adding various parameters to the plot() method, such as colors, line styles, and markers. You can also add labels and titles to your plot using the xlabel(), ylabel(), and title() methods.

3. How can I add multiple trajectories to the same plot?

To add multiple trajectories to the same plot, you can use the plot() method multiple times, each time with different sets of x and y coordinates. You can also use the legend() method to add a legend to your plot, which will label each trajectory.

4. Is it possible to save my trajectory plot as an image file?

Yes, you can save your trajectory plot as an image file by using the savefig() method. This method allows you to specify the file format and location where you want to save your plot.

5. Can I plot a 3d trajectory with Matplotlib?

Yes, you can plot a 3d trajectory with Matplotlib by importing the mpl_toolkits.mplot3d library and creating a 3d axes object. Then, use the plot() method with x, y, and z coordinates to plot your trajectory in 3d space.

Similar threads

  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
1
Views
589
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
902
  • Programming and Computer Science
Replies
21
Views
4K
Back
Top