简体   繁体   English

使用matplotlib在python中绘制轨迹

[英]Plotting trajectories in python using matplotlib

I'm having some trouble using matplotlib to plot the path of something. 使用matplotlib绘制某些内容的路径时遇到麻烦。 Here's a basic version of the type of thing I'm doing. 这是我正在做的事情的基本版本。

Essentially, I'm seeing if the value breaks a certain threshold (6 in this case) at any point during the path and then doing something with it later on. 从本质上讲,我正在查看该值是否在路径中的任何时候都超出某个阈值(在这种情况下为6),然后稍后对其进行处理。

Now, I have 3 lists set-up. 现在,我有3个列表设置。 The end_vector will be based on the other two lists. end_vector将基于其他两个列表。 If the value breaks past 2 any time during a single simulation, I will add the last position of the object to my end_vector 如果一次模拟中的任何时间该值超过2,我都会将该对象的最后一个位置添加到end_vector

trajectories_vect is something I want to keep track of my trajectories for all 5 simulations, by keeping a list of lists. trajectories_vect是我想通过保留列表列表来跟踪所有5个仿真的轨迹的方法。 I'll clarify this below. 我将在下面对此进行澄清。 And, timestep_vect stores the path for a single simulation. 并且, timestep_vect存储单个仿真的路径。

from random import gauss
from matplotlib import pyplot as plt
import numpy as np

starting_val = 5
T = 1                   #1 year
delta_t = .1            #time-step
N = int(T/delta_t)      #how many points on the path looked at
trials = 5              #number of simulations

#main iterative loop
end_vect = []
trajectories_vect = []
for k in xrange(trials):
    s_j = starting_val
    timestep_vect = []
    for j in xrange(N-1):
        xi = gauss(0,1.0)
        s_j *= xi
        timestep_vect.append(s_j)
    trajectories_vect.append(timestep_vect)
    if max(timestep_vect) > 5:
        end_vect.append(timestep_vect[-1])
    else:
        end_vect.append(0)

Okay, at this part if I print my trajectories, I get something like this (I only posted two simulations, instead of the full 5): 好的,在这一部分中,如果我打印轨迹,我将得到如下信息(我只发布了两个仿真,而不是完整的5个仿真):

[[ -3.61689976e+00   2.85839230e+00  -1.59673115e+00   6.22743522e-01
1.95127718e-02  -1.72827152e-02   1.79295788e-02   4.26807446e-02
-4.06175288e-02]  [  4.29119818e-01   4.50321728e-01  -7.62901016e-01
-8.31124346e-02 -6.40330554e-03   1.28172906e-02  -1.91664737e-02
-8.29173982e-03 4.03917926e-03]]

This is good and what I want to happen. 这很好,我想发生什么。

Now, my problem is that I don't know how to plot my path (y-axis) against my time (x-axis) properly. 现在,我的问题是我不知道如何正确地相对于时间(x轴)绘制路径(y轴)。

First, I want to put my data into numpy arrays because I'll need to use them later on to compute some statistics and other things which from experience numpy makes very easy. 首先,我想将数据放入numpy数组中,因为稍后需要使用它们来计算一些统计数据和其他一些东西,而从numpy的经验来看,这些数据非常简单。

 #creating numpy arrays from list
 #might need to use this with matplotlib somehow
 np_trajectories = np.array(trajectories_vect)
 time_array = np.arange(1,10)

Here's the crux of the issue though. 这是问题的症结所在。 When i'm putting my trajectories (y-axis) into matplotlib, it's not treating each "list" (row in numpy) as one path. 当我将轨迹(y轴)放入matplotlib时,它不会将每个“列表”(以numpy表示的行)都视为一条路径。 Instead of getting 5 paths for 5 simulations, I am getting 9 paths for 5 simulations. 我没有获得5条仿真的5条路径,而是获得了5条仿真的9条路径。 I believe I am inputing stuff wrong hence it is using the 9 time intervals in the wrong way. 我相信我输入的内容有误,因此它以错误的方式使用了9个时间间隔。

 #matplotlib stuff
 plt.plot(np_trajectories)
 plt.xlabel('timestep')
 plt.ylabel('trajectories')
 plt.show()

Here's the image produced: 这是产生的图像:

在此处输入图片说明

Obviously, this is wrong for the aforementioned reason. 显然,由于上述原因,这是错误的。 Instead, I want to have 5 paths based on the 5 lists (rows) in my trajectories. 相反,我希望基于轨迹中的5个列表(行)具有5条路径。 I seem to understand what the problem is but don't know how to go about fixing it. 我似乎了解问题所在,但不知道如何解决。

Thanks in advance for the help. 先谢谢您的帮助。

When you call np_trajectories = np.array(trajectories_vect) , your list of trajectories is transformed into a 2d numpy array. 当您调用np_trajectories = np.array(trajectories_vect) ,您的轨迹列表将转换为2d numpy数组。 The information about its dimensions is stored in np_trajectories.shape , and, in your case, is (5, 9) . 有关其尺寸的信息存储在np_trajectories.shape ,在您的情况下为(5, 9) np_trajectories.shape (5, 9) Therefore, when you pass np_trajectories to plt.plot() , the plotting library assumes that the y-values are stored in the first dimension, while the second dimension describes individual lines to plot. 因此,当您将np_trajectories传递到plt.plot() ,绘图库假定y值存储在第一个维度中,而第二个维度则描述了要绘制的各个线条。

In your case, all you need to do is to transpose your np_trajectories array. 就您而言,您所需要做的就是转置np_trajectories数组。 In numpy , it is as simple as numpy ,它很简单

plt.plot(np_trajectories.T)
plt.xlabel('timestep')
plt.ylabel('trajectories')
plt.show()

If you want to plot the x-axis as time, instead of steps of one, you have to define your time progression as a list or an array. 如果要将x轴绘制为时间,而不是1的步长,则必须将时间进度定义为列表或数组。 In numpy , you can do something like numpy ,您可以执行以下操作

times = np.linspace(0, T, N-1)
plt.plot(times, np_trajectories.T)
plt.xlabel('timestep')
plt.ylabel('trajectories')
plt.show()

which produces the following figure: 产生下图: 时间步

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM