我对python中的值错误有一些问题

2024-09-30 04:32:15 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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


Tags: csvimportdfa1asnploadplt
1条回答
网友
1楼 · 发布于 2024-09-30 04:32:15

您可能正在传递一个shape (..., 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'))

编辑:从注释中,您有形状(222, 1)strain数组。这意味着您已经有了一个向量,但该形状与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')

相关问题 更多 >

    热门问题