英文:
how to extend background size in plotly
问题
I have added text-box beside to scatter plot.
Since figure size is defined, I can not see text-box properly.
I would like to extend background and put text-box beside to scatter plot.
import plotly.graph_objects as go
import plotly.express as px
config = {'staticPlot': True}
fig = go.Figure()
fig = px.scatter(terms, x='pr_auc_x1', y='pr_auc_x2', color='hue2', title='test')
fig.add_shape(type="line", x0=0, y0=0, x1=1, y1=1, line=dict(color="Grey", width=1))
fig.update_layout(
annotations=[
go.layout.Annotation(
text="..<br>".join([i for i in terms[terms.hue == 1].legend]),
align='left',
showarrow=False,
xref='paper',
yref='paper',
x=1.22,
y=0.93,
bordercolor='black',
borderwidth=1,
)
]
)
fig.show(config=config)
<details>
<summary>英文:</summary>
I have added text-box beside to scatter plot.
Since figure size is defined, I can not see text-box proparly.
I would like to extend background and put text-box beside to scatter plot.
import plotly.graph_objects as go
import plotly.express as px
config = {'staticPlot': True}
fig = go.Figure()
fig = px.scatter(terms, x='pr_auc_x1', y='pr_auc_x2', color='hue2', title='test')
fig.add_shape( type="line", x0=0, y0=0, x1=1,y1=1, line=dict(color="Grey",width=1) )
fig.update_layout(
annotations=[
go.layout.Annotation(
text="..<br>".join([i for i in terms[terms.hue == 1].legend]),
align='left',
showarrow=False,
xref='paper',
yref='paper',
x=1.22,
y=0.93,
bordercolor='black',
borderwidth=1,
)
]
)
fig.show(config=config)
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/zfFdE.png
</details>
# 答案1
**得分**: 1
在这种情况下,相反地,图表区域可以缩小,并且可以放置图例和文本框。只要 x 轴的位置保持不变,每个位置都设置了,增加图表大小将产生相同的效果。您可以通过设置一个可以设置图表区域的域,将图例放置在其左侧,然后将文本框进一步放置在左侧来解决这个问题。我没有类似的示例数据,所以我使用了通用的鸢尾花数据来适应您的示例。
```python
import plotly.graph_objects as go
import plotly.express as px
config = {'staticPlot': True}
fig = go.Figure()
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species", title='test')
fig.add_shape(type="line", x0=0, y0=0, x1=df.sepal_width.max(), y1=df.sepal_length.max(), line=dict(color="Grey", width=1))
fig.update_layout(
height=500,
xaxis=dict(domain=[0, 0.75], anchor='y1'),
legend=dict(
yanchor="top",
y=0.99,
xanchor="right",
x=0.87),
annotations=[
go.layout.Annotation(
text="..<br>".join([i for i in df.species[:20]]),
align='left',
showarrow=False,
xref='paper',
yref='paper',
x=0.95,
y=0.99,
bordercolor='black',
borderwidth=1,
)
]
)
fig.show(config=config)
英文:
In this case, conversely, the graph area can be narrowed and a legend and text box can be placed. Increasing the graph size will do the same thing as long as the position of the x-axis, where each position is set, remains the same. You can solve this problem by setting a domain where you can set the graph area, placing a legend to the left of it, and then placing a text box further to the left. I don't have similar sample data, so I used general iris data to fit your example.
import plotly.graph_objects as go
import plotly.express as px
config = {'staticPlot': True}
fig = go.Figure()
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species", title='test')
fig.add_shape( type="line", x0=0, y0=0, x1=df.sepal_width.max(), y1=df.sepal_length.max(), line=dict(color="Grey",width=1) )
fig.update_layout(
height=500,
xaxis=dict(domain = [0, 0.75], anchor = 'y1'),
legend=dict(
yanchor="top",
y=0.99,
xanchor="right",
x=0.87),
annotations=[
go.layout.Annotation(
text="..<br>".join([i for i in df.species[:20]]),
align='left',
showarrow=False,
xref='paper',
yref='paper',
x=0.95,
y=0.99,
bordercolor='black',
borderwidth=1,
)
]
)
fig.show(config=config)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论