1

I'm currently doing my numerical analysis homework. I use python to analyze the influence of different parameter's values (which is w in the code) on the backward error in an algorithm. I want to use matplotlib.pyplot to plot a scatter to show the result. But, it seems that the scatter doesn't look like what I want.

As you can see from the figure, the values on y-axis is not ascending from bottom to top, they distribute randomly, and all the points seems like they are at the same line. I've tried a lot of methods to fix it but failed.

Here's the wrong piece of code and data file "SOR2".

import matplotlib.pyplot as plt 
import numpy as np

# read SOR2
SOR2 = open("SOR2", 'r')
w = []
e = []
for line in SOR2:
    data = line.strip().split()
    w.append(data[0])
    e.append(data[1])
SOR2.close()

# plot scatter
plt.xlabel("w")
plt.ylabel("backward error")
plt.scatter(w, e)
plt.show()

The data in file "SOR2", the left column is w, and the right column is backward error:

0.50    1.05549
0.51    1.01085
0.52    0.96795
0.53    0.92669
0.54    0.88701
0.55    0.84883
0.56    0.81210
0.57    0.77676
0.58    0.74274
0.59    0.70999
0.60    0.67847
0.61    0.64811
0.62    0.61889
0.63    0.59075
0.64    0.56366
0.65    0.53758
0.66    0.51247
0.67    0.48829
0.68    0.46502
0.69    0.44263
0.70    0.42107
0.71    0.40034
0.72    0.38039
0.73    0.36120
0.74    0.34276
0.75    0.32503
0.76    0.30799
0.77    0.29163
0.78    0.27592
0.79    0.26084
0.80    0.24638
0.81    0.23251
0.82    0.21921
0.83    0.20648
0.84    0.19429
0.85    0.18263
0.86    0.17148
0.87    0.16083
0.88    0.15067
0.89    0.14097
0.90    0.13173
0.91    0.12293
0.92    0.11457
0.93    0.10662
0.94    0.09908
0.95    0.09193
0.96    0.08516
0.97    0.07876
0.98    0.07272
0.99    0.06702
1.00    0.06166
1.01    0.05663
1.02    0.05190
1.03    0.04748
1.04    0.04335
1.05    0.03950
1.06    0.03599
1.07    0.03276
1.08    0.02977
1.09    0.02699
1.10    0.02442
1.11    0.02208
1.12    0.01993
1.13    0.01794
1.14    0.01609
1.15    0.01438
1.16    0.01280
1.17    0.01139
1.18    0.01009
1.19    0.00890
1.20    0.00791
1.21    0.00706
1.22    0.00630
1.23    0.00560
1.24    0.00498
1.25    0.00441
1.26    0.00402
1.27    0.00384
1.28    0.00434
1.29    0.00514
1.30    0.00610
1.31    0.00723
1.32    0.00856
1.33    0.01013
1.34    0.01196
1.35    0.01408
1.36    0.01655
1.37    0.01940
1.38    0.02268
1.39    0.02645
1.40    0.03077
1.41    0.03571
1.42    0.04133
1.43    0.04773
1.44    0.05498
1.45    0.06319
1.46    0.07246
1.47    0.08291
1.48    0.09466
1.49    0.10786

And the result looks like this: SOR2_Scatter

1
  • 2
    I think you need to convert your data into float, otherwise, they just remain as strings when you read them in. Use w.append(float(data[0])), and similar for e. Commented Apr 12, 2021 at 5:49

1 Answer 1

1

As @krm commented, data needs to be converted to float:

w.append(float(data[0]))
e.append(float(data[1]))

Alternatively you can use pandas to simplify all the parsing and plotting down to 2 lines with pandas.read_fwf() and DataFrame.plot.scatter():

import pandas as pd

df = pd.read_fwf('SOR2', header=None, names=['w', 'e'])
df.plot.scatter(x='w', y='e', ylabel='backward error')

SOR2 pandas plot

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.