python - 如何在 matplotlib 中添加垂直微分线(或残差)?
问题描述
我想添加垂直线,分别用红色和蓝色虚线来描绘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.vlines(x, y1_predict, y_true, color='r')
plt.vlines(x, y2_predict, y_true, color='b')
推荐阅读
- xamarin - Xamarin :: Android - 此处定义的原始属性
- python - 气流 - 如何从上次成功的实例运行中执行 DAG?
- python - 在数据框列上应用函数以获取其他几个列 Pandas Python
- xamarin - 我想在 xaml 中使用按钮获取参数
- python - 为什么这两个 numpy 数组分配有区别?
- amazon-web-services - 用于 jumpbox 的 AWS CloudFormation 用户数据
- python - 如何从一列文本观察中删除没有任何意义的单词
- python - 平均成绩和“如果”
- ibm-odm - IBM ODM 无法生成有效的 WADL
- spacy - spacy.matcher.PhraseMatcher object has no attribute "remove"