-1

我需要在 python 中绘制,图形为应力和应变

有了这个数据

S.No Stress Strain
0 0.000000 0.000000
1 -3343.068596 -0.520833
2 -3359.542402 -1.041667
3 -3363.690275 -1.562500
4 -3368.874071 -2.343750
5 -3375.428713 -3.515625
6 -3377.689516 -3.955078
7 -3380.871487 -4.614258
8 -3385.274720 -5.603027
9 -3386.895892 -5.973816
10 -3389.195531 -6.529999
11 -3392.487109 -7.364273
12 -3397.171464 -8.615685
13 -3398.871128 -9.084964
14 -3401.357499 -9.788883
15 -3404.965858 -10.844761
16 -3406.306571 -11.240716
17 -3408.273823 -11.834647
18 -3411.164927 -12.725545
19 -3412.242114 -13.059631
20 -3413.836265 -13.560761
21 -3416.195332 -14.312456
22 -3419.671825 -15.439998
23 -3420.967483 -15.862826
24 -3422.889965 -16.497069
25 -3425.741024 -17.448432
26 -3426.805424 -17.805193
27 -3428.391134 -18.340335
28 -3430.751166 -19.143049
29 -3434.254359 -20.347118
30 -3435.562196 -20.798644
31 -3437.512479 -21.475933
32 -3440.417226 -22.491867
33 -3441.502922 -22.872842
34 -3443.125125 -23.444305
35 -3445.546054 -24.301499
36 -3446.451645 -24.622947
37 -3447.806274 -25.105118
38 -3449.830488 -25.828376
39 -3452.850690 -26.913262
40 -3453.980182 -27.320094
41 -3455.669727 -27.930343
42 -3458.194084 -28.845716
43 -3459.138722 -29.188980
44 -3460.552807 -29.703877
45 -3462.667548 -30.476223
46 -3465.826112 -31.634742
47 -3466.981187 -32.059390
48 -3468.710177 -32.696362
49 -3470.435183 -33.333334

作为一个问题,我可以在 Abaqus 生成 txt 文件时绘制实时图表吗?

这是我正在使用的代码

import numpy as np
import matplotlib.pyplot as plt

with open("test-1-14M.txt") as f:
    data = f.read()

data = data.split('\n')

x = [row.split(' ')[0] for row in data]
y = [row.split(' ')[0] for row in data]

fig = plt.figure()

ax1 = fig.add_subplot(70)

ax1.set_title("Plot title...")    
ax1.set_xlabel('your x label..')
ax1.set_ylabel('your y label...')

ax1.plot(x, y, c='r', label='the data')

leg = ax1.legend()

plt.show()
4

4 回答 4

1

使用处理低级问题的 numpy 工具更容易:

from pylab import *
# load and change signs; transpose for data in lines :
data=-loadtxt('stress.txt',delimiter=' ',skiprows=1).T 
plot(data[2],data[1])
title( 'stress(strain)' )

给出应力(应变)图. 请注意,数据位于第 2 行和第 1 行。

或者更直接地使用 pandas :

from pandas import read_csv
(-read_csv('stress.txt',sep=' ')).plot('Strain','Stress')
于 2016-03-15T12:09:01.133 回答
0

我终于弄清楚了如何绘制图表,

import numpy as np
import matplotlib.pyplot as plt


with open('3cube_pbc_compression_ss.txt') as f:
     data = np.loadtxt(f, delimiter=" ", dtype='float', comments="#", skiprows=0, usecols=[1,2])
 
x=data[:,1]
y=data[:,0]
fig = plt.figure()
fig.suptitle('Stress-Strain Curve of Hastyelloy C-276', fontsize=18, fontweight='bold')
plt.xlabel('Strain (%)',fontsize=14,fontweight='bold') 
plt.ylabel('Stress(MPa)',fontsize=14,fontweight='bold')
plt.plot(-x, -y, c='r', label='RT')
fig.savefig('SS-Curve.jpg', bbox_inches='tight',format='jpg', dpi=1000)
fig.savefig('SS-Curve.eps', bbox_inches='tight',format='eps', dpi=1000)
leg = plt.legend()
plt.show()

