下图为目标图,想用python画折线图的方式实现,但是x轴坐标刻度与折点的坐标有些出入。
目标图
代码:
import matplotlib.pyplot as plt
from pylab import * #支持中文
mpl.rcParams['font.sans-serif'] = ['SimHei']
names = ['20', '50', '100', '200']
x = range(len(names))
y0 = [0.152,0.254,0.302,0.303]
y1 = [0.215,0.279,0.305,0.306]
y2 = [0.297,0.346,0.351,0.352]
plt.plot(x, y0, marker='*', ms=10, label=u'SimplE')
plt.plot(x, y1, marker='*', ms=10, label=u'ComplEx')
plt.plot(y2, marker='*', ms=10, label=u'TuckER')
axes = plt.subplot(111)
axes.set_yticks([0.1, 0.2, 0.3, 0.4])
axes.grid(True) # add grid
plt.legend() # 让图例生效
plt.xticks(x, names)
plt.xlabel(u"词向量维度") #X轴标签
plt.ylabel("MRR") #Y轴标签
plt.title(u"词向量维度-MRR") #标题
plt.show()
图:明显20到50的距离==100到200的距离,这是不对的,怎么改?????
修改代码之后。修改的地方:1)x直接用数组,而不是用字符串再转换成数组;2)设置了x轴的刻度间隔
import matplotlib.pyplot as plt
from pylab import * #支持中文
mpl.rcParams['font.sans-serif'] = ['SimHei']
x = [20,50,100,200]
y0 = [0.152,0.254,0.302,0.303]
y1 = [0.215,0.279,0.305,0.306]
y2 = [0.297,0.346,0.351,0.352]
plt.plot(x, y0, marker='*', ms=10, label=u'SimplE')
plt.plot(x, y1, marker='*', ms=10, label=u'ComplEx')
plt.plot(x, y2, marker='*', ms=10, label=u'TuckER')
axes = plt.subplot(111)
axes = plt.gca()
axes.set_yticks([0.1, 0.2, 0.3, 0.4])
axes.grid(True) # add grid
x_major_locator = MultipleLocator(50) #把x轴的刻度间隔设置为50,并存在变量里
axes.xaxis.set_major_locator(x_major_locator)
plt.legend() # 让图例生效
plt.xlabel(u"词向量维度") #X轴标签
plt.ylabel("MRR") #Y轴标签
plt.title(u"词向量维度-MRR") #标题
plt.show()
正确的显示效果











网友评论