简体   繁体   English

ValueError:形状不匹配:无法将对象广播到单个形状。 不匹配在 arg 0 与形状 (48000,) 和 arg 1 与形状 (2,) 之间

[英]ValueError: shape mismatch: objects cannot be broadcast to a single shape. Mismatch is between arg 0 with shape (48000,) and arg 1 with shape (2,)

I tried to animate a simple (sine)^3 fucntion using PillowWriter, but stuck with this error.我尝试使用 PillowWriter 为一个简单的(正弦)^3 函数设置动画,但遇到了这个错误。 Belowmentioned is my code for the same:下面提到的是我的代码:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import PillowWriter

def y(x):
    z = 3 * np.sin(x)**3
    return z

fig = plt.figure()
l, = plt.plot([], [], 'g-')

plt.xlim(-12,12)
plt.ylim(-12,12)

metadata = dict(title="3[sin(x)]^3", artist="Me")
writer = PillowWriter(fps=20, metadata=metadata)

x_axis = np.arange(-12, 12, 0.0005)
y_axis = []

with writer.saving(fig, "y(x).gif", 100):
    for x in x_axis:
        y_axis.append(y(x))

        l.set_data(x_axis, y_axis)
        writer.grab_frame()

It seems that your y_axis is not long enough, so the plot does not know how to compare it to the x_axis array.看来您的y_axis不够长,所以绘图不知道如何将其与x_axis数组进行比较。 I would suggest the following solution:我建议以下解决方案:

from matplotlib.axis import XAxis
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import PillowWriter

def y(x):
    z = 3 * np.sin(x)**3
    return z

fig = plt.figure()
l, = plt.plot([], [], 'g-')

plt.xlim(-12,12)
plt.ylim(-12,12)

metadata = dict(title="3[sin(x)]^3", artist="Me")
writer = PillowWriter(fps=20, metadata=metadata)

x_axis = np.arange(-12, 12, 0.05) # 0.0005 causes performance issues
y_axis = np.zeros(len(x_axis))

with writer.saving(fig, "y(x).gif", 100):
    for n, x_val in enumerate(x_axis):
        y_axis[n] = y(x_val)
        l.set_data(x_axis, y_axis)
        writer.grab_frame()

Explanation: It creates a y_axis array which is of equal length as the x_axis array, but is filled with zeros.解释:它创建一个与x_axis数组长度相等的y_axis数组,但用零填充。 Then, for each frame, it counts and sets the next value of the y_axis array equal to the function value.然后,对于每一帧,它计算并设置y_axis数组的下一个值等于函数值。 Note: I reduced the resolution of the x_axis array because it was giving me performance issues.注意:我降低了x_axis数组的分辨率,因为它给我带来了性能问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:形状不匹配:对象不能广播到单个形状 - ValueError: shape mismatch: objects cannot be broadcast to a single shape Python ValueError:形状不匹配:无法将对象广播为单个形状 - Python ValueError: shape mismatch: objects cannot be broadcast to a single shape 熊猫数据框,ValueError:形状不匹配:对象无法广播为单个形状 - Pandas dataframe, ValueError: shape mismatch: objects cannot be broadcast to a single shape ValueError:形状不匹配:绘制时对象不能广播到单个形状 - ValueError: shape mismatch: objects cannot be broadcast to a single shape when plotting ValueError:形状不匹配:无法将对象广播到单个形状 Python 错误 - ValueError: shape mismatch: objects cannot be broadcast to a single shape Python Error ValueError 形状不匹配对象无法广播到单个形状 - ValueError shape mismatch objects cannot be broadcast to a single shape ValueError 形状不匹配:对象不能广播到单个形状 - ValueError shape mismatch: objects cannot be broadcast to a single shape Matplotlib:形状不匹配:对象不能广播为单个形状 - Matplotlib: shape mismatch: objects cannot be broadcast to a single shape Shap LSTM(Keras,TensorFlow)ValueError:形状不匹配:对象不能广播到单个形状 - Shap LSTM (Keras, TensorFlow) ValueError: shape mismatch: objects cannot be broadcast to a single shape 绘制 PMF jupyter 笔记本:ValueError:形状不匹配:无法将对象广播到单个形状 - Plotting PMF jupyter notebook : ValueError: shape mismatch: objects cannot be broadcast to a single shape
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM