一些小细节
import re
import matplotlib.pyplot as plt
from matplotlib.pyplot import MultipleLocator
from random import shuffle
f = open('clip_length.txt', 'r')
length = []
lines = f.readlines() #读取全部内容 ,并以列表方式返回
for line in lines:
line=line.strip('\n')
length.append(int(line))
# print(length)
shuffle(length)
x = [i for i in range(1,11)]
length = [45.05, 40.8,45.11, 39.09, 45.05, 43.69, 40.34, 46.84, 42.29, 40.79]
plt.ylim(0,55) #设置y坐标范围
plt.title('Average length difference', fontsize=14) #设置标题和字体大小
plt.xlabel('Number', fontsize=12)
plt.ylabel('Length Difference', fontsize=12)
name = ['Length Difference',] #图例名字,可以多个
# plt.plot(iterations, loss_1)
# x轴按1刻度显示
x_major_locator = MultipleLocator(1)
ax = plt.gca()
ax.xaxis.set_major_locator(x_major_locator)
#设置坐标刻度字体大小
plt.xticks(fontsize=12)
plt.yticks(fontsize=12)
plt.plot(x, length,) # 画线
plt.plot(x,length,'o') # o代表显示坐标小圆点
plt.legend(name, fontsize=12) #图例
plt.savefig('length1.png', bbox_inches='tight') #其中bbox设置为紧,少留白
# plt.show()