python - plotly :如何防止标题与 plotly 重叠?

标签 python python-3.x plot plotly

这是代码:

import plotly.graph_objects as go

fig = go.Figure()
fig.add_trace(go.Bar(
    name='Group 1',
    x=['Var 1', 'Var 2', 'Var 3'], y=[3, 6, 4],
    error_y=dict(type='data', array=[1, 0.5, 1.5]),
    width=0.15
))
fig.add_trace(go.Bar(
    name='Group 2',
    x=['Var 1', 'Var 2', 'Var 3'], y=[4, 7, 3],
    error_y=dict(type='data', array=[0.5, 1, 2]),
    width=0.15
))
fig.update_layout(barmode='group',
                  title=dict(
                            text = "Some <br> title <br> here",
                            x = 0.5,
                            y = 0.95,
                            xanchor =  'center',
                            yanchor = 'top',
                            #pad = dict(
                            #            t = 0
                            #           ),
                            font = dict(
                                        #family='Courier New, monospace',
                                        size = 40,
                                        #color='#000000'
                                        )
                            ))


fig.show()
这是输出:
enter image description here
问题:如何在不减小绘图标题字体大小的情况下在此处正确对齐标题​​?
当然,这只是示例,实际 plotly 会有所不同。问题的主要思想是获得适当的大字体标题的解决方案。

最佳答案

边距调整以及 y=0.95 的小幅调整应该做的伎俩:
enter image description here
完整代码:

import plotly.graph_objects as go

fig = go.Figure()
fig.add_trace(go.Bar(
    name='Group 1',
    x=['Var 1', 'Var 2', 'Var 3'], y=[3, 6, 4],
    error_y=dict(type='data', array=[1, 0.5, 1.5]),
    width=0.15
))
fig.add_trace(go.Bar(
    name='Group 2',
    x=['Var 1', 'Var 2', 'Var 3'], y=[4, 7, 3],
    error_y=dict(type='data', array=[0.5, 1, 2]),
    width=0.15
))
fig.update_layout(barmode='group',
                  title=dict(
                            text = "Some <br> title <br> here",
                            x = 0.5,
                            y = 0.90,
                            xanchor =  'center',
                            yanchor = 'top',
                            #pad = dict(
                            #            t = 0
                            #           ),
                            font = dict(
                                        #family='Courier New, monospace',
                                        size = 40,
                                        #color='#000000'
                                        )
                            ))

fig.update_layout(margin=dict(l=50, r=50, t=200, b=50),paper_bgcolor="LightSteelBlue")
fig.show()

关于python - plotly :如何防止标题与 plotly 重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63162434/

相关文章:

python - XML 阅读器似乎忽略了标签层次结构

python - 损坏的 pip3 和 easy_install3 : DistributionNotFound

python - 为什么python3k的pyserial返回字节而python2k返回字符串?

plot - 将 Axis 刻度设置为对数时,倍频程图会中断

python - 当键与共享值关联时对字典键进行分组

python - django 如何获取 manytomany 字段的计数

python - sqlite3 : create function regexp with python

python - 为什么一百万个随机数的平均值不在范围的中间?

r - Shiny 的 tabsetPanel 不在多个选项卡中显示图

python - 用 Pandas 分组数据的堆积条形图