【问题标题】:Python data subtraction and plottingPython数据减法和绘图
【发布时间】:2014-02-10 19:53:46
【问题描述】:

我有两个数据集 f 和 g。每个都是不同的长度。我需要绘制 f 和 g 与时间 t 的差异。但是,它不会减去,因为它给出了ValueError:操作数无法与形状一起广播(2,11944)(2,23600)”。我也不确定如何使变量 t 与 x 长度相同,因为它一直告诉我“ValueError:x 和 y 必须具有相同的第一维”。这是到目前为止的代码:

f=np.loadtxt('Single Small Angle 1.txt',unpack=True,skiprows=2)
g=np.loadtxt('Single Small Angle 3.txt',unpack=True,skiprows=2)

x=f-g
t=[]

plt.plot(t,x)
#plt.xlabel("${\Theta}$ [rad]")
#plt.ylabel("${\Omega}$ [rad/s]")
#plt.title("Small Angle Approximation Phase Space")
plt.show()

然后我需要找到 x 与 t 的指数拟合,我也不知道该怎么做。

【问题讨论】:

  • 试试x = f-g[:,:11944]
  • 这行得通,但是我仍然不确定要做什么,因为它仍然给出第二个 ValueError
  • 您能发布您的错误消息的完整文本吗?
  • 我发现了另一个可能的错误,为什么t 在您的代码中为空? t 必须与 x 具有相同的维度才能绘图。

标签: python arrays list matplotlib


【解决方案1】:

您可以将第二个操作数g 限制为与f 相同的形状:x = f-g[:,:11944]x = f-g[:,:f.shape[1]] 还必须将t 设置为与x 具有相同的第一个维度,试试这个:

t = np.arange(f.shape[0]*f.shape[1]).reshape(f.shape)
plt.plot(t,x) #this operation will take a long time!

【讨论】:

    猜你喜欢
    • 2022-01-22
    • 2015-06-24
    • 2022-11-17
    • 1970-01-01
    • 2011-10-04
    • 1970-01-01
    • 2016-06-06
    相关资源
    最近更新 更多