You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何绘制返回三个数组的函数图表?如何单独获取返回值

问题解答

1. 能否单独获取函数返回的times、theta、displacement?

完全可以,Python支持直接解包函数的多返回值,也能通过索引单独提取:

  • 解包所有值(最清晰,避免重复执行函数):
    times, theta, displacement = tacoma(dt=1, cromer=True)
    
  • 单独获取某一个值(不推荐多次调用,会重复执行函数逻辑):
    times = tacoma()[0]       # 获取times
    theta = tacoma()[1]       # 获取theta
    displacement = tacoma()[2]# 获取displacement
    

2. 绘制两张图表的最佳实现方式

用matplotlib库是最常用的方案,推荐把两张图放在同一画布的两个子图中,方便对比数据;也可以生成两个独立窗口,按需选择。

方案一:同一画布的两个子图(推荐)

import matplotlib.pyplot as plt

# 先一次性获取所有数据
times, theta, displacement = tacoma(dt=1, cromer=True)

# 创建画布与两个垂直排列的子图
fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, figsize=(8, 10), sharex=True)

# 绘制theta随时间变化的曲线
ax1.plot(times, theta, color='blue', label='Theta')
ax1.set_title('Theta vs Time')
ax1.set_ylabel('Theta')
ax1.legend()
ax1.grid(True)

# 绘制位移随时间变化的曲线
ax2.plot(times, displacement, color='red', label='Displacement')
ax2.set_title('Displacement vs Time')
ax2.set_xlabel('Time')
ax2.set_ylabel('Displacement')
ax2.legend()
ax2.grid(True)

# 自动调整子图间距,避免标签重叠
plt.tight_layout()
plt.show()

方案二:两个独立窗口

import matplotlib.pyplot as plt

times, theta, displacement = tacoma(dt=1, cromer=True)

# 第一张图:Theta随时间变化
plt.figure(figsize=(8, 5))
plt.plot(times, theta, color='blue')
plt.title('Theta vs Time')
plt.xlabel('Time')
plt.ylabel('Theta')
plt.grid(True)
plt.show()

# 第二张图:位移随时间变化
plt.figure(figsize=(8, 5))
plt.plot(times, displacement, color='red')
plt.title('Displacement vs Time')
plt.xlabel('Time')
plt.ylabel('Displacement')
plt.grid(True)
plt.show()

内容的提问来源于stack exchange,提问作者cookiecainsy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 17:22:11