spedas / pyspedas

Python-based Space Physics Environment Data Analysis Software
https://pyspedas.readthedocs.io/
MIT License
143 stars 58 forks source link

Problems setting colors, possibly other per-trace options on pseudovariables #800

Closed jameswilburlewis closed 3 months ago

jameswilburlewis commented 3 months ago

Consider the case of creating a pseudovariable from two subvariables, one with 3 traces, and the other with 1 trace. We want to override the subvariables' trace colors options on the pseudovariable, say to ['k','r','g','b']

    def test_pseudovar_line_options(self):
        themis.fgm(probe='c')
        store_data('test_pseudo_colors',data=['thc_fge_dsl','thc_fge_btotal'])
        # Set the color option on the pseudovariable (4 traces total, so 4 colors)
        options('test_pseudo_colors','color',['k','r','g','b'])
        tplot('test_pseudo_colors') # should plot without "incorrect number of line colors" messages

The logic for handling colors, markers, and possibly other per-trace attributes in pseudovariables doesn't work if those options are set on the pseudovariable. It seems to assume a single trace per sub-variable. When tplot encounters a pseudovariable, it iterates over the subvariables, calling tplot recursively on each one with and passing an index "pseudo_plot_num" of the subvariable within the parent pseudovariable.

            for pseudo_idx, var in enumerate(pseudo_vars):
                tplot(var, return_plot_objects=return_plot_objects,
                        xsize=xsize, ysize=ysize, save_png=save_png,
                        save_eps=save_eps, save_svg=save_svg, save_pdf=save_pdf,
                        fig=fig, axis=this_axis, display=False,
                        pseudo_plot_num=pseudo_idx, second_axis_size=0.1,
                        pseudo_yaxis_options=yaxis_options, pseudo_zaxis_options=zaxis_options,
                        pseudo_line_options=line_opts, pseudo_extra_options=plot_extras,
                        pseudo_right_axis=pseudo_right_axis)

If you try to set colors on the pseudovariable, it tries to use pseudo_plot_num to look up the color to use. But if there are multiple traces on the current variable, it won't get plotted because num_lines != len(colors). Or the current variable could be a spectrogram, with no traces at all.

Better solution: if the parent pseudovariable has options set for colors, markers, etc., first iterate over the subvariables to see how many traces each one has. Make sure the total number of traces matches the number of colors, markers, etc. if they are set on the pseudovariable. Keep a running count of traces processed so far, and increment by the trace count of that variable after it is processed, and pass that value to tplot in pseudo_plot_num. When a line plot subvariable is processed, use colors[ pseudo_plot_num : pseudo_plot_num + num_lines] rather than colors[pseudo_plot_num] to ensure that we get the right colors on the right traces. And similarly for markers, line styles, etc.

jameswilburlewis commented 3 months ago

This is fixed now, along with several other pseudovariable plotting issues