需要安装matplotlib库,可以用如下命令安装:
pip
install
matplotlib
txt文本数据如下所示(示例中的每一行内部用空格分开):
100 0.6692215
200 0.57682794
300 0.45037615
400 0.42214713
500 0.45073098
600 0.4728373
700 0.48083866
800 0.3751492
900 0.4249844
1000 0.36427215
1100 0.36209464
1200 0.40490758
1300 0.3774191
1400 0.34719718
1500 0.3648946
1600 0.261855
1700 0.4321903
1800 0.35071397
1900 0.279996
2000 0.30030474
适用于Python3的代码如下所示:
import
matplotlib
.
pyplot
as
plt
input_txt
=
'demo.txt'
x
=
[
]
y
=
[
]
f
=
open
(
input_txt
)
for
line
in
f
:
line
=
line
.
strip
(
'\n'
)
line
=
line
.
split
(
' '
)
x
.
append
(
float
(
line
[
0
]
)
)
y
.
append
(
float
(
line
[
1
]
)
)
f
.
close
plt
.
plot
(
x
,
y
,
marker
=
'o'
,
label
=
'lost plot'
)
plt
.
xticks
(
x
[
0
:
len
(
x
)
:
2
]
,
x
[
0
:
len
(
x
)
:
2
]
,
rotation
=
45
)
plt
.
margins
(
0
)
plt
.
xlabel
(
"train step"
)
plt
.
ylabel
(
"lost"
)
plt
.
title
(
"matplotlip plot"
)
plt
.
tick_params
(
axis
=
"both"
)
plt
.
show
(
)
更多内容:
matplotlib官方绘图示例参考:https://matplotlib.org/gallery/index.html
matplotlib PDF手册:https://matplotlib.org/Matplotlib.pdf
matplotlib API文档:https://matplotlib.org/api/index.html