![](/img/trans.png)
[英]Problem with matplotlib.pyplot with matplotlib.pyplot.scatter in the argument s
[英]scatter's axis and ticks in matplotlib.pyplot
我目前正在做我的数值分析作业。 我使用 python 来分析不同参数值(代码中为w
)对算法backward error
的影响。 我想使用matplotlib.pyplot
到 plot 散点图来显示结果。 但是,似乎分散看起来不像我想要的。
从图中可以看出,y轴上的值并不是从下到上递增的,它们是随机分布的,所有的点看起来都在同一条线上。 我尝试了很多方法来修复它,但都失败了。
这是错误的代码和数据文件“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()
文件“SOR2”中的数据,左列是w
,右列是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
正如@krm 评论的那样, data
需要转换为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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.