为了账号安全,请及时绑定邮箱和手机立即绑定

如何在情节中更改注释方向?

如何在情节中更改注释方向?

心有法竹 2021-12-17 16:18:37
说我有下图:import numpy as npimport plotly.graph_objs as goz=np.random.randint(1000, 11000, size=20)trace=dict(type='scatter',          x=3+np.random.rand(20),          y=-2+3*np.random.rand(20),          mode='markers',          marker=dict(color= z,                       colorscale='RdBu', size=14, colorbar=dict(thickness=20)))axis_style=dict(zeroline=False, showline=True, mirror=True)layout=dict(width=550, height=500,            xaxis=axis_style,            yaxis=axis_style,            hovermode='closest',           )fig=go.FigureWidget(data=[trace], layout=layout)fig现在说我希望颜色条有一个标题。由于 plotly 目前没有直接的方法来做到这一点,如果我理解正确,我正在annotations按照此处所示执行此操作:layout.update(    annotations=[dict(          x=1.12,          y=1.05,          align="right",          valign="top",          text='Colorbar Title',          showarrow=False,          xref="paper",          yref="paper",          xanchor="center",          yanchor="top"        )    ]) 如我们所见,出现了颜色栏标题:fig=go.FigureWidget(data=[trace], layout=layout)fig但是,现在说我想将颜色栏标题沿颜色栏放在一边,如下所示:我该怎么做呢?
查看完整描述

1 回答

?
qq_花开花谢_0

TA贡献1835条经验 获得超7个赞

参数textangle为您完成。来自 plotly 文档的示例。textangle=-90如何设置旋转注释。


代码:


# import necessaries libraries

import numpy as np

import plotly.offline as py

import plotly.graph_objs as go


z = np.random.randint(1000, 11000, size=20)

# Create a trace

trace = dict(type='scatter',

             x=3+np.random.rand(20),

             y=-2+3*np.random.rand(20),

             mode='markers',

             marker=dict(color=z, colorscale='RdBu',

                         size=14, colorbar=dict(thickness=20)))

# Define axis_style

axis_style = dict(zeroline=False, showline=True, mirror=True)

# Specify layout style

layout = dict(width=550, height=500,

              xaxis=axis_style,

              yaxis=axis_style,

              hovermode='closest',

              )

# Update layout with annotation

layout.update(

    annotations=[dict(

          # Don't specify y position,because yanchor="middle" do it for you

          x=1.22,

          align="right",

          valign="top",

          text='Colorbar Title',

          showarrow=False,

          xref="paper",

          yref="paper",

          xanchor="right",

          yanchor="middle",

          # Parameter textangle allow you to rotate annotation how you want

          textangle=-90

        )

    ]

)

# Create FigureWidget

fig = go.FigureWidget(data=[trace], layout=layout)

# Plot fig

py.plot(fig)

输出:

//img1.sycdn.imooc.com//61bc481b00015e7805140437.jpg

查看完整回答
反对 回复 2021-12-17
  • 1 回答
  • 0 关注
  • 271 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号