Python 打印自定义按钮溢出
我将遵循这一生动的教程: 具体来说,考虑代码:Python 打印自定义按钮溢出,python,plot,plotly,plotly-dash,plotly-python,Python,Plot,Plotly,Plotly Dash,Plotly Python,我将遵循这一生动的教程: 具体来说,考虑代码: import plotly.graph_objects as go import pandas as pd # load dataset df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/volcano.csv") # create figure fig = go.Figure() # Add surface trace
import plotly.graph_objects as go
import pandas as pd
# load dataset
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/volcano.csv")
# create figure
fig = go.Figure()
# Add surface trace
fig.add_trace(go.Surface(z=df.values.tolist(), colorscale="Viridis"))
# Update plot sizing
fig.update_layout(
width=800,
height=900,
autosize=False,
margin=dict(t=0, b=0, l=0, r=0),
template="plotly_white",
)
# Update 3D scene options
fig.update_scenes(
aspectratio=dict(x=1, y=1, z=0.7),
aspectmode="manual"
)
# Add dropdown
fig.update_layout(
updatemenus=[
dict(
type = "buttons",
direction = "left",
buttons=list([
dict(
args=["type", "surface"],
label="3D Surface",
method="restyle"
),
dict(
args=["type", "heatmap"],
label="Heatmap",
method="restyle"
)
]),
pad={"r": 10, "t": 10},
showactive=True,
x=0.11,
xanchor="left",
y=1.1,
yanchor="top"
),
]
)
# Add annotation
fig.update_layout(
annotations=[
dict(text="Trace type:", showarrow=False,
x=0, y=1.08, yref="paper", align="left")
]
)
fig.show()
现在再添加15个按钮,只需添加
dict(
args=["type", "surface"],
label="3D Surface",
method="restyle"
)
在
updatemenus
的按钮中(基本上再添加15次相同的按钮)。如果您现在运行它,您将看到右侧的按钮开始溢出。我怎样才能解决这个问题?对我来说,显示成行的按钮是一个很好的解决方案,但是显示一个没有溢出的按钮也会起作用。这不是最通用的解决方案,但您可以将相同按钮的单独字典(带有y坐标偏移)传递给图.update\u layout
的updatemenus
参数,定义按钮的位置
我不得不通过反复试验来确定一行中有多少个按钮,以及什么样的间距看起来最好。理想情况下,可以通过某种方式以传递给参数x
和y
的相同单位访问按钮的大小
import plotly.graph_objects as go
import pandas as pd
# load dataset
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/volcano.csv")
# create figure
fig = go.Figure()
# Add surface trace
fig.add_trace(go.Surface(z=df.values.tolist(), colorscale="Viridis"))
# Update plot sizing
fig.update_layout(
width=800,
height=900,
autosize=False,
margin=dict(t=0, b=0, l=0, r=0),
template="plotly_white",
)
# Update 3D scene options
fig.update_scenes(
aspectratio=dict(x=1, y=1, z=0.7),
aspectmode="manual"
)
# Add dropdown
fig.update_layout(
updatemenus=[
dict(
type = "buttons",
direction = "left",
buttons=list([
dict(
args=["type", "surface"],
label="3D Surface",
method="restyle"
),
dict(
args=["type", "heatmap"],
label="Heatmap",
method="restyle"
)
]*4),
pad={"r": 10, "t": 10},
showactive=True,
x=0.11,
xanchor="left",
y=1.1,
yanchor="top"
),
## offset the y coordinate in these buttons
dict(
type = "buttons",
direction = "left",
buttons=list([
dict(
args=["type", "surface"],
label="3D Surface",
method="restyle"
),
dict(
args=["type", "heatmap"],
label="Heatmap",
method="restyle"
)
]*4),
pad={"r": 10, "t": 10},
showactive=True,
x=0.11,
xanchor="left",
y=1.15,
yanchor="top"
)
]
)
# Add annotation
fig.update_layout(
annotations=[
dict(text="Trace type:", showarrow=False,
x=0, y=1.1, yref="paper", align="left")
]
)
fig.show()