python - 我在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 文件的屏幕截图: 在此处输入图像描述
解决方案
您可能正在传递一个 shape 数组(..., N)
作为第一个参数(意思strain
是 form 的 shape (..., N)
)。SciPy 不允许这样做并抛出一个ValueError
. 有关详细信息,请参阅文档。如果strain
数组中有多个向量,则应该运行 for 循环。strain
考虑到您想为其中的每一行插入 1 个函数(并且该应变是二维数组。如果不是,您可以使用 轻松转换它) ,以下代码应该可以工作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
shape 数组(222, 1)
。这意味着你已经有了一个向量,但它的形状与 SciPy 接受的不兼容。在这种情况下,您必须重新调整应变和应力数组的形状,使其具有 form 的形状(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')
推荐阅读
- javascript - 使用 AJAX 选择下拉菜单选择器时遇到问题
- swiftui - SwiftUI 领域内存问题。RAM使用量不断增加
- r - 在图表上输入一个点的值
- identityserver4 - 将自定义用户存储声明添加到身份服务器的访问令牌
- python - 从比例值转换回原始比例
- c - 如何检查通过读取系统调用读入的字符是否是退格?
- spring - 为什么我的嘲笑不适用于 responseEntity 交换?
- asp.net-core - ASP.NET Core Identity 两阶段登录
- javascript - 如何在 iframe 中启用 Electron webview 标签
- ios - 使用 AppDelegate.swift 时 XCode 存档错误“没有这样的模块 flutter_downloader”