我在MatPultLIB(Python)中有一个带错误条的条形图,但是我想要在条中间的错误条。我该怎么做?谢谢

我在MatPultLIB(Python)中有一个带错误条的条形图,但是我想要在条中间的错误条。我该怎么做?谢谢,python,matplotlib,bar-chart,Python,Matplotlib,Bar Chart,问题是,在您的案例中,错误条和条形图使用了相同的x数据cos。一个简单的解决方案是为错误条的x数据添加1/2条宽度,换句话说: #Figure 1, right at the bottom of the code, needs the error bars. import scipy as sp import numpy as np import pylab as pl import matplotlib as mpl import numpy.random as nr import

问题是,在您的案例中,错误条和条形图使用了相同的x数据cos。一个简单的解决方案是为错误条的x数据添加1/2条宽度,换句话说:

#Figure 1, right at the bottom of the code, needs the error bars.

import scipy as sp

import numpy as np

import pylab as pl

import matplotlib as mpl

import numpy.random as nr

import matplotlib.pyplot as plt

M = 91.0

G = 2.5

RS = 0.14

JS = -0.033

S = np.arange(20,140,0.1)

def sigs(com):

    for i in com:

        yield  ((4.0*np.pi)/3.0)*((1/i**2) + (((i**2)*RS + JS*((i**2)-M**2))/(((i**2)-M**2)**2 + (M*G)**2)))


x = list(sigs(S))


RA = 0.0027

JA = 0.81

def siga(com):

    for i in com:

        yield np.pi*((i**2)*RA + JA*((i**2) - M**2))/((((i**2) - M**2)**2) + (M*G)**2) 


a = list(siga(S))


N = []

for m in x:

    N.append(1000000*8*m/3)


cos = np.arange(-0.95, 0.90, 0.05)

sin = np.arange(-0.90, 0.95, 0.05) 

M = []

for (i, j) in zip(cos,sin):

    M.append((1000000*j*(0.094 + 0.0313*j*j + 0.000679*j))-(1000000*i*(0.094 + 0.0313*i*i + 0.000679*i)))


s = np.random.poisson(M)

z = []

for t in s:

    z.append(t**0.5)

plt.figure(4)

pl.bar(cos, s, width = 0.05)

pl.xlabel('cos${\Theta}$')

pl.ylabel('Number of muons produced within the cos${\Theta}$ interval')
yerr = z

plt.errorbar(cos, s, yerr=yerr, fmt = 'o')

pl.show()
其中0.025是钢筋宽度的一半。

注意:

这将自动将错误条放置在每个条的中心。不需要先画条,然后用errorbar在顶部画errorbar

plt.errorbar(cos + 0.025 , s, yerr=yerr, fmt = 'o')
pl.bar(cos, s, width=0.05, yerr=yerr)