【问题标题】:Axis labels in subplots by Plotly appear correctly only to the left subplotPlotly 子图中的轴标签仅在左侧子图中正确显示
【发布时间】:2021-03-01 23:46:17
【问题描述】:

我正在尝试命名使用 Plotly 创建的子图中的轴,如下所示。我在第一个(左)子图中成功,但我不能影响其他子图中的轴标签。我不清楚为什么。如何修复代码?

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)

fig = make_subplots(rows=1, cols=3, specs= [[{'is_3d': True}, {'is_3d': True}, {'is_3d': True}]], subplot_titles=('Prior', 'Likelihood', 'Posterior'))

fig.add_trace(go.Surface(z= prior,  showscale= True), 1, 1)




fig.add_trace(go.Surface(z= likelihood,  showscale= True), 1, 2)




fig.add_trace(go.Surface(z= posterior,  showscale= True),1,3, False)

fig.update_layout(title='Prior - Likelihood - Posterior', autosize= True, scene = dict(
                    xaxis_title='theta1',
                    yaxis_title='theta2',
                    zaxis_title='Probability Density'),
                  width= 1300, height=600,
                  margin=dict(l=65, r=50, b=65, t=90))



fig.show()

代码的输出是这样的:

【问题讨论】:

    标签: python-3.x plotly subplot axis-labels


    【解决方案1】:

    如果您将底部的 update_layout 位替换为以下代码,它将起作用:

    fig.update_layout(title='Prior - Likelihood - Posterior', autosize=True,
        scene1=dict(
        xaxis_title='theta1',
        yaxis_title='theta2',
        zaxis_title='Probability Density'),
        scene2=dict(
        xaxis_title='theta1',
        yaxis_title='theta2',
        zaxis_title='Probability Density'),
        scene3=dict(
        xaxis_title='theta1',
        yaxis_title='theta2',
        zaxis_title='Probability Density'),
        width=1300, height=600,
        margin=dict(l=65, r=50, b=65, t=90))
    

    阅读官方文档here,它确实说您可以指定 layout.scene、layout.scene2 等等,仅通过查看他们提供的示例并不是最明显的,但我们开始吧。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-16
      • 2021-08-02
      • 2022-11-21
      • 2014-07-04
      • 2017-05-10
      • 1970-01-01
      • 1970-01-01
      • 2022-10-04
      相关资源
      最近更新 更多