Plotly: Bagaimana cara mengatur jarak antara label sumbu dan area plot?

Aug 18 2020

Bagaimana cara menyesuaikan spasi antara label sumbu dan area plot di Plotly? Secara khusus saya tertarik untuk mengurangi spasi antara label sumbu dan area plot. Saya telah melampirkan tangkapan layar dengan sebuah contoh.

Berikut potongan kode yang dapat direproduksi:

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)

Dan inilah 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

Semoga potongan ini akan membantu menemukan jawaban.

Jawaban

7 DerekO Aug 18 2020 at 06:20

Sayangnya tidak ada cara yang baik untuk melakukan ini di Plotly. Plotly sangat bagus untuk membuat plot interaktif, tetapi dengan mengorbankan kesesuaian.

Satu-satunya "solusi" yang dapat saya pikirkan (dan itu tidak bagus) adalah membuat latar belakang plot dengan warna yang sama dengan latar belakang browser, ubah warna kisi menjadi sesuatu yang berbeda dari latar belakang browser Anda, lalu tambahkan padding. (Dalam contoh saya, saya menganggap latar belakang browser berwarna putih). Namun, ini akan menekan sumbu x dan y, dan saya tidak dapat menemukan jalan keluarnya.

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()

3 rex Aug 18 2020 at 16:09

Jika tanda centang tersedia untuk disesuaikan dan bergantung pada data Anda, Anda dapat menambahkan spasi untuk tanda centang. Spasi dapat ditambahkan menggunakan pemahaman daftar, seperti ini:

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

Berikut ini contoh penggunaan tanda centang dan spasi:

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()

GitHunter0 Mar 22 2021 at 01:34

Saya percaya cara terbaik adalah melakukan fig.update_yaxes(ticksuffix = " ")trik:

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()