plot function and legend in a for loop [message #91851] |
Fri, 04 September 2015 05:25  |
andreas.brunn
Messages: 3 Registered: September 2015
|
Junior Member |
|
|
Dear all,
I'm trying to create an indefinite number of lines in one plot. Additionally the goal is to additionally get a legend.
I was trying to do this somehow like this:
p=plot(spec(0), name=names(0))
for i=1,n_elements(spec-1) do p1=plot(spec(i), name=names(i), /over)
l=legend(target=[p,p1])
doing that, not unsurprisingly, I get only the first and last name in the legend.
Is there a way to mitigate the issue other than adding if statements like
if n_elements(spec) gt 2 then p3=plot ....
if n_elements(spec) gt 3 then p4=plot ....
with this way, no matter how many of these lines I add I would at some day end up with some plots not being plotted ;-)
thanks a lot and best regards
Andreas
|
|
|
Re: plot function and legend in a for loop [message #91852 is a reply to message #91851] |
Fri, 04 September 2015 05:39   |
Alain Kattnig
Messages: 9 Registered: November 2009
|
Junior Member |
|
|
Le vendredi 4 septembre 2015 14:25:37 UTC+2, andrea...@gmx.de a écrit :
> Dear all,
>
> I'm trying to create an indefinite number of lines in one plot. Additionally the goal is to additionally get a legend.
>
> I was trying to do this somehow like this:
>
> p=plot(spec(0), name=names(0))
>
> for i=1,n_elements(spec-1) do p1=plot(spec(i), name=names(i), /over)
>
>
> l=legend(target=[p,p1])
>
> doing that, not unsurprisingly, I get only the first and last name in the legend.
>
> Is there a way to mitigate the issue other than adding if statements like
>
> if n_elements(spec) gt 2 then p3=plot ....
> if n_elements(spec) gt 3 then p4=plot ....
>
> with this way, no matter how many of these lines I add I would at some day end up with some plots not being plotted ;-)
>
> thanks a lot and best regards
> Andreas
I use IPLOT which is less open to manipulations but quicker to code
IPLOT,spec[0],NAME=name[0],/INSERT_LEGEND
FOR number=1,Nbr_names DO IPLOT,spec[number],NAME=name[number],/INSERT_LEGEND
Best
|
|
|
Re: plot function and legend in a for loop [message #91853 is a reply to message #91851] |
Fri, 04 September 2015 05:40   |
andreas.brunn
Messages: 3 Registered: September 2015
|
Junior Member |
|
|
On Friday, September 4, 2015 at 2:25:37 PM UTC+2, andrea...@gmx.de wrote:
> Dear all,
>
> I'm trying to create an indefinite number of lines in one plot. Additionally the goal is to additionally get a legend.
>
> I was trying to do this somehow like this:
>
> p=plot(spec(0), name=names(0))
>
> for i=1,n_elements(spec-1) do p1=plot(spec(i), name=names(i), /over)
>
>
> l=legend(target=[p,p1])
>
> doing that, not unsurprisingly, I get only the first and last name in the legend.
>
> Is there a way to mitigate the issue other than adding if statements like
>
> if n_elements(spec) gt 2 then p3=plot ....
> if n_elements(spec) gt 3 then p4=plot ....
>
> with this way, no matter how many of these lines I add I would at some day end up with some plots not being plotted ;-)
>
> thanks a lot and best regards
> Andreas
Ok I have tried the most obvious last and just got rid of the target keyword. In this case it did what I wanted from the beginning.
Nevertheless. Is there a way to address properties explicitly in cases like that?
thanks again,
Andreas
|
|
|
Re: plot function and legend in a for loop [message #91854 is a reply to message #91852] |
Fri, 04 September 2015 05:45   |
andreas.brunn
Messages: 3 Registered: September 2015
|
Junior Member |
|
|
On Friday, September 4, 2015 at 2:39:32 PM UTC+2, kallisthene wrote:
> Le vendredi 4 septembre 2015 14:25:37 UTC+2, andrea...@gmx.de a écrit :
>> Dear all,
>>
>> I'm trying to create an indefinite number of lines in one plot. Additionally the goal is to additionally get a legend.
>>
>> I was trying to do this somehow like this:
>>
>> p=plot(spec(0), name=names(0))
>>
>> for i=1,n_elements(spec-1) do p1=plot(spec(i), name=names(i), /over)
>>
>>
>> l=legend(target=[p,p1])
>>
>> doing that, not unsurprisingly, I get only the first and last name in the legend.
>>
>> Is there a way to mitigate the issue other than adding if statements like
>>
>> if n_elements(spec) gt 2 then p3=plot ....
>> if n_elements(spec) gt 3 then p4=plot ....
>>
>> with this way, no matter how many of these lines I add I would at some day end up with some plots not being plotted ;-)
>>
>> thanks a lot and best regards
>> Andreas
>
> I use IPLOT which is less open to manipulations but quicker to code
>
> IPLOT,spec[0],NAME=name[0],/INSERT_LEGEND
> FOR number=1,Nbr_names DO IPLOT,spec[number],NAME=name[number],/INSERT_LEGEND
>
>
> Best
Thanks a lot, I'll give it a try ... never tried the itools,
|
|
|
Re: plot function and legend in a for loop [message #91860 is a reply to message #91851] |
Fri, 04 September 2015 12:24   |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
On Friday, September 4, 2015 at 8:25:37 AM UTC-4, andrea...@gmx.de wrote:
> Dear all,
>
> I'm trying to create an indefinite number of lines in one plot. Additionally the goal is to additionally get a legend.
>
> I was trying to do this somehow like this:
>
> p=plot(spec(0), name=names(0))
>
> for i=1,n_elements(spec-1) do p1=plot(spec(i), name=names(i), /over)
>
>
> l=legend(target=[p,p1])
>
> doing that, not unsurprisingly, I get only the first and last name in the legend.
>
> Is there a way to mitigate the issue other than adding if statements like
>
> if n_elements(spec) gt 2 then p3=plot ....
> if n_elements(spec) gt 3 then p4=plot ....
>
> with this way, no matter how many of these lines I add I would at some day end up with some plots not being plotted ;-)
>
> thanks a lot and best regards
> Andreas
I would try something like this:
p=objarr(n)
for i = 0, n-2 do begin
p[i] = plot(...., name=names[i],..., overplot=i)
endfor
l = legend(target=p)
cheers,
paulv
|
|
|
|