How to change annotation orientation in plotly?

Question:

Say I have the following figure:

import numpy as np
import plotly.graph_objs as go

z=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

enter image description here

Now say I want the colorbar to have a title. Since plotly does not currently have a direct way to do that, if I understand correctly, I am doing this through annotations as shown here:

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"
        )
    ]
) 

As we can see, the colorbar title appears:

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

enter image description here

However, now say I want to place the colorbar title sideways, along the colorbar, like so:

enter image description here

How do I do this?

Asked By: Kristada673

||

Answers:

Parameter textangle do it for you. Example from plotly docs. Setting textangle=-90 rotate annotation how you want.

Code:

# 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)

Output:

Plot

Answered By: Dmitriy Kisil

For anyone who may have found this question now, there is (now?) a very easy way of adding a title to a colorbar, and to make it oriented sideways along the colorbar using the colorbar title property.

In this case, we could just update trace like so:

# 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, 
                                       title=dict(text="Colorbar title", orient="right"))))

Documentation here: https://plotly.com/python/reference/scatter/#scatter-marker-colorbar

Answered By: Hestre999