jgieseler / solarmach

Python package of the multi-spacecraft longitudinal configuration plotter Solar-MACH
https://solarmach.readthedocs.io
BSD 3-Clause "New" or "Revised" License
28 stars 6 forks source link

Give the option to show the figure after plot() #36

Open AthKouloumvakos opened 1 year ago

AthKouloumvakos commented 1 year ago

Currently, because plt.show() is called by default in plot() the figure is created there and this prevents the user to add more data in the plots after function returns.

It would be usefull to select plt.show() with a key.

jgieseler commented 1 year ago

We have the boolean option return_plot_object in solarmach.plot(). With that the user can get the matplotlib object and alter it, doesn't this already provide more or less the same functionality? It's for advanced users anyhow, while making it necessary to call plt.show() every time makes it more complicate for all users (including non-advanced ones). :thinking:

AthKouloumvakos commented 1 year ago

Using the example in the Jupyter notebook doing the following in the make plot section:

import numpy as np
import matplotlib.pyplot as plt

fig, ax = sm.plot(plot_spirals=plot_spirals, plot_sun_body_line=plot_sun_body_line, 
                  reference_vsw=reference_vsw, transparent=transparent, numbered_markers=numbered_markers,
                  long_offset=long_offset, return_plot_object=return_plot_object,
                  show_plot=False)
ax.plot(np.deg2rad(90), 1, 's', color='black', label='Arbritary')
plt.show()

will not show the extra point because plt.show() has been called prevously

jgieseler commented 1 year ago

I think the problem is in that case that you call plt.show(). If you use fig.show() instead (or just fig in a Jupyter Notebook), it will show the updated plot: plot But as you can see, it will also update the plotting range and don't update the legend. So the user would still need to adjust some things manually.

And this is independent of whether plt.show() has been called before or not. In fact, I just tried locally replacing

if _isstreamlit():
    import streamlit as st
    st.pyplot(fig)
else:
    plt.show()

if return_plot_object:
    return fig, ax

with

if _isstreamlit():
    import streamlit as st
    st.pyplot(fig)
    return
else:
    if return_plot_object:
        return fig, ax
    else:
        plt.show()
        return

In the latter case, fig, ax = sm.plot(return_plot_object=True) doesn't show the plot, while plt.show() and fig.show() show the same figure as before. So the only thing that would be gained from that change is that the figure is not shown "twice" and that the "usual" plt.show() would work. I don't really see any benefit from that, but also no reason not to introduce that if you think it makes things more clearer.