How do I create diagonal reference lines using Plotly express?

I have created a scatter plot in Plotly using a log, log x and y axis. (shown below)

In Matlab my professor was able to add diagonal lines in place of the x and y gridlines as shown below.

Is there a way to achieve this in plotly? I have tried using fig.add_trace() and fig.add_line(), but I cant seem to get either to work.

The bones(without all of the graph customization) for my code is as follows:

fig = px.scatter(
df,
x=‘g’,
y=[‘δLP’,‘Fδ0TLS’],
color=‘Reference’,
symbol=‘SC’,
symbol_sequence= symbols,
labels={‘SC’ : ‘Superconductor’, ‘Reference’ : ‘Source’, ‘Dep.’ : ‘Depostion’},
title=‘Putting Resonator Loss In Context’,
hover_name=‘Reference’,
hover_data=[‘Dep.’,‘SC’,‘Substrate’],
log_x=True, log_y=True)

Hi @dylanblevins49 welcome to the forums.

Are these lines connecting values on the axis? You could just add go.Scatter() traces.

Yes, The values for the Matlab code are plotted at coordinates to obtain the diagonal lines. the code my professor used in Matlab is:

xtest = 0.1:0.1:40;
plot(xtest,(1./xtest).*0.25E-6 , ‘-’, ‘Color’ ,[0.9 0.9 0.9] , ‘LineWidth’,1.5)
plot(xtest,(1./xtest).*0.5E-6,‘-’,‘Color’,[0.9 0.9 0.9],‘LineWidth’,1.5)
plot(xtest,(1./xtest).*1E-6,‘-’,‘Color’,[0.9 0.9 0.9],‘LineWidth’,1.5)
plot(xtest,(1./xtest).*2E-6,‘-’,‘Color’,[0.9 0.9 0.9],‘LineWidth’,1.5)
plot(xtest,(1./xtest).*4E-6,‘-’,‘Color’,[0.9 0.9 0.9],‘LineWidth’,1.5)
plot(xtest,(1./xtest).*8E-6,‘-’,‘Color’,[0.9 0.9 0.9],‘LineWidth’,1.5)
plot(xtest,(1./xtest).*16E-6,‘-’,‘Color’,[0.9 0.9 0.9],‘LineWidth’,1.5)
plot(xtest,(1./xtest).*32E-6,‘-’,‘Color’,[0.9 0.9 0.9],‘LineWidth’,1.5)
plot(xtest,(1./xtest).*64E-6,‘-’,‘Color’,[0.9 0.9 0.9],‘LineWidth’,1.5)
plot(xtest,(1./xtest).*128E-6,‘-’,‘Color’,[0.9 0.9 0.9],‘LineWidth’,1.5)
plot(xtest,(1./xtest).*256E-6,‘-’,‘Color’,[0.9 0.9 0.9],‘LineWidth’,1.5)

I’m not sure how the code would look in Plotly to obtain the same x and y coordinates.
Additionally I am using the plotly.express method, would I need to reformat my code if I were to use the go.Scatter() method instead of the px.scatter?

A quick example:

import plotly.graph_objs as go
import plotly.express as px
import numpy as np

fig = go.Figure(
    data=go.Scatter(
        x=np.random.randint(2, 400, 50), 
        y=np.random.randint(2, 400, 50), 
        mode='markers'
    )
)

x = np.arange(100, 500, 100)
y = np.arange(100, 500, 100)

for xx, yy in zip(x,y):
    fig.add_scatter(
        x=[0, xx], 
        y=[yy, 0], 
        mode='lines', 
        line_color='gray', 
        showlegend=False
    )
    
fig.update_xaxes(range=[0, 400])

1 Like

No, this should not be necessary. The loop where I add the scatter traces should work for your existing figure.

1 Like

Thank you for your help. I was able to make it work with a few adjustments using the values I was presented with:

x = np.arange(0.1, 50, 10)
constants = [0.25E-6, 0.5E-6, 1.0E-6, 2.0E-6, 4.0E-6, 8.0E-6, 16.0E-6, 32.0E-6,
64.0E-6, 128.0E-6, 256.0E-6, 512.0E-6, 1014.0E-6, 2018.0E-6]
for constant in constants:
y = (1/x) * constant
fig.add_scatter(x=x,y=y,mode=‘lines’,showlegend=False,line_color=‘#cccccc’)