Search code examples
pythonplotlyplotly-dashmap-projections

Is there a way to plot a skymap with plotly?


I want to insert in a dashboard a skymap with Aitoff projection (like this) and on top a scatterplot with the data taken from a DB. The final results should be like this. Is it possible to do such thing with plotly? I tried to create the figure with matplotlib and then import it but it fails. I saw there is px.the scatter_geo, but is not exactly what I need.

This is a tentative to implement the figure creation with matplotlib.

@app.callback(
    Output('aitoff', 'children'),
    Input('my-table', 'data')
)
def add_row(data):
    df_grpah = pd.DataFrame(data)
    ra = coord.Angle(df_grpah["ra"], unit=u.hour) # create an Angle object
    ra = ra.wrap_at(180*u.degree)
    dec = coord.Angle(df_grpah['dec'],unit=u.deg)
    print("Ra: ", ra.degree)
    print("Dec: ", dec.degree)
    fig = plt.figure(figsize=(8,6))
    ax = fig.add_subplot(111, projection="aitoff")
    ax.scatter(ra.radian, dec.radian)
    ax.grid(True)
    figure = go.Figure(fig)
    return [
        html.Div(children=[dcc.Graph(figure=figure, className="card")])
    ]

ra and dec are an array of values:

Ra:  [  73.6305     56.868975  -81.49779   -28.375005  -52.832145  145.95
 -175.296    -151.65      101.85     -154.5      -175.821   ]
Dec:  [-72.5771    26.622     23.530393  38.21      89.348426  14.7899
 -28.042     30.27      25.28      17.76      -4.3204  ]

The app works, but the plot is not showed and I get the error: ValueError: Invalid element(s) received for the 'data' property


Solution

  • You can draw a white map by specifying 'aitoff' for the projection on the scatter plot of the map. I drew the map with the data you provided replaced with latitude and longitude values. The latitude and longitude scales were displayed in the expected output, but there does not seem to be a function to display the latitude and longitude scales. The rationale is given by the developer in SO. It is not known if it is currently possible to display ticks.

    import plotly.graph_objects as go
    
    Ra=[73.6305,56.868975,-81.49779,-28.375005,-52.832145,145.95,-175.296,-151.65,101.85,-154.5,-175.821]
    Dec=[-72.5771,26.622,23.530393,38.21,89.348426,14.7899,-28.042,30.27,25.28,17.76,-4.3204]
    fig = go.Figure()
    
    fig.add_trace(go.Scattergeo(
        mode="markers",
        lon = Ra,
        lat = Dec,
        marker = {'size': 8,
                  'color':'blue',
                 })
                 )
    
    fig.update_layout(geo=dict(
                        showland=False,
                        showcountries=False,
                        showocean=False,
                        countrywidth=0.5,
                        #coastlinecolor='rgb(255, 255, 255)',
                        landcolor='rgb(255, 255, 255)',
                        lakecolor='rgb(255, 255, 255)',
                        oceancolor='rgb(255, 255, 255)',
                        projection=dict(
                            type='aitoff',
                        ),
                        lonaxis=dict(
                            showgrid=True,
                            dtick=15,
                            gridcolor='rgb(102, 102, 102)',
                            gridwidth=0.5
                        ),
                        lataxis=dict(
                            showgrid=True,
                            dtick=15,
                            gridcolor='rgb(102, 102, 102)',
                            gridwidth=0.5
                        )
                    )
    )
    fig.show()
    #plot(fig)
    

    enter image description here