如何绘制打印数据

问题描述 投票:0回答:1

我正在做作业,为我的Python课程生成一个曲线图,由于这是我第一次学习Python,所以我很难编写一个简单的代码。我使用 Jupyter Handbook 进行学习。下面是我被卡住的代码。

import numpy as np
import matplotlib.pyplot as plt

from scipy.optimize import fsolve

%matplotlib inline

#THE INFORMATION THE PROFESSOR GAVE ME
F=150 
M=70 
Co=350 
Ce=0 
Mabs=0 #not quite necessary information for the code
init_guess= Co/10
qm=246 
KL=0.0026 
q_prior = 0 
qe = 0 
Ce_output = [0.000001] #not quite necessary information for the code
time_output = [0.000001] #not quite necessary information for the code
qe_output = [0.000001] #not quite necessary information for the code
max_time = 24*60 #not quite necessary information for the code

func = lambda Ce : (Co-Ce)*F/(1000*M) + q_prior - qm*KL*Ce/(1+KL*Ce)

Ce_solution = fsolve(func, init_guess)
qe = qm*KL*Ce_solution/(1+KL*Ce_solution)

#THE WAY I TRY TO MAKE A PLOT
count = 1
while (count < 21):
    t = 60*70/150*count #so it's actually 28*count
    print('Ce',Ce_solution,'qe',qe,'t',t)
    plt.plot(t, Ce)
    count = count + 1
    qm = qe
    Ce_solution = fsolve(func, init_guess)
    qe = qm*KL*Ce_solution/(1+KL*Ce_solution)

plt.xlim([0,560])
plt.ylim([0,350])
plt.show()

使用此代码,我可以打印 20 个(Ce、ge、t)数据和一张空白图表。我想要的是20个数据和根据20个数据的Ce-t曲线图(t在x轴上;Ce在y轴上)。我想我应该在 while 循环内重新定位

plt.plot(t, Ce)
,但我无法猜测如何做。你能教我我缺少什么吗?谢谢。

python numpy matplotlib
1个回答
0
投票

当您在 while 循环中使用绘图函数时,您最终会绘制出许多不必要的绘图。您需要做的是将时间变量 t 和 Ce_solution 存储在列表(或等效项)中,然后在 while 循环后进行绘图。你可以做这样的事情:

count = 1
times = []
list_of_ce = []
while (count < 21):
    t = 60*70/150*count #so it's actually 28*count
    print('Ce',Ce_solution,'qe',qe,'t',t)
    count = count + 1
    qm = qe
    Ce_solution = fsolve(func, init_guess)
    qe = qm*KL*Ce_solution/(1+KL*Ce_solution)

    times.append(t)
    list_of_ce.append(Ce_solution)

plt.plot(times, list_of_ce, 'k.-')

plt.xlim([0,560])
plt.ylim([0,350])
plt.show('')

代码的作用是在 while 循环运行时将变量保存在列表中,然后在 while 循环完成后进行绘图。

© www.soinside.com 2019 - 2024. All rights reserved.