legend() fails and best way to overplot multi-symbols and lines

22 views
Skip to first unread message

cosmicbruce

unread,
Oct 22, 2025, 7:03:51 PMOct 22
to idl-pvwave
Hello All: 

Summary: My (only slightly) more complex plot()s usually break legend. Why is this, is there a workaround? Is there some "resetting" I have to do to avoid this? 

Specific example: 
The following code causes failure of the legend , repeatably for me.  I only get the first symbol in the legend, the rest are not present. This is a problem I have in general with legend, though sometimes it works with e.g. 3 very simple plots or scatterplots. ***I would be super-grateful if someone could tell me why and suggest fix.***  
Numerous other legend fail examples on request.

(It might be helpful to some people to note the zero sample width hack for points, not lines. This was surprisingly hard to track down on the net.)

================================================
pro plot251013c2

; simple fake data example
tt=10^(2+findgen(50)/50*3.)
imag=14.+findgen(50)/50.*6.
sig=0.05*abs(imag)
; NOW NEW METHOD WITH FUNCTIONS NOT PLOT PROS...------------------

yerror=replicate(0.05,n_elements(imag))
p1 = SCATTERPLOT(tt, imag,/xlog,name='NUT',$
      XRANGE=[10^1.75,10^5.25], yrange=[22.,13.],xsty=1,ysty=1 )
p1.symbol='o'

p1.font_size=2.7
p1.sym_filled=1
p1.sym_size=.6
p1.font_size=12
p1.title="251013C LC"
p1.xtitle='Log t-t0 (s)'
p1.ytitle="mags in given color"

; unam GCN 42242
 r_UNAM= [18.50]
   p2=scatterplot([8.65*3600.],r_UNAM,/overplot,name='UNAM')
 p2.symbol='Plus'
 p2.sym_color='red'
 p2.sym_size=2.5
 p2.sym_thick=2

; 42276 OHP GCN 42276

 r_OHP=[21.29]
  p3=scatterplot([28.71*3600.],r_OHP,/overplot,name='OHP')
 p3.symbol='Square'
 p3.sym_color='blue'
 p3.sym_size=1.5
 p3.sym_filled=1

 
lgnd = LEGEND(TARGET=[p1, p2,p3], SAMPLE_WIDTH=0,$
       HORIZONTAL_SPACING=.1,/AUTO_TEXT_COLOR)
 
 stop

end

================================================

What's going on here is that I am trying to finally enter the 21st century and use the plot function (I'm an old school plot,x,y  *procedure* guy), and learn what the heck this "object" programming thing is all about. I think that the function (plot(),errorplot(),scatterplot()) plots look a bit better than the procedures (plot,x,y; errorplot,x,y,sigma), and the legend thing would be spiffy if it worked. 

In the greater scheme of things, I plot lots of points and sometimes the lines fitting a function over them. I would never, ever plot points with lines between them, as plot() prefers, extremely rare in my field (but you can line_style='' your way out of this). Other than that, I cannot figure out why there would be a scatterplot and a plot function. So, in the end, the plots look a little better and the possibility to nudge the locations of labels and change the symbols and fonts interactively could be a *tremendous* timesaver if I can figure these out. 

-WOuld be ever so appreciative of a fix.
-Advice on general approaches to IDL plotting in the 21st century will be read with great interest and gratitude.

Thanks for helping!

cosmicbruce

unread,
Oct 23, 2025, 2:28:10 AMOct 23
to idl-pvwave
I found a solution!

(1) legend has a bug:  if you plot only one value, legend will NOT include this in the legend it makes. 
THIS CODE FAILS: 
p1=plot(multiple points, name='station1')
p2=plot([x1],[y1],name='station2',/overplot)
leg=legend(target=[p1,p2])
... the legend has only station1. 

(2) Genious hack provided by GH:   
p1=plot(multiple points, name='station1')
p2=plot(x1*[1., 1.+1.e-9],y1*[1.,1.+1.e-9],name='station2',/overplot)
leg=legend(target=[p1,p2])
This works!   both station1 and station2 get an entry.

GH was a genius to figure this out. 

(3)  I will include the "points only" hack here: for completeness (this exists on the web, but it is hard to find, let's promulgate it): 

 Legend wants to plot TWO symbols connected by a length of line.  In my field pretty much no one would ever use that plotting style (redundant, makes plot too busy, plots a linear extrapolation, not real data or fit). 

So, the hack is: 
 lgnd = LEGEND(TARGET=[p1, p2], SAMPLE_WIDTH=0,$
       HORIZONTAL_SPACING=.1,/AUTO_TEXT_COLOR)

with sample width=0, and horizontal_spacing=0.1, you get no line, and just one symbol next to the name. 

I hope this helps everyone. Thanks again to GH. 
Reply all
Reply to author
Forward
0 new messages