【问题标题】:how to plott several curves after while loop in function如何在函数中的while循环后绘制多条曲线
【发布时间】:2021-01-15 22:21:58
【问题描述】:
def a(t, A, B, C, At, Bt):
  while:
    calculations
  return t, A, B, C, At, Bt

打印(def(t, A, B, C, At, Bt))

我返回了几个 numpy.arrays。并想以表格形式绘制它们

B, = plt.plot(t, B)
C, = plt.plot(t, C) 
plt.legend(handles=[ B, C, A],
           labels=[ 'B', 'C', 'A'])

【问题讨论】:

  • 您的具体问题是什么?你有一些玩具数据来说明你的问题吗?

标签: python-3.x numpy matplotlib


【解决方案1】:

使用matplotlib的面向对象接口:

from matplotlib import pyplot
t, A, B, C, At, Bt = a(t, A, B, C, At, Bt)

fig, ax = pyplot.subplots()
for array, label in zip([A, B, C], ['A', 'B', 'C']):
    ax.plot(t, array, label=label)

ax.legend()

【讨论】:

  • 所以我需要将我的功能写到一个类中?因为 A,B,C,t,tend,ka,kb,At = a(A,B,C,t,tend,ka,kb,At) 给我的值不足以解包
  • @pythonabsir 在您的问题中,如所写,函数 a 返回 6 个值。在我的回答中,我从函数 a 中解压缩了 6 个值。您可以根据需要返回尽可能多的值(或在其他地方定义它们)
  • 你!我调用的值与返回的值不同。谢谢!
猜你喜欢
  • 2020-08-16
  • 2019-09-16
  • 1970-01-01
  • 2019-08-31
  • 1970-01-01
  • 2022-10-15
  • 2015-09-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多