Streamlit and galpy - how to display plot or animation?

Hello, the galpy package computes and plots orbits of stars in our galaxy for scientific research.
I am trying render the resulting plots or animations in Streamlit but without success.
The galpy webpage: https://www.galpy.org/

Simple code to implement (this computes and plots our Suns orbit):

from galpy.orbit import Orbit
from galpy.potential import MWPotential2014
from astropy import units as u
import numpy
ts= numpy.linspace(0.,5.,2001)*u.Gyr
o= Orbit() # default = the sun
o.integrate(ts,MWPotential2014)
o.plot(), o.animate()

The o.plot() makes a matplotlib graph, while o.animate() makes a plotly animation.
How can I display these graphs or animation in Streamilt.
I have tried st.pyplot(o.plot()) but it doesn’t work.
It give the error:

AttributeError: 'list' object has no attribute 'savefig'

Full error:

File "/Users/adriancioc/opt/anaconda3/lib/python3.9/site-packages/streamlit/scriptrunner/script_runner.py", line 557, in _run_script
    exec(code, module.__dict__)
File "/Users/adriancioc/Desktop/ETCwebApp/webApp.py", line 578, in <module>
    st.pyplot(fig = orb)
File "/Users/adriancioc/opt/anaconda3/lib/python3.9/site-packages/streamlit/elements/pyplot.py", line 92, in pyplot
    marshall(
File "/Users/adriancioc/opt/anaconda3/lib/python3.9/site-packages/streamlit/elements/pyplot.py", line 132, in marshall
    fig.savefig(image, **kwargs)

I found an easy way. Basically I extract the orbit computed values, and then you can plot them with motplotlib or plotly without using the galpy o.plot() wrapper

R = o.R(ts)
Z = o.z(ts)
X = o.x(ts)
Y = o.y(ts)
plt.plot(X,Y, color = 'red')
plt.show()

Then you can use streamlit with ease

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.