【发布时间】:2020-06-02 19:50:41
【问题描述】:
我想添加表示y1_predict - y_true 和y2_predict - y_true 残差的垂直线,分别用红色和蓝色虚线表示。我更喜欢在当前图表的顶部添加线条,我可以编写的代码如下所示。如果需要从头开始绘制,请忽略我的代码。
import matplotlib.pyplot as plt
x = x_data
ys = [y_true,y1_predict,y2_predict]
labels = ['EMT', 'NN 1st round', 'NN 2nd round']
markers = ['o','s','D']
colors = ['k','r','b']
alphas = [1, 1, 1]
linestyles = ['None','None','None']
fillstyles = ['full','none','none']
plt.figure(figsize=(8, 6), dpi=100, facecolor='w', edgecolor='k')
plt.xlabel("Structure", fontsize=12)
plt.ylabel("Relaxed energy per atom / eV", fontsize=12)
plt.title("NN vs. EMT (low energy region)")
for y, label, marker, color, alpha, linestyle, fillstyle in zip(ys, labels, markers, colors, alphas, linestyles, fillstyles):
plt.plot(x,y, label=label, marker=marker, color=color, alpha=alpha, linestyle=linestyle, fillstyle=fillstyle)
plt.ylim([0.21, 0.25])
plt.legend(loc="lower left",prop={'size': 14})
plt.savefig('nn-emt.png')
【问题讨论】:
-
尝试使用
plt.axvline(y1_predict - y_true, c='r', ls='--')和plt.axvline(y2_predict - y_true, c='b', ls='--') -
由于 y1_predict、y2_predict 和 y_true 都是 1darrays,我得到以下错误:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
标签: python matplotlib vline