简体   繁体   中英

decrementing in matplotlib.pyplot and numpy

 import matplotlib.pyplot as plt import numpy as np import os import sys import time MissionName = "Mars" savename = "Mission" start_time = time.time() t = np.arange(0.0, 200.0, 10) M0 = 2970000 mps = 12857.1429 mT = (mps * t) m = (M0 - mT) Fstuw = 35100000 a = Fstuw / m for time in t: if time >= 50: vE = 0 for time in t: if time < 50: vE = 2580 h1 = (vE * M0/mps) h2 = (1-(m / M0)) h3 = (np.log(M0 / m) / np.log(2.718281828)) + 1 h = h1 * h2 * h3 v = vE * (np.log(M0 / m) / np.log(2.718281828)) plt.plot(t,v) plt.xlabel('time (s)') plt.ylabel('Velocity (m/s)') plt.title('Model raketmissie ' + str(MissionName)) plt.grid(True) plt.savefig(savename + ".png") plt.show() 

Okay so the problem i have is that it does not change vE to 0 when the time is bigger or equal to 50, the result i get is this:

在此处输入图片说明

Your problem is in this part of the code:

for time in t:
    if time >= 50:
        vE = 0
for time in t:
    if time < 50:
        vE = 2580

Right now your vE is just a value, not a list or some other collection. You iterate twice. The first time you set vE=0 , the second time you set it to vE=2580 , overwriting the zero, you set before.

If you want a value for each timepoint, you could do something like:

vE=[0.0]*len(t)
for i, time in enumerate(t):
    if time < 50:
        v[i] = 2580.0

So you initialize a list of the same length as t with only zeros and change the value to 2580 for each element corresponding to a time <50.

An even better way, as suggested by Mad Physicist is to use a numpy array:

t = np.arange(0.0, 200.0, 10)
vE = np.zeros_like(t);
vE[t < 50] = 2580

or in one line

vE = np.where(t<50, 2580, 0)

So you don't have to use a loop to populate the list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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