绘图纸格式不统一

Posted

技术标签:

【中文标题】绘图纸格式不统一【英文标题】:Plotly paper formatting is not uniform 【发布时间】:2021-04-15 07:23:06 【问题描述】:

我正在使用 Plotly 中的注释系统来注释图形的轴。我正在使用 plotly 中的“纸张”设置指定坐标。但是,我注意到纸张坐标在图形上并不总是一致的,至少在箭头关闭时是这样。有谁知道为什么会发生这种情况?这里我提供代码给大家看:

def test():
    
    fig = go.Figure()

    ylabels = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
    annotations = []
    for i, label in enumerate(ylabels):
        print(label/100)
        annotations.append(
            dict(
                yref = 'paper',
                y = label/100, x = 0,
                text = ylabels[1],
                showarrow = False,
                yshift = 0,
            )
        )
    fig.update_layout(
        annotations = annotations,
        yaxis = dict(
            showticklabels = False,
            tickvals = ylabels,
            range = [0, 100]
        ),
        xaxis = dict(
            showticklabels = False
        )
    )
    fig.show()

Non-uniform spacing when arrows are off

出于某种原因,这与关闭箭头有关。当箭头打开时,它们在 x 轴上均匀分布。

Uniform spacing when arrows are on

【问题讨论】:

【参考方案1】:

问题似乎可以通过将yanchor 设置为topmiddlebottom 来解决,请参阅Plotly documentation。

import plotly.graph_objects as go

fig = go.Figure()

ylabels = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

annotations = []
for i, label in enumerate(ylabels):
    annotations.append(
        dict(
            yref='paper',
            y=label / 100,
            x=0,
            text=ylabels[1],
            showarrow=False,
            yshift=0,
            yanchor='middle' # default is 'auto'
        )
    )
    
fig.update_layout(
    annotations=annotations,
    yaxis=dict(
        showticklabels=False,
        tickvals=ylabels,
        range=[0, 100]
    ),
    xaxis=dict(
        showticklabels=False
    )
)

fig.show()

【讨论】:

以上是关于绘图纸格式不统一的主要内容,如果未能解决你的问题,请参考以下文章

springboot统一返回json数据格式并配置系统异常拦截

接口返回值response统一标准格式

pycharm格式怎么统一?

Delphi Xe 中如何把日期格式统一处理,玩转 TDatetime

plt格式打开字的颜色不统一怎么办

SpringBoot返回统一的JSON标准格式