# How to rotate a plotly fig

I have a plot that I want to rotate by 45 degree given .
How will I handle my x and y axis values

Outcome as follows . Any suggestion will work

@yogi_dhiman Over the last few years I have answered this question about three times. Please search the forum and if you don’t find it or if in the meantime another plotly user doesn’t answer, I will leave the solution tomorrow.

I search it before posting my question , can you please give the link directing to the relevant solution post , a better practice to make forum more helpful.

@yogi_dhiman
The previous examples have been related to heatmap and contour plot, but meanwhile the scipy function involved in rotating a figure changed its name, as well as np.float (now it’s simply `float`). https://chart-studio.plotly.com/~empet/15760

To rotate a `go.Figure`, fig, it is converted by the function `plotly_fig2array()` to a RGB-array, i.e. an array of shape (m, n, 4).
The RGB-array is passed to the scipy function, `scipy.ndimage.rotate()` to get the rotated figure, represented also by a RBG-array, which is displayed by `px.imshow()`. But by default the `px.imshow()` adds a hovertemplarte to this image which leads to displaying pixel position and its color in the rotated figure. However we are not interested in such information. That’s why we create an image with hoverinfo=“none”.
Unfortunately the transformed plotly figures by a scipy geometrical map (not only rotation) are no more interactive.

``````import plotly.graph_objects as go
import plotly.express as px
import numpy as np
import scipy.ndimage
import io
from PIL import Image
def plotly_fig2array(fig):
#convert a Plotly fig to  a RGB-array
fig_bytes = fig.to_image(format="png")
buf = io.BytesIO(fig_bytes)
img = Image.open(buf)
return np.asarray(img)

fig = go.Figure(go.Scatter(x=3+5* np.random.rand(20),
y=3+5*np.random.rand(20),
mode="markers",
marker=dict(color=4+2*np.random.rand(20),
showscale=True, colorbar_thickness=23,
symbol="diamond", size=14),
hovertemplate="x: %{x}<br>y: %{y}<br>val: %{marker.color:.2f}"))
fig.update_layout(width=500, height=500)
fig.show()

farr = plotly_fig2array(fig)
rotated_farr = scipy.ndimage.rotate(farr, -45, reshape=True, cval=np.nan)
print(farr.shape, rotated_farr.shape)
fign=px.imshow(rotated_farr)  #fign has a hovertemplate and on hover are displyed image data
#fign.update_traces(hovertemplate="<extra></extra>")#has an unexpected effect

#redefine the same image with hoverinfo= none, because hovertemplate and hoverinfo are not allowed simultaneously
myfign = go.Figure(go.Image(source=fign.data[0].source, hoverinfo="none"))
myfign.update_layout(width=rotated_farr.shape[0]+2, height=rotated_farr.shape[1]+2, template="plotly_white",
xaxis_visible=False, yaxis_visible=False, margin=dict(t=2, r=2, b=2, l=2)
)
``````

2 Likes