创建从数据点延伸到精确曲线的误差线
Creating error bar that extends from data point to an exact curve
我有以下用 matplotlib 生成的图。
我想绘制从数据点到红色曲线的误差线。我已经尝试过 matplotlib.pyplot.errorbars()
函数,但我找不到合适的方法来做到这一点。
这是情节的代码:
# imports
import numpy as np
import matplotlib.pyplot as plt
# data for scatter plot
target_time = np.array([0., 1.001001, 2.002002, 3.003003, 4.004004, 5.00500501, 6.00600601, 7.00700701, 8.00800801, 9.00900901])
results = np.array([0.9073288221126276, 0.16595939901364837, 0.37664869824521163, 0.7899988530794816, 0.03750430095194403, 0.5939901364835417, 0.6294299805023512, 0.031081546048858814, 0.7823144856061475, 0.4050923271017319])
# exact curve
time = np.arange(0, 1000, 0.01)
y = np.cos(2*time)**2
# plot data
plt.scatter(target_time, results, label='Simulation Result')
# plot exact curve
plt.plot(time, y, color='red', label='Exact Curve')
plt.xlim(0, 10)
plt.xlabel('Time')
plt.ylabel('Probability Density')
plt.title('Noisy Simulation: Time Dependent Probability Density')
plt.legend()
plt.show()
您可以使用 matplotlib.pyplot.errorbars 通过指定错误来执行此操作。这里的误差由蓝点和红色曲线之间的距离给出。在代码中,假设错误在 y-direction 中,我已经为您完成了。对于 x-direction.
中的错误,这可以类似地完成
import numpy as np
import matplotlib.pyplot as plt
# data for scatter plot
target_time = np.array([0., 1.001001, 2.002002, 3.003003, 4.004004, 5.00500501, 6.00600601, 7.00700701, 8.00800801, 9.00900901])
results = np.array([0.9073288221126276, 0.16595939901364837, 0.37664869824521163, 0.7899988530794816, 0.03750430095194403, 0.5939901364835417, 0.6294299805023512, 0.031081546048858814, 0.7823144856061475, 0.4050923271017319])
# exact curve
time = np.arange(0, 1000, 0.01)
y = np.cos(2*time)**2
# plot data
yerr = abs(results - np.cos(2*target_time)**2) #calcuating the y-error
plt.errorbar(target_time, results,yerr=yerr, marker='o', ls ='none', label='Simulation Result') #This plots both the points AND the error bars. I replaced plt.scatter with this.
# plot exact curve
plt.plot(time, y, color='red', label='Exact Curve')
plt.xlim(0, 10)
plt.xlabel('Time')
plt.ylabel('Probability Density')
plt.title('Noisy Simulation: Time Dependent Probability Density')
plt.legend()
plt.show()
此外,您还可以添加花哨的样式,例如错误栏上限、颜色等。请参考维基页面:https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.errorbar.html
我有以下用 matplotlib 生成的图。
我想绘制从数据点到红色曲线的误差线。我已经尝试过 matplotlib.pyplot.errorbars()
函数,但我找不到合适的方法来做到这一点。
这是情节的代码:
# imports
import numpy as np
import matplotlib.pyplot as plt
# data for scatter plot
target_time = np.array([0., 1.001001, 2.002002, 3.003003, 4.004004, 5.00500501, 6.00600601, 7.00700701, 8.00800801, 9.00900901])
results = np.array([0.9073288221126276, 0.16595939901364837, 0.37664869824521163, 0.7899988530794816, 0.03750430095194403, 0.5939901364835417, 0.6294299805023512, 0.031081546048858814, 0.7823144856061475, 0.4050923271017319])
# exact curve
time = np.arange(0, 1000, 0.01)
y = np.cos(2*time)**2
# plot data
plt.scatter(target_time, results, label='Simulation Result')
# plot exact curve
plt.plot(time, y, color='red', label='Exact Curve')
plt.xlim(0, 10)
plt.xlabel('Time')
plt.ylabel('Probability Density')
plt.title('Noisy Simulation: Time Dependent Probability Density')
plt.legend()
plt.show()
您可以使用 matplotlib.pyplot.errorbars 通过指定错误来执行此操作。这里的误差由蓝点和红色曲线之间的距离给出。在代码中,假设错误在 y-direction 中,我已经为您完成了。对于 x-direction.
中的错误,这可以类似地完成import numpy as np
import matplotlib.pyplot as plt
# data for scatter plot
target_time = np.array([0., 1.001001, 2.002002, 3.003003, 4.004004, 5.00500501, 6.00600601, 7.00700701, 8.00800801, 9.00900901])
results = np.array([0.9073288221126276, 0.16595939901364837, 0.37664869824521163, 0.7899988530794816, 0.03750430095194403, 0.5939901364835417, 0.6294299805023512, 0.031081546048858814, 0.7823144856061475, 0.4050923271017319])
# exact curve
time = np.arange(0, 1000, 0.01)
y = np.cos(2*time)**2
# plot data
yerr = abs(results - np.cos(2*target_time)**2) #calcuating the y-error
plt.errorbar(target_time, results,yerr=yerr, marker='o', ls ='none', label='Simulation Result') #This plots both the points AND the error bars. I replaced plt.scatter with this.
# plot exact curve
plt.plot(time, y, color='red', label='Exact Curve')
plt.xlim(0, 10)
plt.xlabel('Time')
plt.ylabel('Probability Density')
plt.title('Noisy Simulation: Time Dependent Probability Density')
plt.legend()
plt.show()
此外,您还可以添加花哨的样式,例如错误栏上限、颜色等。请参考维基页面:https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.errorbar.html