【问题标题】:I have some problems with a value error in python我在python中遇到了一些值错误的问题
【发布时间】:2021-01-19 18:51:57
【问题描述】:

这是我绘制应力-应变曲线的代码

import matplotlib.pyplot as plt
import numpy as np
import math
from scipy.interpolate import interp1d
from matplotlib.offsetbox import AnchoredText
import pandas as pd


#both strain is a column in the given dataframe, and I manually calculated stress
df_1 = pd.read_csv('1045.csv',skiprows=25,header=[0,1])
print(df_1.head())

A1 = 40.602*(10e-6)
stress1 = ((df_1.Load)/A1)


plt.figure(figsize=(12,9))
plt.plot(df_1.Strain1.values,df_1.Load.values,'g')
plt.ylabel('stress(Pa)',fontsize=13)
plt.xlabel('Strain(%)',fontsize=13)
plt.xticks(np.arange(-6e-5,0.15,step=0.005),rotation = 45)
plt.yticks(np.arange(0,42000,step=1000))

strain = df_1.Strain1.values
stress = np.array(((df_1.Load.values)/A1))
strain = np.array((df_1.Strain1.values))

LinearLimit=1
Strain_values_linear = np.linspace(strain[0], strain[LinearLimit], num=50, endpoint=True)
Strain_values_eng = np.linspace(strain[LinearLimit], strain[-1], num=50, endpoint=True)
f1 = interp1d(strain, stress, fill_value='extrapolate')
f2 = interp1d(strain, stress, kind=3, fill_value='extrapolate')

现在我不断收到一个值错误消息:“x 和 y 数组沿插值轴的长度必须相等。”我不明白这个......我打印了应变和应力的形状,它们是一样的 顺便说一句,这里是 csv 文件的屏幕截图: enter image description here

【问题讨论】:

    标签: python scipy valueerror


    【解决方案1】:

    您可能正在传递一个形状为(..., N) 的数组作为第一个参数(意味着strain 的形状为(..., N))。 SciPy 不允许这样做并抛出ValueError。有关详细信息,请参阅documentation。如果strain 数组中有多个向量,则应该运行 for 循环。以下代码应该可以工作,考虑到您想为strain 中的每一行插入一个函数(并且该应变是一个二维数组。如果不是,您可以使用strain.reshape(-1, N) 轻松转换它):

    import matplotlib.pyplot as plt
    import numpy as np
    import math
    from scipy.interpolate import interp1d
    from matplotlib.offsetbox import AnchoredText
    import pandas as pd
    
    
    #both strain is a column in the given dataframe, and I manually calculated stress
    df_1 = pd.read_csv('1045.csv',skiprows=25,header=[0,1])
    print(df_1.head())
    
    A1 = 40.602*(10e-6)
    stress1 = ((df_1.Load)/A1)
    
    
    plt.figure(figsize=(12,9))
    plt.plot(df_1.Strain1.values,df_1.Load.values,'g')
    plt.ylabel('stress(Pa)',fontsize=13)
    plt.xlabel('Strain(%)',fontsize=13)
    plt.xticks(np.arange(-6e-5,0.15,step=0.005),rotation = 45)
    plt.yticks(np.arange(0,42000,step=1000))
    
    strain = df_1.Strain1.values
    stress = np.array(((df_1.Load.values)/A1))
    strain = np.array((df_1.Strain1.values))
    
    LinearLimit=1
    Strain_values_linear = np.linspace(strain[0], strain[LinearLimit], num=50, endpoint=True)
    Strain_values_eng = np.linspace(strain[LinearLimit], strain[-1], num=50, endpoint=True)
    
    f1, f2 = [], []
    for row in range(len(strain)):
        f1.append(interp1d(strain[row], stress, fill_value='extrapolate'))
        f2.append(interp1d(strain[row], stress, kind=3, fill_value='extrapolate'))
    

    编辑:从评论中,您有 strain 形状数组 (222, 1)。这意味着你已经有了一个向量,但它的形状与 SciPy 接受的不兼容。在这种情况下,您必须重新调整应变和应力数组的形状,使其具有(N,) 形式的形状。以下代码应该可以工作:

    import matplotlib.pyplot as plt
    import numpy as np
    import math
    from scipy.interpolate import interp1d
    from matplotlib.offsetbox import AnchoredText
    import pandas as pd
    
    
    #both strain is a column in the given dataframe, and I manually calculated stress
    df_1 = pd.read_csv('1045.csv',skiprows=25,header=[0,1])
    print(df_1.head())
    
    A1 = 40.602*(10e-6)
    stress1 = ((df_1.Load)/A1)
    
    
    plt.figure(figsize=(12,9))
    plt.plot(df_1.Strain1.values,df_1.Load.values,'g')
    plt.ylabel('stress(Pa)',fontsize=13)
    plt.xlabel('Strain(%)',fontsize=13)
    plt.xticks(np.arange(-6e-5,0.15,step=0.005),rotation = 45)
    plt.yticks(np.arange(0,42000,step=1000))
    
    strain = df_1.Strain1.values
    stress = np.array(((df_1.Load.values)/A1))
    strain = np.array((df_1.Strain1.values))
    strain = strain.reshape(-1,)
    stress = stress.reshape(-1,)
    
    LinearLimit=1
    Strain_values_linear = np.linspace(strain[0], strain[LinearLimit], num=50, endpoint=True)
    Strain_values_eng = np.linspace(strain[LinearLimit], strain[-1], num=50, endpoint=True)
    f1 = interp1d(strain, stress, fill_value='extrapolate')
    f2 = interp1d(strain, stress, kind=3, fill_value='extrapolate')
    

    【讨论】:

    • 我打印了应变和应力阵列的形状,它们都是 (222,1)。我尝试使用您在上面提供的 for 外观,但在这里出现另一个错误:ValueError:x 和 y 数组必须至少有 2 个条目。我不明白这是什么意思
    • 我在上面添加了我的 csv 文件的屏幕截图
    • 你已经有了一个向量,所以不需要运行 for 循环。您将不得不使用strain = strain.reshape(-1,) 重塑您的strain 数组以塑造(222,)。其他一切在其他条件不变的情况下。我会相应地更新答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 2022-11-23
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多