于 2016-03-21T04:02:38.497 回答
0

您发布的代码

with open("test-1-14M.txt") as f:
    data = f.read()

data = data.split('\n')

x = [row.split(' ')[0] for row in data]
y = [row.split(' ')[0] for row in data]

几乎是正确的...

  1. 当您迭代时,data您也在第一行(包含标题)上进行迭代,您想使用例如切片符号丢弃第一行

    data = data.split('\n')[1:]
    
  2. 当您构建x并且y从每一行中选择错误的元素时

    # see below for THE correct solution
    x = [row.split()[2] for row in data]
    y = [row.split()[1] for row in data]
    

    请注意,我使用索引2forx1fory因为您想绘制应力与应变的关系图...

  3. 列表xy还没有准备好绘图,因为它们是字符串列表,你必须记住将它们转换为数字(最好从你的列表中提取列表时data

    x = [float(row.split()[2]) for row in data]
    y = [float(row.split()[1]) for row in data]
    

还有其他方法可以解决您的问题,也许使用numpy辅助函数,但是您尝试过的方法很简单,只需进行几次调整就可以正常工作。

于 2016-03-15T13:01:08.373 回答
-1

根据 Church-Turing 理论,一切都是可计算的,这也是。

请注意,您不仅可以硬编码 y 的值,还可以只解析文件。

如果您正在积极获取输入数据并希望在输入数据时连续绘制数据,请查看此线程

改编自 matplotlibs 最简单的例子

import matplotlib.pyplot as plt
import numpy as np

y = np.array([0, 0, 0, 1, -3343.068596, -0.520833, 2, -3359.542402, -1.041667,
              3, -3363.690275, -1.562500, 4, -3368.874071, -2.343750, 5,
              -3375.428713, -3.515625, 6, -3377.689516, -3.955078, 7,
              -3380.871487, -4.614258, 8, -3385.274720, -5.603027, 9,
              -3386.895892, -5.973816, 10, -3389.195531, -6.529999, 11,
              -3392.487109, -7.364273, 12, -3397.171464, -8.615685, 13,
              -3398.871128, -9.084964, 14, -3401.357499, -9.788883, 15,
              -3404.965858, -10.844761, 16, -3406.306571, -11.240716, 17,
              -3408.273823, -11.834647, 18, -3411.164927, -12.725545, 19,
              -3412.242114, -13.059631, 20, -3413.836265, -13.560761, 21,
              -3416.195332, -14.312456, 22, -3419.671825, -15.439998, 23,
              -3420.967483, -15.862826, 24, -3422.889965, -16.497069, 25,
              -3425.741024, -17.448432, 26, -3426.805424, -17.805193, 27,
              -3428.391134, -18.340335, 28, -3430.751166, -19.143049, 29,
              -3434.254359, -20.347118, 30, -3435.562196, -20.798644, 31,
              -3437.512479, -21.475933, 32, -3440.417226, -22.491867, 33,
              -3441.502922, -22.872842, 34, -3443.125125, -23.444305, 35,
              -3445.546054, -24.301499, 36, -3446.451645, -24.622947, 37,
              -3447.806274, -25.105118, 38, -3449.830488, -25.828376, 39,
              -3452.850690, -26.913262, 40, -3453.980182, -27.320094, 41,
              -3455.669727, -27.930343, 42, -3458.194084, -28.845716, 43,
              -3459.138722, -29.188980, 44, -3460.552807, -29.703877, 45,
              -3462.667548, -30.476223, 46, -3465.826112, -31.634742, 47,
              -3466.981187, -32.059390, 48, -3468.710177, -32.696362, 49,
              -3470.435183, -33.333334])
x = np.arange(len(y))
plt.plot(x, y)

plt.xlabel('0...n (made up as you didnt give any x data)')
plt.ylabel('your data')
plt.title('About as simple as it gets, folks')
plt.grid(True)
plt.savefig("test.png")
plt.show()

在此处输入图像描述

于 2016-03-15T10:49:36.437 回答