【问题标题】:VALUE ERROR: x, y, and format string must not be None. (error while plotting hysteresis loop)值错误:x、y 和格式字符串不得为无。 (绘制磁滞回线时出错)
【发布时间】:2021-09-26 09:33:14
【问题描述】:

使用低周疲劳数据,我正在尝试绘制磁滞回线。但我收到以下错误:

[ -52.18297297  -45.58565338   16.9913185  ... -354.53630032 -295.50857248
-155.42088911]
[-0.01229182 -0.00891753  0.02256744 ... -0.33507242 -0.31283728
-0.24790212]
Traceback (most recent call last):
File "f:\I2M\LCF\Ep1_camp4_P4_TTH650 06-9-21 11 01 24\ep1_camp4_P4.py", line 16, in <module>
plt.plot(strain, Sigma, color = 'k')
File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site- 
packages\matplotlib\pyplot.py", line 2840, in plot
return gca().plot(
File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site- 
packages\matplotlib\axes\_axes.py", line 1743, in plot
lines = [*self._get_lines(*args, data=data, **kwargs)]
File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site- 
packages\matplotlib\axes\_base.py", line 273, in __call__
yield from self._plot_args(this, kwargs)
File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site- 
packages\matplotlib\axes\_base.py", line 379, in _plot_args
raise ValueError("x, y, and format string must not be None")
ValueError: x, y, and format string must not be None

这是我的代码:

import matplotlib.pyplot as plt
import numpy as np
plt.style.use(['science','no-latex'])

x = np.loadtxt('F:\\I2M\\LCF\\Ep1_camp4_P4_TTH650 06-9-21 11 01 24\\data_1.csv',unpack = True, 
skiprows = 2, usecols = 2, delimiter = ',')
y = np.loadtxt('F:\\I2M\\LCF\\Ep1_camp4_P4_TTH650 06-9-21 11 01 24\\data_1.csv',unpack = True, 
skiprows = 2, usecols = 3, delimiter = ',')
stress = (x*1000)/28.27   #N/mm^2 = MPa
length = len(stress)
length = len(y)

plt.figure(figsize=(5, 5))
Sigma = print(stress[0:length:10]) #stress
strain = print(y[0:length:10])


plt.plot(strain, Sigma, color = 'k')

plt.show()

数据包含许多行。所以我使用了一些命令来仅访问行中的特定值

【问题讨论】:

  • 你检查过 x 或 y 是否有 None 元素吗?
  • 什么是strain?它的实际价值,而不是你认为应该的!

标签: python python-3.x numpy matplotlib


【解决方案1】:

你的问题来了

Sigma = print(stress[0:length:10]) #stress
strain = print(y[0:length:10])

你想要的似乎是每 10 个数据点采样一次,但你得到的是......什么都没有,或者从 Python 的角度来看:None,以便稍后你的堆栈跟踪会通知你 x, y, and format string must not be None

为什么会发生这种情况,你如何解决这个问题?

当你赋值时,右边表达式的被保存了,你可以使用左边的名字以后使用它,所以你保存,例如,print(y[0:length:10]) 返回的值 以便稍后在名称strain 下使用它,但print() 用于它的副作用(即在您的终端上显示一堆字符)和返回的值这些情况默认为​​None,而不是您终端上显示的内容。

如果我理解你的意图,你应该省略上面两行,直接使用

plt.plot(x[0:length:10], y[0:length:10], color='k')

附注,你有

length = len(stress)
length = len(y)

但是你从同一个文件中读取它们,一个作业应该就足够了……


PS

x, y = np.loadtxt('…\\data_1.csv', unpack=1, skiprows=2, usecols=[2,3], delimiter=',')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    • 1970-01-01
    • 2020-05-31
    • 2022-07-03
    • 2011-01-27
    相关资源
    最近更新 更多