簡體   English   中英

我在matplotlib(python)中有一個帶有誤差條的條形圖,但我希望誤差條位於該條的中間。 我該怎么做呢? 謝謝

[英]I have a bar chart with error bars in matplotlib (python), but i want the error bars in the middle of the bar. how do i do this? Thanks

#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()

注意, bar()也接受yerr=參數

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

這將自動將錯誤欄放置在每個欄的中心。 無需先繪制條形,然后使用errorbar()在頂部繪制錯誤條。

問題是,對於誤差線和條形圖,您使用相同的x數據(在您的情況下為cos)。 一種簡單的解決方案是為誤差線的x數據增加1/2線寬度,換句話說:

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

其中0.025是條形寬度的一半。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM