Hi, am looking for a fast way to make simple 3d plots. I am currently using matplotlib to plot the 3d trajectory of an ODE system. I use a slider to update plotting parameters. This is horribly slow. So, slow it makes me wonder why I would even use streamlit. Is there a better way to render 3d plots in streamlit? Thanks for the help.
Hi @Joe314, I got some prior knowledge while doing some scientific stuff that needed 3d plotting (a wavelet transform with a crazy amount of data), if I remember correctly matplotlib isn’t the best way if you need a fast plotting method, as far as I know the fastest way (for me after doing some testing) to render is using Plotly, you can check it out on the streamlit documentation and plotly documentation especially the scatter 3d part because it seems to fit your current use case with the trajectory and ODE system.
But the word slow in your question can actually be cause by the calculation process of the ODE (I remember long time ago, I made an error while calculating an ODE system that made it super slow), so maybe you can try to time the calculation excluding the plotting, but if it is already fast, try switching from matplotlib to Plotly
For me I suspect the former due to the fact that you use slider to update the plotting parameter, maybe if you haven’t used st.form, it can help reduce the amount of reloading cause by you changing the slider (I presume that there are more than one slider)
Good luck with your project Joe, cheers
PS: Plotly work really well with a lot of point, you can check it out why here
Thanks, I am looking into using plotly.
I do have a questions now. The ODE is solved for all plotting parameters first. That is at least what I think. So, as you change the plotting parameters then you would only be selected subset of the already generated data. Is this how streamlit works. Or, when parameter are changed with the slider is streamlit rerunning the entire code?
The latter is the correct one, when you change the slider the program rerun itself, it is the core functionality of streamliy, so you can keep this in mind that when you do anything to a streamlit input such as st.button, st.slider, etc it’ll cause a rerun of the whole script. This don’t apply to the st.form I quoted from my previous reply, st.form help when you had a lot of input but you don’t want it to rerun the program before you finish tweaking the input.
I assume that this ODE calculation had a fixed input, if so, you should see the st.cache documentation, where you essentially keep the finished calculation from the first run as a cache so you don’t do the whole calculation again and again while you are tweaking the plotting parameter. I personally would wrap the slider inside a st.form so when the slider is accidentally change, it won’t terun the program and save a little time from you trying to match the previous position the slider is in (I assume this is a number slider, but if it isn’t the number one, this might come in handy in your next project)
If you don’t have a fixed input, st.cache still work if your input don’t change, essentially it’ll rerun your calculation only when the input change. So if you haven’t already, make your calculation from start to finish in a function, so that it’ll be easier for you to made it as a cached function
I hope my explanation can help, cheers!
Hi, this is very helpful, thank you.
I am still looking into st.form. I have rewritten my code and cached the function that generates the data. This helps out a lot. Now, the plot I am making has several components. The most complicated component does not change, so I think I would like to cache that part of the plot, if possible. Is that possible?
Hello @Joe314, sorry I forgot to press the Reply Button (silly me)
About the component, what is it that you are talking about? is it the graphic? for the graphic part unfortunately as far as I know, there are no way to cache it, the only thing you can cache is the data used for plotting, maybe you can give more information regarding your component you are saying, maybe there are some optimization that you can do
I implemented the method found here, How to render chart faster. Does this help you? I am not sure if this has speed up the plot rendering.