Black Lives Matter. Please consider donating to Black Girls Code today.

How to rotate text in markers+text mod

Tried to use Plotly for making XRD analysis plot, and I’ve got the little problem… It’s about rotation text in “markers+text” mode in go.Scatter. Here is some code:

import plotly
import chart_studio.plotly
import plotly.graph_objects as go
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import plotly.express as px
import math
from plotly.subplots import make_subplots

AuBN_US = np.genfromtxt('somedatfile.dat')
AuBN_noUS = np.genfromtxt('otherdatafile.dat')
dataBN = pd.read_csv('somecsvfile.csv', sep=';')
dataAu = pd.read_csv('othercsvfile.csv', sep=';')    
fig = make_subplots(rows=2, cols=1, y_title='Intensity', x_title="2\u03F4")

fig.add_trace(go.Scatter(x=AuBN_US[:,0], y=AuBN_US[:,1],
                             mode='lines',
                             name='AuBN_US'),
                             row=1, col=1)
fig.add_trace(go.Scatter(x=AuBN_noUS[:,0], y=AuBN_noUS[:,1],
                             mode='lines',
                             name='AuBN_noUS'),
                             row=2, col=1)
fig.add_trace(go.Scatter(x = dataBN['Angle'], y = dataBN['Int']+100,
                             mode='markers+text',
                             text=dataBN['hkl'],
                             textposition="top center",
                             name='BN',
                             orientation='v',
                             marker=dict(size=10, symbol="triangle-down")))

fig.add_trace(go.Scatter(x= dataAu['Angle'], y=dataAu['Int']+100,
                             name='Au',
                             mode='markers+text',
                             text=dataAu['hkl'],
                             textposition="top center",
                             orientation='v',
                             marker=dict(size=10, symbol="square")))

fig.update_layout(template="plotly_white", font=dict(family="Courier New, 
                                                     monospace",
                                                     size=14,
                                                     color="black"))
fig.show() 

Result:

Is it possible to rotate all indexes (100…) in a simple way? I have already tried “textangle” but always got errors.

Hi @Ilia, welcome to the forum! Great to see some physics / materials science applications here :-).

I’m not sure you can rotate the text markers, but you can easily do this with annotations (you would need to define a loop with your annotations). See the annotations tutorial, and there is a textangle attribute of go.layout.Annotation. This tutorial from @empet can also help.

Sorry, I am nooby in Python. My researching field is “Material Science”, so I don’t understand how to make “easily” loop with annotations. If you can help me, it will be great, in don’t, well I will do it in Photoshop :smiley:,
Thank you for answer)

Here is an example

import plotly.graph_objects as go
import numpy as np
t = np.linspace(0, 4*np.pi, 50)
t2 = np.pi * np.arange(5)
fig = go.Figure(go.Scatter(x=t, y=np.sin(t), mode='lines'))
fig.add_trace(go.Scatter(x=t2, y=np.sin(t2), mode='markers'))
fig.update_layout(annotations=[
            go.layout.Annotation(x=point,
            y=np.sin(point),
            xref="x",
            yref="y",
            text="dict Text",
            align='center',
            showarrow=False,
            yanchor='bottom',
            textangle=90) for point in t2])
fig.show()

2 Likes

It does not work for me in right way, but today I made my first loop and it works well))). (Text should also change)

for i, m, l in zip(dataBN['Angle'], dataBN['Int'], dataBN['hkl']):
    fig.add_annotation(go.layout.Annotation(
    x = i,
    y = m,
    text = "BN"+ l,
    align='center',
    showarrow=False,
    yanchor='bottom',
    textangle=-90))