-1

I need to plot in python, the graph as stress and strain

with this data

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

As an aside question, can I plot live graph while Abaqus generates the txt file?

This is the code that I'm using

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()
gboffi
  • 22,939
  • 8
  • 54
  • 85

4 Answers4

1

It's easier to use numpy tools, which handle low level problems :

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)' )

gives the stress(strain) graph. Be aware that data is in lines 2 and 1.

Or even more directly with pandas :

from pandas import read_csv
(-read_csv('stress.txt',sep=' ')).plot('Strain','Stress')
B. M.
  • 18,243
  • 2
  • 35
  • 54
0

The code you've posted

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]

is almost correct...

  1. when you iterate on data you iterate also on the 1st row, that contains the heading, you want to throw away the first line using, e.g., the slice notation

    data = data.split('\n')[1:]
    
  2. when you construct x and y you are choosing the wrong element from each line

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

    note that I used the index 2 for x and 1 for y because you want to plot stresses vs strains...

  3. the lists x, y are yet not ready for plotting because they're lists of strings, you must remember to convert them to numbers (preferably when you're extracting the lists from your data)

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

There are other ways of solving your problem, maybe using numpy helper functions, but what you've tried is simple and with a couple of adjustments it can work OK.

gboffi
  • 22,939
  • 8
  • 54
  • 85
0

I finally figured it out how to plot the graph,

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()
-1

According to the Church-Turing thesis, everything is computable, so is this.

Note that instead of just hardcoding the values for y, you can also just parse the file.

If you're actively getting input data and want to continuously plot the data as it get's in, look into this thread

adapted from matplotlibs most simple example

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()

enter image description here

Community
  • 1
  • 1
Work of Artiz
  • 1,085
  • 7
  • 16