如何在Plotly中扩展背景大小

huangapple go评论66阅读模式
英文:

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)

如何在Plotly中扩展背景大小


<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 = {&#39;staticPlot&#39;: True}
    fig = go.Figure()
    fig = px.scatter(terms, x=&#39;pr_auc_x1&#39;, y=&#39;pr_auc_x2&#39;, color=&#39;hue2&#39;, title=&#39;test&#39;)
    fig.add_shape( type=&quot;line&quot;, x0=0, y0=0, x1=1,y1=1, line=dict(color=&quot;Grey&quot;,width=1) )
    
    
    fig.update_layout(
            annotations=[
                go.layout.Annotation(
                    text=&quot;..&lt;br&gt;&quot;.join([i for i in terms[terms.hue == 1].legend]),
                    align=&#39;left&#39;,
                    showarrow=False,
                    xref=&#39;paper&#39;,
                    yref=&#39;paper&#39;,
                    x=1.22,
                    y=0.93,
                    bordercolor=&#39;black&#39;,
                    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="..&lt;br&gt;".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)

如何在Plotly中扩展背景大小

英文:

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 = {&#39;staticPlot&#39;: True}
fig = go.Figure()
fig = px.scatter(df, x=&quot;sepal_width&quot;, y=&quot;sepal_length&quot;, color=&quot;species&quot;, title=&#39;test&#39;)
fig.add_shape( type=&quot;line&quot;, x0=0, y0=0, x1=df.sepal_width.max(), y1=df.sepal_length.max(), line=dict(color=&quot;Grey&quot;,width=1) )

fig.update_layout(
    height=500,
    xaxis=dict(domain = [0, 0.75],  anchor = &#39;y1&#39;),
    legend=dict(
        yanchor=&quot;top&quot;,
        y=0.99,
        xanchor=&quot;right&quot;,
        x=0.87),
        annotations=[
            go.layout.Annotation(
                text=&quot;..&lt;br&gt;&quot;.join([i for i in df.species[:20]]),
                align=&#39;left&#39;,
                showarrow=False,
                xref=&#39;paper&#39;,
                yref=&#39;paper&#39;,
                x=0.95,
                y=0.99,
                bordercolor=&#39;black&#39;,
                borderwidth=1,
            )
        ]
)

fig.show(config=config)

如何在Plotly中扩展背景大小

huangapple
  • 本文由 发表于 2023年5月25日 00:29:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76325661.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定