python Plotly:如何调整坐标轴标签和绘图区域之间的间距?

pbpqsu0x  于 2024-01-05  发布在  Python
关注(0)|答案(5)|浏览(292)

如何在Plotly中调整轴标签和绘图区域之间的空间?我特别感兴趣的是减少轴标签和绘图区域之间的空间。我附上了一个示例截图。


的数据
以下是可复制的代码片段:

  1. import dash
  2. import dash_core_components as dcc
  3. import dash_html_components as html
  4. import plotly.graph_objs as go
  5. import plotly.express as px
  6. import pandas as pd
  7. import numpy as np
  8. external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
  9. app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
  10. df = pd.read_csv("data.txt", sep='\t', parse_dates=["StartTimeRun", "EndTimeRun"])
  11. df = df[df['Status'] != 'NOT_RUN']
  12. df = df[df['Status2'] != 'NOT_RUN']
  13. # assume you have a "long-form" data frame
  14. # see https://plotly.com/python/px-arguments/ for more options
  15. df_PASS = df.loc[df['Status'] == "PASS"]
  16. df_FAIL = df.loc[df['Status'] == "FAIL"]
  17. 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)))
  18. 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)))
  19. fig = {
  20. 'data': [trace1, trace2],
  21. 'layout':
  22. go.Layout(
  23. boxmode='group', margin=dict(l=200, r=150)
  24. )
  25. }
  26. app.layout = html.Div(children=[
  27. html.H1(children='Hello Dash'),
  28. html.Div(children='''
  29. Dash: A web application framework for Python.
  30. '''),
  31. dcc.Graph(
  32. id='example-graph',
  33. figure=fig
  34. )
  35. ])
  36. if __name__ == '__main__':
  37. app.run_server(debug=True)

字符串
下面是一个数据结构(data.txt):

  1. SuiteName Test Status Keyword Status2 Duration(seconds) StartTimeRun EndTimeRun Type FileName
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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


希望这段文字能帮助我们找到答案。

bhmjp9jg

bhmjp9jg1#

我认为最好的方法是做fig.update_yaxes(ticksuffix = " ")技巧:

  1. import plotly.graph_objects as go
  2. labels = ['giraffes', 'orangutans', 'monkeys']
  3. fig = go.Figure(go.Bar(x=[20, 14, 23], y=labels, orientation='h'))
  4. # Adjust for your purposes the width of the blank space in " "
  5. fig.update_yaxes(ticksuffix = " ")
  6. fig.show()

字符串

k2arahey

k2arahey2#

不幸的是,Plotly中有no good way可以做到这一点。Plotly非常适合创建交互式情节,但代价是可调性。
我能想到的唯一的“变通方法”(也不是很好)是将绘图背景设置为与浏览器背景相同的颜色,将gridcolor更改为与浏览器背景不同的颜色,然后添加填充(在我的示例中,我假设浏览器背景是白色)。然而,这将填充x轴和y轴,我找不到解决方法。

  1. import pandas as pd
  2. import plotly.graph_objects as go
  3. df = pd.DataFrame({'ColumnA':[1,2,3,4,5], 'ColumnB':[5,6,7,8,9], 'ColumnC':[6,7,8,9,10]})
  4. trace1 = go.Box(x=df['ColumnA'],orientation='h')
  5. trace2 = go.Box(x=df['ColumnB'],orientation='h')
  6. trace3 = go.Box(x=df['ColumnB'],orientation='h')
  7. fig = go.Figure(data=[trace1, trace2, trace3])
  8. fig.update_layout(
  9. boxmode='group',
  10. boxgap=0.25,
  11. boxgroupgap=0.25,
  12. height=500,
  13. paper_bgcolor='rgba(0,0,0,0)',
  14. plot_bgcolor='rgba(0,0,0,0)',
  15. xaxis=dict(gridcolor='lightgrey'),
  16. margin=dict(l=600, r=150, b=10, pad=100)
  17. )
  18. fig.show()

字符串


的数据

展开查看全部
nwnhqdif

nwnhqdif3#

如果刻度线可以调整,并且取决于您的数据,您可以向刻度线添加空格。空格可以使用列表解析来添加,如下所示:

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

字符串
下面是一个使用刻度线和空格的例子:

  1. import plotly.graph_objects as go
  2. spaces = ' '* 25
  3. labels = ['giraffes', 'orangutans', 'monkeys']
  4. newLabels = [label+spaces for label in labels]
  5. fig = go.Figure(go.Bar(
  6. x=[20, 14, 23],
  7. y=newLabels,
  8. orientation='h'))
  9. fig.show()


的数据

展开查看全部
ahy6op9u

ahy6op9u4#

实际上,你可以这样做:

  1. fig.update_yaxes(
  2. title_standoff = 50
  3. )

字符串

qgzx9mmu

qgzx9mmu5#

另一种选择是使用带有指定ticklen的外部记号,并将它们绘制为纸张颜色(甚至可能以某种方式透明?):

  1. import plotly.graph_objects as go
  2. labels = ['giraffes', 'orangutans', 'monkeys']
  3. fig = go.Figure(go.Bar(x=[20, 14, 23], y=labels, orientation='h'))
  4. fig.update_yaxes(ticks="outside",
  5. tickcolor="white", # adjust color of the tick
  6. ticklen=50 # adjust length of the tick = distance from axis
  7. )
  8. fig.show()

字符串


的数据
也适用于fig.update_xaxes

展开查看全部

相关问题