Colorbar is overlapping with Contour Plot in Plotly

﹥>﹥吖頭↗ 提交于 2021-01-29 06:50:55

问题


I am getting margin on the left and right of the first subplot (please look at the image I have included at the end of the post) of the contour which I do no understand. Moreover, I can not position correctly the colorbar in the second plot. How can I fix the code?

def bern(theta, z, N):
    
    """Bernoulli likelihood with N trials and z successes."""
    
    return np.clip(theta**z * (1-theta)**(N-z), 0, 1)

def bern2(theta1, theta2, z1, z2, N1, N2):
    
    """Bernoulli likelihood with N trials and z successes."""
    
    return bern(theta1, z1, N1) * bern(theta2, z2, N2)

def make_thetas(xmin, xmax, n):
    
    xs = np.linspace(xmin, xmax, n)
    
    widths =(xs[1:] - xs[:-1])/2.0
    
    thetas = xs[:-1]+ widths
    
    return thetas

thetas1 = make_thetas(0, 1, 101)

thetas2 = make_thetas(0, 1, 101)

X, Y = np.meshgrid(thetas1, thetas2)

a = 2

b = 3

z1 = 11

N1 = 14

z2 = 7

N2 = 14

prior = stats.beta(a, b).pdf(X) * stats.beta(a, b).pdf(Y)

likelihood = bern2(X, Y, z1, z2, N1, N2)


posterior = stats.beta(a + z1, b + N1 - z1).pdf(X) * stats.beta(a + z2, b + N2 - z2).pdf(Y)

layout = go.Layout(yaxis=dict(scaleanchor="x", scaleratio=1))
fig = make_subplots(rows=1, cols=3, subplot_titles=('Prior', 'Likelihood', 'Posterior'))
cbarlocs = [.28, .63, 1]
fig.add_trace(
    go.Contour(
        z= prior,
        colorbar=dict(
            #title='Color bar title', # title here
            #titleside='right',
            x = cbarlocs[0],
            titlefont=dict(
                size=14,
                family='Arial, sans-serif') )
        ), 1, 1),
fig.update_layout( layout   ) ,
fig.add_trace(
    go.Contour(
        z= likelihood,
        colorbar=dict(
            #title='Color bar title', # title here
            #titleside='right',
             x = cbarlocs[1],
            titlefont=dict(
                size=14,
                family='Arial, sans-serif'))
        ), 1, 2),
fig.update_layout( layout   ) ,
fig.add_trace(
    go.Contour(
        z= posterior,
        colorbar=dict(
            #title='Color bar title', # title here
            #titleside='right',
             x = cbarlocs[2],
            titlefont=dict(
                size=14,
                family='Arial, sans-serif') )
        ), 1, 3),
fig.update_layout( layout   )  
fig.show()

来源:https://stackoverflow.com/questions/64896471/colorbar-is-overlapping-with-contour-plot-in-plotly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!