ValueError:x和y必须具有相同的第一维,但形状为(21,)和(22,)
我正在尝试从数组生成图形,但结果总是出现 ValueError
错误。我该怎么办?
这是我的代码:
# array bisection
array_list_iter_bisect = np.array(list_iter_bisect)
array_list_ea_bisect = np.array(list_ea_bisect)
# array false position
array_list_iter_falsepost = np.array(list_iter_falsepost)
array_list_ea_falsepost = np.array(list_ea_falsepost)
iter_max = 0
matrix_iter_count = [array_list_ea_bisect,array_list_iter_falsepost]
for arr_iter in matrix_iter_count:
if (iter_max < arr_iter[-1]):
iter_max = arr_iter[-1]
# Parameter for the constant function of Error Max
error_max = es
x_error_max = range(1, iter_max+1)
y_error_max = [error_max] * len(x_error_max)
# Plotting the graph
plt.plot(list_iter_bisect, list_ea_bisect, c='blue')
plt.plot(list_iter_falsepost, list_ea_falsepost, c='green')
plt.plot(x_error_max, y_error_max, c='black')
plt.plot(x_error_max, y_error_max, c='black')
plt.annotate('εs', xy=(0, error_max), xytext=(-1, error_max), fontsize=12)
plt.xlabel('Iteration #')
plt.ylabel('εa (error)')
plt.title(f'εa method vs iteration Z-factor | T = {660}R | P = {3000} psia')
plt.legend(["Bisection", "False-Position"], bbox_to_anchor=(0.75, 1.15), ncol=2)
plt.grid(True)
plt.show()
我需要把数组中的某个元素弹出吗?
1 个回答
0
在不知道错误发生在哪一行,也不知道 list_iter_bisect、list_ea_bisect、list_iter_falsepost 和 list_ea_falsepost 的内容或大小的情况下,
我觉得错误可能出现在这里:
plt.plot(x_error_max, y_error_max, c='black')
这里面有一些定义:
x_error_max = range(1, iter_max+1)
y_error_max = [error_max] * len(x_error_max)
试着用以下方式定义 x_error_max:
x_error_max = np.arange(1, iter_max+1)
或者
x_error_max = range(1, iter_max)
我建议不要重复这一行:
plt.plot(x_error_max, y_error_max, c='black')
两次