繁体   English   中英

如何在不触发广播错误的情况下创建非对称错误栏?

[英]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)
  • 如果我注释掉 yerr=err,代码运行正常。
  • truth[:,np.newaxis]medianerr的形状是 (3,1); (3,1); 和(2,3)分别。
  • 我将err格式化为 (2,N) 形状,因为那是axes.errorbar文档告诉我要做的
  • 当我转置truth[:, np.newaxis]hpd以匹配err的形状时,它会抛出错误。

最终,我想要三个数据点及其各自的非对称错误栏,但我目前无法获得 ErrorbarContainer object 没有任何错误(是的,这些图目前是空白的......)

truthhpd的大小必须是(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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM