[英]How to create asymmetric errorbars without triggering broadcast error?
我正在尝试使用 seaborn plot 一些不对称错误栏。我不明白为什么我会收到ValueError: operands could not be broadcast together with shapes (3,1) (2,3)
这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
truth = np.array([0.15725964, 0.15611989, 0.15820897])
hpd = np.array([[0.00310974, 0.01833195],
[0.00546891, 0.017973 ],
[0.00687474, 0.01628064]])
median = np.array([[0.15517015],[0.12985473],[0.12510344]])
with sns.plotting_context('notebook', font_scale=1.2):
fig, axmatrix = plt.subplots(ncols=2, figsize=(16,8))
for ax in axmatrix.flatten():
ax.set_aspect('equal')
def plot_hpd_err(truth, hpd, median):
err = np.absolute(np.transpose(hpd - median@np.ones((1,2))))
return ax.errorbar(truth[:,np.newaxis], median, yerr=err)
plot_hpd_err(truth, hpd, median)
truth[:,np.newaxis]
、 median
和err
的形状是 (3,1); (3,1); 和(2,3)分别。err
格式化为 (2,N) 形状,因为那是axes.errorbar
文档告诉我要做的truth[:, np.newaxis]
和hpd
以匹配err
的形状时,它会抛出错误。最终,我想要三个数据点及其各自的非对称错误栏,但我目前无法获得 ErrorbarContainer object 没有任何错误(是的,这些图目前是空白的......)
truth
和hpd
的大小必须是(N,); 它不能是 (N, 1)。 因此,代码应该阅读
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
truth = np.array([0.15725964, 0.15611989, 0.15820897])
hpd = np.array([[0.00310974, 0.01833195],
[0.00546891, 0.017973 ],
[0.00687474, 0.01628064]])
median = np.array([0.15517015, 0.12985473, 0.12510344])
with sns.plotting_context('notebook', font_scale=1.2):
fig, axmatrix = plt.subplots(ncols=2, figsize=(16,8))
for ax in axmatrix.flatten():
ax.set_aspect('equal')
def plot_hpd_err(truth, hpd, median):
err = np.absolute(np.transpose(hpd - (median[:, np.newaxis])@np.ones((1,2))))
return ax.errorbar(truth, median, yerr=err)
plot_hpd_err(truth, hpd, median)
感谢这个答案帮助我解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.