Adding Legends to Coyote Graphics Windows [message #86840] |
Fri, 06 December 2013 12:52  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Folks,
I have nothing against the NASA routine AL_Legend for adding legends to
graphics displays. I've used it for years, and so has nearly everyone I
know. But, I've been feeling guilty about not having a "native" Coyote
Graphics solution, and I have long wanted something a bit simpler to
understand and use. A new program, cgLegend, may fit the bill. It
doesn't have as many features as AL_Legend, but it does have the "fit
and finish" of a Coyote Library program, which I like. You can find
documentation here:
http://www.idlcoyote.com/idldoc/cg/cglegend.html
And you can find examples of using cgLegend in this updated web page:
http://www.idlcoyote.com/cg_tips/al_legend.php
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
|
|
|
Re: Adding Legends to Coyote Graphics Windows [message #86858 is a reply to message #86840] |
Mon, 09 December 2013 02:15   |
atmospheric physics
Messages: 121 Registered: June 2010
|
Senior Member |
|
|
Hello,
How to control the size of the text & symbols in the legend box?
Thanks in advance.
On Friday, December 6, 2013 9:52:26 PM UTC+1, David Fanning wrote:
> Folks,
>
>
>
> I have nothing against the NASA routine AL_Legend for adding legends to
>
> graphics displays. I've used it for years, and so has nearly everyone I
>
> know. But, I've been feeling guilty about not having a "native" Coyote
>
> Graphics solution, and I have long wanted something a bit simpler to
>
> understand and use. A new program, cgLegend, may fit the bill. It
>
> doesn't have as many features as AL_Legend, but it does have the "fit
>
> and finish" of a Coyote Library program, which I like. You can find
>
> documentation here:
>
>
>
> http://www.idlcoyote.com/idldoc/cg/cglegend.html
>
>
>
> And you can find examples of using cgLegend in this updated web page:
>
>
>
> http://www.idlcoyote.com/cg_tips/al_legend.php
>
>
>
> Cheers,
>
>
>
> David
>
> --
>
> David Fanning, Ph.D.
>
> Fanning Software Consulting, Inc.
>
> Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
>
> Sepore ma de ni thue. ("Perhaps thou speakest truth.")
|
|
|
|
|
|
|
Re: Adding Legends to Coyote Graphics Windows [message #86864 is a reply to message #86863] |
Mon, 09 December 2013 06:07   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Mats Löfdahl writes:
> I have found that with al_legend, I almost always use the top/bottom and left/right keywords for placement. Any chance you will implement them in cglegend?
Yes, there is a "chance" I will do something like this. I usually wait
for people to ask for more complicated things, but I usually wait more
than two minutes. ;-)
> Specifying the coordinates of the upper left hand corner of the legend box seems tricky if you want the legend to go in any other corner of the plot than upper left, given that you may want to add or remove items of various lengths. Or is there a smart way if thinking about this that I just don't see?
Here is the problem. It is impossible to know what size the legend is
going to be until it is drawn. AL_Legend and cgLegend both solve the
problem in the same way: they draw the legend twice, the second time
overwriting the first. I had hoped to be able to do this a different
way, and I wrote code to do so, but I haven't been able to make it work
correctly in the PostScript device yet.
Of course, as I am writing this I am thinking of another way it might be
implemented. So, yes, there is a "chance". But, why would you choose
cgLegend over AL_Legend if they both did the same thing?
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
|
|
|
Re: Adding Legends to Coyote Graphics Windows [message #86868 is a reply to message #86864] |
Mon, 09 December 2013 07:10   |
Mats Löfdahl
Messages: 263 Registered: January 2012
|
Senior Member |
|
|
Den måndagen den 9:e december 2013 kl. 15:07:58 UTC+1 skrev David Fanning:
> Mats Löfdahl writes:
>
>> I have found that with al_legend, I almost always use the top/bottom and left/right keywords for placement. Any chance you will implement them in cglegend?
>
> Yes, there is a "chance" I will do something like this. I usually wait
> for people to ask for more complicated things, but I usually wait more
> than two minutes. ;-)
Not complicated enough? OK, what about automatically figuring out where there is empty space in the plot and put the legend there? :o)
>> Specifying the coordinates of the upper left hand corner of the legend box seems tricky if you want the legend to go in any other corner of the plot than upper left, given that you may want to add or remove items of various lengths. Or is there a smart way if thinking about this that I just don't see?
>
> Here is the problem. It is impossible to know what size the legend is
> going to be until it is drawn. AL_Legend and cgLegend both solve the
> problem in the same way: they draw the legend twice, the second time
> overwriting the first. I had hoped to be able to do this a different
> way, and I wrote code to do so, but I haven't been able to make it work
> correctly in the PostScript device yet.
I realize that something like that has to go on behind the scene. What I meant was that I didn't know of a smart way to figure this out as a user.
> Of course, as I am writing this I am thinking of another way it might be
> implemented. So, yes, there is a "chance". But, why would you choose
> cgLegend over AL_Legend if they both did the same thing?
They are solving the same problem, so there's bound to be some overlap. The placement keywords are overlap that I would like to see but that doesn't mean I don't think you will make cgLegend smarter than AL_Legend in many other respects.
|
|
|
Re: Adding Legends to Coyote Graphics Windows [message #86878 is a reply to message #86863] |
Mon, 09 December 2013 14:10   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Mats Löfdahl writes:
> I have found that with al_legend, I almost always use the top/bottom and left/right keywords for placement. Any chance you will implement them in cglegend?
I have added functionality to cgLegend that will make this slightly more
flexible than AL_Legend, I think. Here is how it works.
There is an ALIGNMENT keyword that can be used to change the meaning of
the LOCATION keyword. (The LOCATION keyword is called POSITION in
AL_Legend, but I use POSITION to mean a particular four-element array in
Coyote Graphics, so I changed this keyword name.) Basically, with the
ALIGNMENT keyword you can specify whether the LOCATION is any of the
four corners of the legend "box", or the middle of any of the four sides
of the legend "box". You have eight choices.
This allows you to put the legend anywhere you like in the plot window
(including outside the plot, if you want to). You can center the legend
box both horizontally and vertically. Or, you can left, right, top, and
bottom align it. You simply specify a point location, and tell me, with
the ALIGNMENT keyword, what part of the box should be aligned with that
point.
You will need two files:
http://www.idlcoyote.com/programs/cglegend.pro
http://www.idlcoyote.com/programs/cglegenditem.pro
You can find documentation on the program and on the ALIGNMENT keyword
here:
http://www.idlcoyote.com/idldoc/cg/cglegend.html
Hours spent updating cgLegend for better legend positioning: 8 hours.
Income from newsgroup requested activities for December: $60.
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
|
|
|
Re: Adding Legends to Coyote Graphics Windows [message #86880 is a reply to message #86878] |
Mon, 09 December 2013 14:22   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
David Fanning writes:
> This allows you to put the legend anywhere you like in the plot window
> (including outside the plot, if you want to). You can center the legend
> box both horizontally and vertically. Or, you can left, right, top, and
> bottom align it. You simply specify a point location, and tell me, with
> the ALIGNMENT keyword, what part of the box should be aligned with that
> point.
Oh, I also fixed it so that I only have to draw things on the plot once.
More or less. I had to use some smoke and mirrors to do this, but it
works pretty good, I think. In PostScript I just write off the page
where you can't see it. :-)
This also works in cgWindows in the usual way.
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
|
|
|
Re: Adding Legends to Coyote Graphics Windows [message #86884 is a reply to message #86880] |
Tue, 10 December 2013 02:19   |
atmospheric physics
Messages: 121 Registered: June 2010
|
Senior Member |
|
|
Thanks for your effort and update.
On Monday, December 9, 2013 11:22:19 PM UTC+1, David Fanning wrote:
> David Fanning writes:
>
>
>
>> This allows you to put the legend anywhere you like in the plot window
>
>> (including outside the plot, if you want to). You can center the legend
>
>> box both horizontally and vertically. Or, you can left, right, top, and
>
>> bottom align it. You simply specify a point location, and tell me, with
>
>> the ALIGNMENT keyword, what part of the box should be aligned with that
>
>> point.
>
>
>
> Oh, I also fixed it so that I only have to draw things on the plot once.
>
> More or less. I had to use some smoke and mirrors to do this, but it
>
> works pretty good, I think. In PostScript I just write off the page
>
> where you can't see it. :-)
>
>
>
> This also works in cgWindows in the usual way.
>
>
>
> Cheers,
>
>
>
> David
>
>
>
>
>
> --
>
> David Fanning, Ph.D.
>
> Fanning Software Consulting, Inc.
>
> Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
>
> Sepore ma de ni thue. ("Perhaps thou speakest truth.")
|
|
|
Re: Adding Legends to Coyote Graphics Windows [message #87536 is a reply to message #86880] |
Fri, 14 February 2014 07:09   |
elisadelgadomena
Messages: 6 Registered: February 2014
|
Junior Member |
|
|
Hello David
First of all, thank for all your effort, it's being very helpful for me.
I wanted to do a plot with a colorbar, legend and several data sets and I was searching in your available examples to do that.
Finally I got it and it's perfect but I have a small problem. When saving it in a ps or eps the first appearance is correct (open with gv for example) but when I include it in a latex to produce a ps or pdf the titles of the legend are appearing in their normal place but also in the upper left part of the plot, ones over the others, but outside, getting mixed with other plots or text that appear in the pdf.
Have this happened to you? Since I'm a newbie in coyote libraries maybe I'm doing something wrong but I cannot find the error. I have something like this:
set_plot,'ps'
device,filename='plot.eps',/color,/encapsulated
cgPlot, mass6,log_Li6, /NoData, XTitle='Mass', YTitle='log Li',xrange=[0.7,1.75],yrange=[-0.8,3.5]
cgplotS,mass6,log_Li6,psym=8,color=age6_colors
cgplotS,mass7,log_Li7,psym=symcat(17),color=age7_colors
cgplotS,mass8,log_Li8,psym=symcat(18),color=age8_colors
cgColorbar, /Vertical, Position=[0.9, 0.1, 0.95, 0.9], Bottom=1, NColors=255, $
Minor=0, YTicklen=1, Range=[0,12], $
/Right, Title='Age (Gyr)', Format='(I2)',/fit
cglegend,titles=['planets harps','comparison harps','planets others'],psyms=[8,6,17],/box,length=0,location=[0.7,0.28],ch arsize=1.2
device,/close
set_plot,'x'
I changed the order of the legend (before the first cgplotS) and also I added the keyword alignment to the legend, but nothing changed...
I also tried to change from eps to ps but again obtained the same result :(
thanks
|
|
|
Re: Adding Legends to Coyote Graphics Windows [message #87537 is a reply to message #87536] |
Fri, 14 February 2014 07:36   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
elisadelgadomena@gmail.com writes:
> First of all, thank for all your effort, it's being very helpful for me.
> I wanted to do a plot with a colorbar, legend and several data sets and I was searching in your available examples to do that.
> Finally I got it and it's perfect but I have a small problem. When saving it in a ps or eps the first appearance is correct (open with gv for example) but when I include it in a latex to produce a ps or pdf the titles of the legend are appearing in their normal place but also in the upper left part of the plot, ones over the others, but outside, getting mixed with other plots or text that appear in the pdf.
> Have this happened to you?
No, it has never happened to me, but it is not totally surprising to me,
either. :-)
The problem with a legend is that to create one and draw a box around
it, or to add a background color, or to place it somewhere in the
graphics window, you have to know how big it is. In particular, you have
to know the length of your text strings. Unfortunately, with direct
graphics, the only way to discover the length of your text string is to
draw it somewhere. In other words, you can only find out *after* the
fact.
So, there are a lot of smoke and mirror tricks that have be used. One of
my tricks is to use a pixmap to do the drawing. That works for graphics
windows, but not for PostScript files. In PostScript files, I tend to
draw the text off the page and in the background color, hoping no one
will notice. Obviously, I haven't been totally successful. :-(
I honestly don't know how to solve this problem. I'm not sure the
problem *can* be solved. I'll look into it. But I don't have great
expectations for success. Sorry.
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
|
|
|
Re: Adding Legends to Coyote Graphics Windows [message #87538 is a reply to message #87537] |
Fri, 14 February 2014 07:45   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
David Fanning writes:
>
> elisadelgadomena@gmail.com writes:
>
>> First of all, thank for all your effort, it's being very helpful for me.
>> I wanted to do a plot with a colorbar, legend and several data sets and I was searching in your available examples to do that.
>> Finally I got it and it's perfect but I have a small problem. When saving it in a ps or eps the first appearance is correct (open with gv for example) but when I include it in a latex to produce a ps or pdf the titles of the legend are appearing in their normal place but also in the upper left part of the plot, ones over the others, but outside, getting mixed with other plots or text that appear in the pdf.
>> Have this happened to you?
>
> No, it has never happened to me, but it is not totally surprising to me,
> either. :-)
I'm not sure I can duplicate the problem. I created a plot with a legend
as a PostScript file and I opened it in Adobe Illustrator, which I
assumed would be the equivalent of opening it in LaTex, but the graphic
looks perfect in Illustrator.
I'm afraid I don't have time right now to figure out how to install and
use LaTex. Perhaps someone else here has a suggestion.
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
|
|
|
Re: Adding Legends to Coyote Graphics Windows [message #87542 is a reply to message #87537] |
Fri, 14 February 2014 08:11   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
David Fanning writes:
> So, there are a lot of smoke and mirror tricks that have be used. One of
> my tricks is to use a pixmap to do the drawing. That works for graphics
> windows, but not for PostScript files. In PostScript files, I tend to
> draw the text off the page and in the background color, hoping no one
> will notice. Obviously, I haven't been totally successful. :-(
Ah, it turns out I *wasn't* doing this in the background color as I
assumed I was. Try this version of the program and see if the results
are any better.
http://www.idlcoyote.com/programs/cglegenditem__define.pro
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
|
|
|
|
Re: Adding Legends to Coyote Graphics Windows [message #87544 is a reply to message #87543] |
Fri, 14 February 2014 09:22   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
elisadelgadomena@gmail.com writes:
> Thanks for the quick answer. I will try that new.
> In any case I changed to al_legend and it worked! Before I was using legend.pro, I didn't know this one, and I assumed that I had to use cglegend when using cgplot (since the normal legend.pro was not working with cgplot).
> I kept the program as before, just changing from cglegend to al_legend and though the appearance of the eps opened with gb or similar is the same, when creating the pdf in latex nothing strange appears now.
Yes, AL_LEGEND does the smoke and mirror trick in a slightly different
way. It draws the legend twice, but then "erases" the first instance
by drawing a rectangle over the first instance in the background color.
My solution was an attempt to avoid drawing the entire legend twice. :-)
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
|
|
|
|
|
|
|
|
|
Re: Adding Legends to Coyote Graphics Windows [message #87594 is a reply to message #87590] |
Mon, 17 February 2014 05:15  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
elisadelgadomena@gmail.com writes:
> You were right, now with the last version of cglegenditem__define everything is fine, there is no necessity of changing yy=1.25 to 0.85.
> What I don't understand is why cglegend now also works fine if there is no difference between the previous and current version, contrary to what happens to cglegenditem__define
cgLegend is simply a one line wrapper for cgLegendItem for people who
are afraid of objects. :-)
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
|
|
|