简体   繁体   English

Plotly:如何调整轴标签与plot区域之间的间距?

[英]Plotly: How to adjust the space between axis labels and plot area?

How can I adjust space between axis labels and plot area in Plotly?如何调整轴标签和 Plotly 中 plot 区域之间的空间? In particularly I am interesting in decreasing space between axis labels and plot area.我特别感兴趣的是减少轴标签和 plot 区域之间的空间。 I've attached a screenshot with an example.我附上了带有示例的屏幕截图。

在此处输入图像描述

Here is reproducible code snippet:这是可重现的代码片段:

import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import plotly.express as px
import pandas as pd
import numpy as np

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)


df = pd.read_csv("data.txt", sep='\t', parse_dates=["StartTimeRun", "EndTimeRun"])
df = df[df['Status'] != 'NOT_RUN']
df = df[df['Status2'] != 'NOT_RUN']

# assume you have a "long-form" data frame
# see https://plotly.com/python/px-arguments/ for more options
df_PASS = df.loc[df['Status'] == "PASS"]
df_FAIL = df.loc[df['Status'] == "FAIL"]
trace1 = go.Box(x=df_PASS["Duration(seconds)"], y=df_PASS["Keyword"], name="PASS", orientation='h', marker=dict(color='rgb(158,202,225)', line=dict(color='rgb(8,48,107)', width=1.5)))
trace2 = go.Box(x=df_FAIL["Duration(seconds)"], y=df_FAIL["Keyword"], name="FAIL", orientation='h', marker=dict(color='#fd9668', line=dict(color='rgb(8,48,107)', width=1.5)))
fig = {
    'data': [trace1, trace2],
    'layout':
    go.Layout(
        boxmode='group',  margin=dict(l=200, r=150)
    )
}

app.layout = html.Div(children=[
    html.H1(children='Hello Dash'),

    html.Div(children='''
        Dash: A web application framework for Python.
    '''),

    dcc.Graph(
        id='example-graph',
        figure=fig
    )
])

if __name__ == '__main__':
    app.run_server(debug=True)

And here is the dataframe (data.txt):这是 dataframe (data.txt):

SuiteName   Test    Status  Keyword Status2 Duration(seconds)   StartTimeRun    EndTimeRun  Type    FileName
0SmokeTestDD    Validate the use case for Single UE PASS    BuiltIn.Run Keywords    FAIL    12.619  20200809 06:45:18.515   20200809 06:45:31.134   setup   output-20200809-064513.xml
0SmokeTestDD    Validate the use case for Single UE PASS    Validate the work flow  PASS    34.56   20200809 06:45:31.135   20200809 06:49:25.695   kw  output-20200809-064513.xml
0SmokeTestDD    Validate the use case for Single UE PASS    BuiltIn.Run Keywords    PASS    15.344  20200809 06:49:25.695   20200809 06:49:41.039   teardown    output-20200809-064513.xml
Validate the use case for Single UE Validate the work flow  PASS    Login To    FAIL    8.502   20200809 06:45:31.135   20200809 06:45:39.637   kw  output-20200809-064513.xml
Validate the use case for Single UE Validate the work flow  PASS    Select Technology   PASS    1.243   20200809 06:45:39.637   20200809 06:45:55.880   kw  output-20200809-064513.xml
Validate the use case for Single UE Validate the work flow  PASS    Select Menu PASS    7.147   20200809 06:45:55.880   20200809 06:46:03.027   kw  output-20200809-064513.xml
Validate the use case for Single UE Validate the work flow  PASS    BuiltIn.Log FAIL    0.001   20200809 06:46:03.027   20200809 06:46:03.028   kw  output-20200809-064513.xml
Validate the use case for Single UE Validate the work flow  PASS    BuiltIn.Sleep   PASS    5.0 20200809 06:46:03.028   20200809 06:46:08.028   kw  output-20200809-064513.xml

Hope this snippet will help to find answer.希望这段代码有助于找到答案。

Unfortunately there is no good way to do this in Plotly.不幸的是,在 Plotly 中没有很好的方法来做到这一点。 Plotly is great for creating interactive plots, but at the cost of tunability. Plotly 非常适合创建交互式绘图,但代价是可调性。

The only "workaround" I can think of (and it's not great) is to make the plot background the same color as the browser background, change the gridcolor to something different from your browser background, then add padding.我能想到的唯一“解决方法”(这不是很好)是使 plot 背景与浏览器背景颜色相同,将网格颜色更改为与浏览器背景不同的颜色,然后添加填充。 (In my example, I assume the browser background is white). (在我的示例中,我假设浏览器背景为白色)。 However, this will pad both the x- and y-axes, and I can't find a way around this.但是,这会同时填充 x 轴和 y 轴,我找不到解决方法。

import pandas as pd
import plotly.graph_objects as go

df = pd.DataFrame({'ColumnA':[1,2,3,4,5], 'ColumnB':[5,6,7,8,9], 'ColumnC':[6,7,8,9,10]})
trace1 = go.Box(x=df['ColumnA'],orientation='h')
trace2 = go.Box(x=df['ColumnB'],orientation='h')
trace3 = go.Box(x=df['ColumnB'],orientation='h')

fig = go.Figure(data=[trace1, trace2, trace3])
fig.update_layout(
    boxmode='group', 
    boxgap=0.25,
    boxgroupgap=0.25,
    height=500, 
    paper_bgcolor='rgba(0,0,0,0)', 
    plot_bgcolor='rgba(0,0,0,0)',
    xaxis=dict(gridcolor='lightgrey'),
    margin=dict(l=600, r=150, b=10, pad=100)
)
fig.show()

在此处输入图像描述

If tick marks are available to adjust and depending on your data, you can add spaces to tick marks.如果刻度线可用于调整并且根据您的数据,您可以在刻度线中添加空格。 The spaces can be added using a list comprehension, like this:可以使用列表推导添加空格,如下所示:

spaces = ' '* 25
labels = ['giraffes', 'orangutans', 'monkeys']
newLabels = [label+spaces for label in labels]

Here is an example using tickmarks and spaces:这是一个使用刻度线和空格的示例:

import plotly.graph_objects as go
spaces = ' '* 25
labels = ['giraffes', 'orangutans', 'monkeys']

newLabels = [label+spaces for label in labels]

fig = go.Figure(go.Bar(
            x=[20, 14, 23],
            y=newLabels,
            orientation='h'))

fig.show()

在此处输入图像描述

I believe the best way is to do fig.update_yaxes(ticksuffix = " ") trick:我相信最好的方法是做fig.update_yaxes(ticksuffix = " ")技巧:

import plotly.graph_objects as go

labels = ['giraffes', 'orangutans', 'monkeys']

fig = go.Figure(go.Bar(x=[20, 14, 23], y=labels, orientation='h'))

# Adjust for your purposes the width of the blank space in "  "
fig.update_yaxes(ticksuffix = "  ")

fig.show()

Actually you can just do:其实你可以这样做:

fig.update_yaxes(
        title_standoff = 50
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM