Plot problems - setColor

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Plot problems - setColor

Kenneth Sloan-3
Here is a snippet of Java code

    private Plot[] plot;

    ...


       for(int m=0;m<nMeridians;m++)
        {

         ...compute means[], stdDevs[], etc


          String title = "MPOD spatial distribution (" + mLabel[m] + ")";
          String xLabel = "Radius(') - " + mLabel[m];,
          String yLabel = "MPOD";
          float[] xValues = new float[nBins];
          float[] xValuesInDegrees = new float[nBins];
          float[] yValues = new float[nBins];
          float[] errorBars = new float[nBins];
          double toMiddleOfBin = (lowerBounds[1]-lowerBounds[0])/2.0;
          for(int b=0;b<nBins;b++)
              {
                  xValues[b] = (float)(lowerBounds[b] + toMiddleOfBin);
                  xValuesInDegrees[b] = (float)a((double)xValues[b]);
                  yValues[b] = (float)means[b];
                  errorBars[b] = (float)stdDevs[b];
              }

          plot[m] = new Plot(title,xLabel,yLabel,xValuesInDegrees,yValues);
          plot[m].addErrorBars(errorBars);
          plot[m].addPoints(xValuesInDegrees,yMinValues,Plot.LINE);
          plot[m].addPoints(xValuesInDegrees,yMaxValues,Plot.LINE);

          if(m==0)
            {
                plot[m].setColor(Color.GREEN);          /*  PROBLEM */
                plot[m].drawLine(1.0,-2.0,1.0,2.0);
            }
          plot[m].setLimits(0.0, 9.75, minMPOD, maxMPOD);
      }

   ...


These plots are later assembled into a Stack, like so:


                ImageStack plotStack = null;
                for(int m=0;m<nMeridians;m++)
                    {
                        plot[m].setSize(plotWidth, plotHeight);
                        ImageProcessor plotImageProcessor = plot[m].getImagePlus().getProcessor();
                        if(0==m)
                            plotStack
                                = new ImageStack(plotImageProcessor.getWidth(),
                                                 plotImageProcessor.getHeight());
                        plotStack.addSlice(mLabel[m],plotImageProcessor);
                    }
                ImageWindow.setNextLocation(plotLocationX, plotLocationY);
                ImageWindow plotImageWindow
                    = new ImageWindow(
                                      new ImagePlus("MPOD spatial distribution plots",plotStack));
                plotImageWindow.show();


If I comment out the line marked "PROBLEM", then everything works as expected.  I get 9 different plots.  Only plot[0]
has a vertical line at 1.0.  Everything is in BLACK.

If I leave it as written above, several things happen:
   a) there is a vertical line, in GREEN, at 1.0 (good!)
   b) the main x,y values and the error bars are also in GREEN (bad)
   c) all 9 plots are identical!  (very, very bad)

I just ran tests with ONLY the line marked "PROBLEM" in and out.  Nothing else changed.  I'm confused.

Is there something obvious that I'm doing wrong?

Eventually, I want to add color coding, and a few more vertical lines to mark special values.  I'm going to add the vertical lines, now (in BLACK), but I'd really like to color code them.  But..."setColor" seems to do something strange.

Help, please!

--
Kenneth Sloan
[hidden email]
Vision is the art of seeing what is invisible to others.






--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Plot problems - setColor

Gabriel Landini
On Wednesday, 12 September 2018 12:44:02 BST Kenneth wrote:
> If I leave it as written above, several things happen:
>    a) there is a vertical line, in GREEN, at 1.0 (good!)
>    b) the main x,y values and the error bars are also in GREEN (bad)
>    c) all 9 plots are identical!  (very, very bad)

Maybe b) is due to the fact that you have not set back the colour to the
default so the next line also uses it:
           plot[m].setLimits(0.0, 9.75, minMPOD, maxMPOD);

What about adding a line:
 if(m==0)
            {
                plot[m].setColor(Color.GREEN);          /*  PROBLEM */
                plot[m].drawLine(1.0,-2.0,1.0,2.0);
                plot[m].setColor(Color.BLACK);
            }
I did not try this, though.

Hope it helps

Gabriel

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Plot problems - setColor

Kenneth Sloan-3
Thanks - I tried that.

But, note that the main curve, and the error bars, are drawn BEFORE my attempt to add
vertical lines to the plot.

It's confusing to me that you can't seem to set the color for the initial set of points - but
I'll worry about that, later...

Right now, I'm trying to work out how to turn the grid off.  [I'm NOT using a Plot Window]
I think I've found it...testing...


--
Kenneth Sloan
[hidden email]
Vision is the art of seeing what is invisible to others.





> On 12 Sep 2018, at 13:00 , Gabriel Landini <[hidden email]> wrote:
>
> On Wednesday, 12 September 2018 12:44:02 BST Kenneth wrote:
>> If I leave it as written above, several things happen:
>>   a) there is a vertical line, in GREEN, at 1.0 (good!)
>>   b) the main x,y values and the error bars are also in GREEN (bad)
>>   c) all 9 plots are identical!  (very, very bad)
>
> Maybe b) is due to the fact that you have not set back the colour to the
> default so the next line also uses it:
>           plot[m].setLimits(0.0, 9.75, minMPOD, maxMPOD);
>
> What about adding a line:
> if(m==0)
>            {
>                plot[m].setColor(Color.GREEN);          /*  PROBLEM */
>                plot[m].drawLine(1.0,-2.0,1.0,2.0);
>                plot[m].setColor(Color.BLACK);
>            }
> I did not try this, though.
>
> Hope it helps
>
> Gabriel
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Plot problems - setColor

Wayne Rasband-2
In reply to this post by Kenneth Sloan-3
> On Sep 12, 2018, at 7:44 AM, Kenneth Sloan <[hidden email]> wrote:
>
> Here is a snippet of Java code

Snippets are not very helpful. We need a minimal working example so we can reproduce the problem,  something like the example at Help>Examples>JavaScript>Dynamic Plot, which generates and displays an array of plots.

-wayne

>   private Plot[] plot;
>
>    ...
>
>
>       for(int m=0;m<nMeridians;m++)
> {
>
>         ...compute means[], stdDevs[], etc
>
>
>          String title = "MPOD spatial distribution (" + mLabel[m] + ")";
>          String xLabel = "Radius(') - " + mLabel[m];,
>          String yLabel = "MPOD";
>          float[] xValues = new float[nBins];
>          float[] xValuesInDegrees = new float[nBins];
>          float[] yValues = new float[nBins];
>          float[] errorBars = new float[nBins];
>          double toMiddleOfBin = (lowerBounds[1]-lowerBounds[0])/2.0;
>          for(int b=0;b<nBins;b++)
>              {
>                  xValues[b] = (float)(lowerBounds[b] + toMiddleOfBin);
>                  xValuesInDegrees[b] = (float)a((double)xValues[b]);
>                  yValues[b] = (float)means[b];
>                  errorBars[b] = (float)stdDevs[b];
>              }
>
>          plot[m] = new Plot(title,xLabel,yLabel,xValuesInDegrees,yValues);
>          plot[m].addErrorBars(errorBars);
>          plot[m].addPoints(xValuesInDegrees,yMinValues,Plot.LINE);
>          plot[m].addPoints(xValuesInDegrees,yMaxValues,Plot.LINE);
>
>          if(m==0)
>            {
>                plot[m].setColor(Color.GREEN);          /*  PROBLEM */
>                plot[m].drawLine(1.0,-2.0,1.0,2.0);
>            }
>          plot[m].setLimits(0.0, 9.75, minMPOD, maxMPOD);
>      }
>
>   ...
>
>
> These plots are later assembled into a Stack, like so:
>
>
>                ImageStack plotStack = null;
>                for(int m=0;m<nMeridians;m++)
>                    {
>                        plot[m].setSize(plotWidth, plotHeight);
>                        ImageProcessor plotImageProcessor = plot[m].getImagePlus().getProcessor();
>                        if(0==m)
>                            plotStack
>                                = new ImageStack(plotImageProcessor.getWidth(),
>                                                 plotImageProcessor.getHeight());
>                        plotStack.addSlice(mLabel[m],plotImageProcessor);
>                    }
>                ImageWindow.setNextLocation(plotLocationX, plotLocationY);
>                ImageWindow plotImageWindow
>                    = new ImageWindow(
>                                      new ImagePlus("MPOD spatial distribution plots",plotStack));
>                plotImageWindow.show();
>
>
> If I comment out the line marked "PROBLEM", then everything works as expected.  I get 9 different plots.  Only plot[0]
> has a vertical line at 1.0.  Everything is in BLACK.
>
> If I leave it as written above, several things happen:
>   a) there is a vertical line, in GREEN, at 1.0 (good!)
>   b) the main x,y values and the error bars are also in GREEN (bad)
>   c) all 9 plots are identical!  (very, very bad)
>
> I just ran tests with ONLY the line marked "PROBLEM" in and out.  Nothing else changed.  I'm confused.
>
> Is there something obvious that I'm doing wrong?
>
> Eventually, I want to add color coding, and a few more vertical lines to mark special values.  I'm going to add the vertical lines, now (in BLACK), but I'd really like to color code them.  But..."setColor" seems to do something strange.
>
> Help, please!
>
> --
> Kenneth Sloan
> [hidden email]
> Vision is the art of seeing what is invisible to others.
>
>
>
>
>
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Plot problems - setColor

Kenneth Sloan-3
Thank you for the reply.  This code was embedded in a much larger program, and I was on deadline.
I have worked around it by deciding to NOT try to markup a single plot in the array of plots, and all
seems well.

When I return home (in about 48 hours) I'll see if I can produce a minimal program that duplicates the problem.

For now, all I know is that simply commenting out ONE line causes the problem to go away, and re-inserting that line causes the problem to reappear.  What is particularly puzzling is that code doing a "setColor" in one plot is changing the contents of
subsequent plots!  Not just the colors - the values plotted.

More sometime over the weekend...

--
Kenneth Sloan
[hidden email]
Vision is the art of seeing what is invisible to others.





> On 12 Sep 2018, at 16:02 , Wayne Rasband <[hidden email]> wrote:
>
>> On Sep 12, 2018, at 7:44 AM, Kenneth Sloan <[hidden email] <mailto:[hidden email]>> wrote:
>>
>> Here is a snippet of Java code
>
> Snippets are not very helpful. We need a minimal working example so we can reproduce the problem,  something like the example at Help>Examples>JavaScript>Dynamic Plot, which generates and displays an array of plots.
>
> -wayne
>
>>  private Plot[] plot;
>>
>>   ...
>>
>>
>>      for(int m=0;m<nMeridians;m++)
>> {
>>
>>        ...compute means[], stdDevs[], etc
>>
>>
>>         String title = "MPOD spatial distribution (" + mLabel[m] + ")";
>>         String xLabel = "Radius(') - " + mLabel[m];,
>>         String yLabel = "MPOD";
>>         float[] xValues = new float[nBins];
>>         float[] xValuesInDegrees = new float[nBins];
>>         float[] yValues = new float[nBins];
>>         float[] errorBars = new float[nBins];
>>         double toMiddleOfBin = (lowerBounds[1]-lowerBounds[0])/2.0;
>>         for(int b=0;b<nBins;b++)
>>             {
>>                 xValues[b] = (float)(lowerBounds[b] + toMiddleOfBin);
>>                 xValuesInDegrees[b] = (float)a((double)xValues[b]);
>>                 yValues[b] = (float)means[b];
>>                 errorBars[b] = (float)stdDevs[b];
>>             }
>>
>>         plot[m] = new Plot(title,xLabel,yLabel,xValuesInDegrees,yValues);
>>         plot[m].addErrorBars(errorBars);
>>         plot[m].addPoints(xValuesInDegrees,yMinValues,Plot.LINE);
>>         plot[m].addPoints(xValuesInDegrees,yMaxValues,Plot.LINE);
>>
>>         if(m==0)
>>           {
>>               plot[m].setColor(Color.GREEN);          /*  PROBLEM */
>>               plot[m].drawLine(1.0,-2.0,1.0,2.0);
>>           }
>>         plot[m].setLimits(0.0, 9.75, minMPOD, maxMPOD);
>>     }
>>
>>  ...
>>
>>
>> These plots are later assembled into a Stack, like so:
>>
>>
>>               ImageStack plotStack = null;
>>               for(int m=0;m<nMeridians;m++)
>>                   {
>>                       plot[m].setSize(plotWidth, plotHeight);
>>                       ImageProcessor plotImageProcessor = plot[m].getImagePlus().getProcessor();
>>                       if(0==m)
>>                           plotStack
>>                               = new ImageStack(plotImageProcessor.getWidth(),
>>                                                plotImageProcessor.getHeight());
>>                       plotStack.addSlice(mLabel[m],plotImageProcessor);
>>                   }
>>               ImageWindow.setNextLocation(plotLocationX, plotLocationY);
>>               ImageWindow plotImageWindow
>>                   = new ImageWindow(
>>                                     new ImagePlus("MPOD spatial distribution plots",plotStack));
>>               plotImageWindow.show();
>>
>>
>> If I comment out the line marked "PROBLEM", then everything works as expected.  I get 9 different plots.  Only plot[0]
>> has a vertical line at 1.0.  Everything is in BLACK.
>>
>> If I leave it as written above, several things happen:
>>  a) there is a vertical line, in GREEN, at 1.0 (good!)
>>  b) the main x,y values and the error bars are also in GREEN (bad)
>>  c) all 9 plots are identical!  (very, very bad)
>>
>> I just ran tests with ONLY the line marked "PROBLEM" in and out.  Nothing else changed.  I'm confused.
>>
>> Is there something obvious that I'm doing wrong?
>>
>> Eventually, I want to add color coding, and a few more vertical lines to mark special values.  I'm going to add the vertical lines, now (in BLACK), but I'd really like to color code them.  But..."setColor" seems to do something strange.
>>
>> Help, please!
>>
>> --
>> Kenneth Sloan
>> [hidden email]
>> Vision is the art of seeing what is invisible to others.
>>
>>
>>
>>
>>
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html <http://imagej.nih.gov/ij/list.html>
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html <http://imagej.nih.gov/ij/list.html>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Plot problems - setColor

Fred Damen
In reply to this post by Kenneth Sloan-3
Greetings,

see inline.

Fred


On Wed, September 12, 2018 7:18 am, Kenneth Sloan wrote:
> Thanks - I tried that.
>
> But, note that the main curve, and the error bars, are drawn BEFORE my attempt
> to add
> vertical lines to the plot.
My feeble  understanding is that the curve is not drawn when the method is
called to request the curve, it is drawn when a request to draw/show the plot.
 The ramification being knowing what the limits are before the draw/show.  It
would be nice to have a horizontal/vertical line that has infinite asymptotes.

>
> It's confusing to me that you can't seem to set the color for the initial set
> of points - but
> I'll worry about that, later...
What I do is to request the curve twice, or, do not draw the curve in the
initial plot creation.  Visually looks like the desired color scheme.

>
> Right now, I'm trying to work out how to turn the grid off.  [I'm NOT using a
> Plot Window]
> I think I've found it...testing...
>
>
> --
> Kenneth Sloan
> [hidden email]
> Vision is the art of seeing what is invisible to others.
>
>
>
>
>
>> On 12 Sep 2018, at 13:00 , Gabriel Landini <[hidden email]> wrote:
>>
>> On Wednesday, 12 September 2018 12:44:02 BST Kenneth wrote:
>>> If I leave it as written above, several things happen:
>>>   a) there is a vertical line, in GREEN, at 1.0 (good!)
>>>   b) the main x,y values and the error bars are also in GREEN (bad)
>>>   c) all 9 plots are identical!  (very, very bad)
>>
>> Maybe b) is due to the fact that you have not set back the colour to the
>> default so the next line also uses it:
>>           plot[m].setLimits(0.0, 9.75, minMPOD, maxMPOD);
>>
>> What about adding a line:
>> if(m==0)
>>            {
>>                plot[m].setColor(Color.GREEN);          /*  PROBLEM */
>>                plot[m].drawLine(1.0,-2.0,1.0,2.0);
>>                plot[m].setColor(Color.BLACK);
>>            }
>> I did not try this, though.
>>
>> Hope it helps
>>
>> Gabriel
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Plot problems - setColor

Michael Schmid-3
Hi Kenneth,

yes there is an odd behavior with the sequence how the curves are drawn
in plots:

The easiest way to get the expected result is not giving any data when
creating the plot (constructor in Java, Plot.create in the macro
language). Then, simply add the data sets one by one, always setting the
color etc. before adding the data set. These data sets are drawn in the
sequence they are added; the last one on top of the previous ones.

If you specify a data set in the constructor (or in a macro, in
Plot.create), this data set is plotted last (on top of the others). The
color for this data set can be specified before you *show* the plot.
This is only briefly mentioned in the constructor of the Plot class:
    Note that the data xValues, yValues passed with the constructor are
plotted last, with the settings (color, lineWidth) at the time when
'draw' or 'getProcessor' is called.

The reason for this odd behavior is how the Plot class in ImageJ has
evolved over time, and straightening this out would cause
incompatibility with previous versions.


Michael
________________________________________________________________
On 12/09/2018 19:44, Kenneth Sloan wrote:
 > Thank you for the reply.  This code was embedded in a much larger
program, and I was on deadline.
 > I have worked around it by deciding to NOT try to markup a single
plot in the array of plots, and all
 > seems well.
 >
 > When I return home (in about 48 hours) I'll see if I can produce a
minimal program that duplicates the problem.
 >
 > For now, all I know is that simply commenting out ONE line causes the
problem to go away, and re-inserting that line causes the problem to
reappear.  What is particularly puzzling is that code doing a "setColor"
in one plot is changing the contents of
 > subsequent plots!  Not just the colors - the values plotted.
 >
 > More sometime over the weekend...
 >
 > --
 > Kenneth Sloan
 > [hidden email]
 > Vision is the art of seeing what is invisible to others.
 >
 >
 >
 >
 >
 >> On 12 Sep 2018, at 16:02 , Wayne Rasband <[hidden email]> wrote:
 >>
 >>> On Sep 12, 2018, at 7:44 AM, Kenneth Sloan <[hidden email]
<mailto:[hidden email]>> wrote:
 >>>
 >>> Here is a snippet of Java code
 >>
 >> Snippets are not very helpful. We need a minimal working example so
we can reproduce the problem,  something like the example at
Help>Examples>JavaScript>Dynamic Plot, which generates and displays an
array of plots.
 >>
 >> -wayne
 >>
 >>>   private Plot[] plot;
 >>>
 >>>    ...
 >>>
 >>>
 >>>       for(int m=0;m<nMeridians;m++)
 >>>     {
 >>>
 >>>         ...compute means[], stdDevs[], etc
 >>>
 >>>
 >>>          String title = "MPOD spatial distribution (" + mLabel[m] +
")";
 >>>          String xLabel = "Radius(') - " + mLabel[m];,
 >>>          String yLabel = "MPOD";
 >>>          float[] xValues = new float[nBins];
 >>>          float[] xValuesInDegrees = new float[nBins];
 >>>          float[] yValues = new float[nBins];
 >>>          float[] errorBars = new float[nBins];
 >>>          double toMiddleOfBin = (lowerBounds[1]-lowerBounds[0])/2.0;
 >>>          for(int b=0;b<nBins;b++)
 >>>              {
 >>>                  xValues[b] = (float)(lowerBounds[b] + toMiddleOfBin);
 >>>                  xValuesInDegrees[b] = (float)a((double)xValues[b]);
 >>>                  yValues[b] = (float)means[b];
 >>>                  errorBars[b] = (float)stdDevs[b];
 >>>              }
 >>>
 >>>          plot[m] = new
Plot(title,xLabel,yLabel,xValuesInDegrees,yValues);
 >>>          plot[m].addErrorBars(errorBars);
 >>>          plot[m].addPoints(xValuesInDegrees,yMinValues,Plot.LINE);
 >>>          plot[m].addPoints(xValuesInDegrees,yMaxValues,Plot.LINE);
 >>>
 >>>          if(m==0)
 >>>            {
 >>>                plot[m].setColor(Color.GREEN);          /*  PROBLEM */
 >>>                plot[m].drawLine(1.0,-2.0,1.0,2.0);
 >>>            }
 >>>          plot[m].setLimits(0.0, 9.75, minMPOD, maxMPOD);
 >>>      }
 >>>
 >>>   ...
 >>>
 >>>
 >>> These plots are later assembled into a Stack, like so:
 >>>
 >>>
 >>>                ImageStack plotStack = null;
 >>>                for(int m=0;m<nMeridians;m++)
 >>>                    {
 >>>                        plot[m].setSize(plotWidth, plotHeight);
 >>>                        ImageProcessor plotImageProcessor =
plot[m].getImagePlus().getProcessor();
 >>>                        if(0==m)
 >>>                            plotStack
 >>>                                = new
ImageStack(plotImageProcessor.getWidth(),
 >>>
plotImageProcessor.getHeight());
 >>>
plotStack.addSlice(mLabel[m],plotImageProcessor);
 >>>                    }
 >>>                ImageWindow.setNextLocation(plotLocationX,
plotLocationY);
 >>>                ImageWindow plotImageWindow
 >>>                    = new ImageWindow(
 >>>                                      new ImagePlus("MPOD spatial
distribution plots",plotStack));
 >>>                plotImageWindow.show();
 >>>
 >>>
 >>> If I comment out the line marked "PROBLEM", then everything works
as expected.  I get 9 different plots.  Only plot[0]
 >>> has a vertical line at 1.0.  Everything is in BLACK.
 >>>
 >>> If I leave it as written above, several things happen:
 >>>   a) there is a vertical line, in GREEN, at 1.0 (good!)
 >>>   b) the main x,y values and the error bars are also in GREEN (bad)
 >>>   c) all 9 plots are identical!  (very, very bad)
 >>>
 >>> I just ran tests with ONLY the line marked "PROBLEM" in and out.
Nothing else changed.  I'm confused.
 >>>
 >>> Is there something obvious that I'm doing wrong?
 >>>
 >>> Eventually, I want to add color coding, and a few more vertical
lines to mark special values.  I'm going to add the vertical lines, now
(in BLACK), but I'd really like to color code them.  But..."setColor"
seems to do something strange.
 >>>
 >>> Help, please!
 >>>
 >>> --
 >>> Kenneth Sloan
 >>> [hidden email]
 >>> Vision is the art of seeing what is invisible to others.
 >>>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Plot problems - setColor

Kenneth Sloan-3
Thank you - I had other things to do, and ended up re-structuring the code to add more
colors.  As a result, I changed the constructor to NOT provide initial data.  I also
removed the restriction of the changes to plot[0].  Everything works now.

Your explanation is perfect for why the colors appeared as they did.

There is still the problem (now merely a curiousity) that when I was trying to add colored
features to only plot[0], ALL the plots were displaying exactly the same data.

Since I now have what I want, and am still on deadline, I will put off exploring this issue
until later.  At that time, I'll write a minimal program to try to reproduce the problem.

Or, perhaps I'll just stare at the original code until a facepalm event occurs.

Again - thank you to everyone who provided helpful replies.

One nagging question: it appears that "errorBars" are intimately attached to the curve they modify.
Is that correct?  The reason I ask is that I want the curve and the errorBars in different colors, and
the only way I can get that to work is to plot the curve and errorBars together, and then plot the
curve again (in a different color) on top.  It seems slightly inefficient, but it works.

This happens when plotting the curve and errorBars in one call, and also when plotting the curve in one call and
adding the errorBars separately.  In the latter case, a setColor in between the curve and the errorBars seems
to affect the color of BOTH the curve AND the errorBars.

But, again, this is all now at the "curious" level, and is no longer on my critical path.  I have working code that does what I want.

For the record, below is the modified code to build the array of plots.  Criticism welcome!

  ...
   private Plot[] plot;
  ...

        this.plot = new Plot[nMeridians];
        ...
        for(int m=0;m<nMeridians;m++)
            {
                ...

                String title = "MPOD spatial distribution (" + mLabel[m] + ")";
                String xLabel = "Radius(') - " + mLabel[m];
                String yLabel = "MPOD";
                double[] xValues = new double[nBins];
                double[] xValuesInDegrees = new double[nBins];
                double[] yValues = new double[nBins];
                double[] errorBars = new double[nBins];
                double toMiddleOfBin = (lowerBounds[1]-lowerBounds[0])/2.0;
                for(int b=0;b<nBins;b++)
                    {
                        xValues[b] = lowerBounds[b] + toMiddleOfBin;
                        xValuesInDegrees[b] = a(xValues[b]);  /* convert from radians to degrees */
                        yValues[b] = means[b];
                        errorBars[b] = stdDevs[b];
                    }

                plot[m] = new Plot(title,xLabel,yLabel);
                int flags = plot[m].getFlags();
                flags = flags ^ Plot.X_GRID;
                flags = flags ^ Plot.Y_GRID;
                plot[m].setFormatFlags(flags);

                plot[m].setColor(Color.GREEN);
                plot[m].addPoints(xValuesInDegrees,yValues,errorBars,Plot.LINE);
                plot[m].setColor(Color.RED);
                plot[m].addPoints(xValuesInDegrees,yValues,Plot.LINE);

                plot[m].setColor(Color.BLUE);
                plot[m].addPoints(xValuesInDegrees,yMinValues,Plot.LINE);
                plot[m].addPoints(xValuesInDegrees,yMaxValues,Plot.LINE);

                plot[m].setColor(Color.RED);
                plot[m].drawLine(1.0,-2.0,1.0,2.0);

                plot[m].setColor(Color.BLUE);
                plot[m].drawLine(2.0,-2.0,2.0,2.0);

                plot[m].setColor(Color.GREEN);
                plot[m].drawLine(6.0,-2.0,6.0,2.0);

                plot[m].setColor(Color.GREEN);
                plot[m].drawLine(0.0,0.0,9.75,0.0);

                plot[m].setLimits(0.0, 9.75, minMPOD, maxMPOD);
            }


--
Kenneth Sloan
[hidden email]
Vision is the art of seeing what is invisible to others.





> On 13 Sep 2018, at 04:57 , Michael Schmid <[hidden email]> wrote:
>
> Hi Kenneth,
>
> yes there is an odd behavior with the sequence how the curves are drawn in plots:
>
> The easiest way to get the expected result is not giving any data when creating the plot (constructor in Java, Plot.create in the macro language). Then, simply add the data sets one by one, always setting the color etc. before adding the data set. These data sets are drawn in the sequence they are added; the last one on top of the previous ones.
>
> If you specify a data set in the constructor (or in a macro, in Plot.create), this data set is plotted last (on top of the others). The color for this data set can be specified before you *show* the plot.
> This is only briefly mentioned in the constructor of the Plot class:
>   Note that the data xValues, yValues passed with the constructor are plotted last, with the settings (color, lineWidth) at the time when 'draw' or 'getProcessor' is called.
>
> The reason for this odd behavior is how the Plot class in ImageJ has evolved over time, and straightening this out would cause incompatibility with previous versions.
>
>
> Michael
> ________________________________________________________________
> On 12/09/2018 19:44, Kenneth Sloan wrote:
> > Thank you for the reply.  This code was embedded in a much larger program, and I was on deadline.
> > I have worked around it by deciding to NOT try to markup a single plot in the array of plots, and all
> > seems well.
> >
> > When I return home (in about 48 hours) I'll see if I can produce a minimal program that duplicates the problem.
> >
> > For now, all I know is that simply commenting out ONE line causes the problem to go away, and re-inserting that line causes the problem to reappear.  What is particularly puzzling is that code doing a "setColor" in one plot is changing the contents of
> > subsequent plots!  Not just the colors - the values plotted.
> >
> > More sometime over the weekend...
> >
> > --
> > Kenneth Sloan
> > [hidden email]
> > Vision is the art of seeing what is invisible to others.
> >
> >
> >
> >
> >
> >> On 12 Sep 2018, at 16:02 , Wayne Rasband <[hidden email]> wrote:
> >>
> >>> On Sep 12, 2018, at 7:44 AM, Kenneth Sloan <[hidden email] <mailto:[hidden email]>> wrote:
> >>>
> >>> Here is a snippet of Java code
> >>
> >> Snippets are not very helpful. We need a minimal working example so we can reproduce the problem,  something like the example at Help>Examples>JavaScript>Dynamic Plot, which generates and displays an array of plots.
> >>
> >> -wayne
> >>
> >>>   private Plot[] plot;
> >>>
> >>>    ...
> >>>
> >>>
> >>>       for(int m=0;m<nMeridians;m++)
> >>>     {
> >>>
> >>>         ...compute means[], stdDevs[], etc
> >>>
> >>>
> >>>          String title = "MPOD spatial distribution (" + mLabel[m] + ")";
> >>>          String xLabel = "Radius(') - " + mLabel[m];,
> >>>          String yLabel = "MPOD";
> >>>          float[] xValues = new float[nBins];
> >>>          float[] xValuesInDegrees = new float[nBins];
> >>>          float[] yValues = new float[nBins];
> >>>          float[] errorBars = new float[nBins];
> >>>          double toMiddleOfBin = (lowerBounds[1]-lowerBounds[0])/2.0;
> >>>          for(int b=0;b<nBins;b++)
> >>>              {
> >>>                  xValues[b] = (float)(lowerBounds[b] + toMiddleOfBin);
> >>>                  xValuesInDegrees[b] = (float)a((double)xValues[b]);
> >>>                  yValues[b] = (float)means[b];
> >>>                  errorBars[b] = (float)stdDevs[b];
> >>>              }
> >>>
> >>>          plot[m] = new Plot(title,xLabel,yLabel,xValuesInDegrees,yValues);
> >>>          plot[m].addErrorBars(errorBars);
> >>>          plot[m].addPoints(xValuesInDegrees,yMinValues,Plot.LINE);
> >>>          plot[m].addPoints(xValuesInDegrees,yMaxValues,Plot.LINE);
> >>>
> >>>          if(m==0)
> >>>            {
> >>>                plot[m].setColor(Color.GREEN);          /*  PROBLEM */
> >>>                plot[m].drawLine(1.0,-2.0,1.0,2.0);
> >>>            }
> >>>          plot[m].setLimits(0.0, 9.75, minMPOD, maxMPOD);
> >>>      }
> >>>
> >>>   ...
> >>>
> >>>
> >>> These plots are later assembled into a Stack, like so:
> >>>
> >>>
> >>>                ImageStack plotStack = null;
> >>>                for(int m=0;m<nMeridians;m++)
> >>>                    {
> >>>                        plot[m].setSize(plotWidth, plotHeight);
> >>>                        ImageProcessor plotImageProcessor = plot[m].getImagePlus().getProcessor();
> >>>                        if(0==m)
> >>>                            plotStack
> >>>                                = new ImageStack(plotImageProcessor.getWidth(),
> >>> plotImageProcessor.getHeight());
> >>> plotStack.addSlice(mLabel[m],plotImageProcessor);
> >>>                    }
> >>>                ImageWindow.setNextLocation(plotLocationX, plotLocationY);
> >>>                ImageWindow plotImageWindow
> >>>                    = new ImageWindow(
> >>>                                      new ImagePlus("MPOD spatial distribution plots",plotStack));
> >>>                plotImageWindow.show();
> >>>
> >>>
> >>> If I comment out the line marked "PROBLEM", then everything works as expected.  I get 9 different plots.  Only plot[0]
> >>> has a vertical line at 1.0.  Everything is in BLACK.
> >>>
> >>> If I leave it as written above, several things happen:
> >>>   a) there is a vertical line, in GREEN, at 1.0 (good!)
> >>>   b) the main x,y values and the error bars are also in GREEN (bad)
> >>>   c) all 9 plots are identical!  (very, very bad)
> >>>
> >>> I just ran tests with ONLY the line marked "PROBLEM" in and out. Nothing else changed.  I'm confused.
> >>>
> >>> Is there something obvious that I'm doing wrong?
> >>>
> >>> Eventually, I want to add color coding, and a few more vertical lines to mark special values.  I'm going to add the vertical lines, now (in BLACK), but I'd really like to color code them.  But..."setColor" seems to do something strange.
> >>>
> >>> Help, please!
> >>>
> >>> --
> >>> Kenneth Sloan
> >>> [hidden email]
> >>> Vision is the art of seeing what is invisible to others.
> >>>
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html


--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Plot problems - setColor

Michael Schmid-3
Hi Kenneth,

concerning:
 > One nagging question: it appears that "errorBars" are intimately
 > attached to the curve they modify.
 > Is that correct?
 > The reason I ask is that I want the curve and the errorBars in
different colors

Yes, there is only one color for the symbol and error bar.
You can work around it by adding a data set twice, once with a dot as a
symbol and the error bars, and then once with the symbol you prefer.
Before the first "add", use specify the color of the error bars; before
the second "add", specify the color of the symbol.


Concerning:
 > There is still the problem (now merely a curiousity) that when I
 > was trying to add colored features to only plot[0],
 > ALL the plots were displaying exactly the same data.
Hmmm, I fear that something was wrong in your code. If you have
different Plot objects, they are independent; I see no way how one Plot
could access the data of another Plot.

By the way, in your previous code I noticed the line
   ImageProcessor plotImageProcessor =
plot[m].getImagePlus().getProcessor();
There is also a Plot.getProcessor() method, so you don't need the detour
via the ImagePlus.

One more remark:
If you want to have many plots in an ImageStack, there is also a
Plot.addToStack() method; when you are done, use the getStack() method.
See the Help>Examples>Plots>Dynamic Plots macro.
If you have a large number of plots, it has the advantage of less memory
consumption (it stores the data, not the images, and creates the image
to be displayed only for the time it is needed. Only if you save or
duplicate the stack, the plots are stored as images).


Michael
________________________________________________________________

On 14/09/2018 16:36, Kenneth Sloan wrote:

> Thank you - I had other things to do, and ended up re-structuring the code to add more
> colors.  As a result, I changed the constructor to NOT provide initial data.  I also
> removed the restriction of the changes to plot[0].  Everything works now.
>
> Your explanation is perfect for why the colors appeared as they did.
>
> There is still the problem (now merely a curiousity) that when I was trying to add colored
> features to only plot[0], ALL the plots were displaying exactly the same data.
>
> Since I now have what I want, and am still on deadline, I will put off exploring this issue
> until later.  At that time, I'll write a minimal program to try to reproduce the problem.
>
> Or, perhaps I'll just stare at the original code until a facepalm event occurs.
>
> Again - thank you to everyone who provided helpful replies.
>
> One nagging question: it appears that "errorBars" are intimately attached to the curve they modify.
> Is that correct?  The reason I ask is that I want the curve and the errorBars in different colors, and
> the only way I can get that to work is to plot the curve and errorBars together, and then plot the
> curve again (in a different color) on top.  It seems slightly inefficient, but it works.
>
> This happens when plotting the curve and errorBars in one call, and also when plotting the curve in one call and
> adding the errorBars separately.  In the latter case, a setColor in between the curve and the errorBars seems
> to affect the color of BOTH the curve AND the errorBars.
>
> But, again, this is all now at the "curious" level, and is no longer on my critical path.  I have working code that does what I want.
>
> For the record, below is the modified code to build the array of plots.  Criticism welcome!
>
>    ...
>     private Plot[] plot;
>    ...
>
>          this.plot = new Plot[nMeridians];
>          ...
>          for(int m=0;m<nMeridians;m++)
>              {
>                  ...
>
>                  String title = "MPOD spatial distribution (" + mLabel[m] + ")";
> String xLabel = "Radius(') - " + mLabel[m];
>                  String yLabel = "MPOD";
>                  double[] xValues = new double[nBins];
>                  double[] xValuesInDegrees = new double[nBins];
>                  double[] yValues = new double[nBins];
>                  double[] errorBars = new double[nBins];
>                  double toMiddleOfBin = (lowerBounds[1]-lowerBounds[0])/2.0;
>                  for(int b=0;b<nBins;b++)
>                      {
>                          xValues[b] = lowerBounds[b] + toMiddleOfBin;
>                          xValuesInDegrees[b] = a(xValues[b]);  /* convert from radians to degrees */
>                          yValues[b] = means[b];
> errorBars[b] = stdDevs[b];
>                      }
>
>                  plot[m] = new Plot(title,xLabel,yLabel);
>                  int flags = plot[m].getFlags();
>                  flags = flags ^ Plot.X_GRID;
> flags = flags ^ Plot.Y_GRID;
>                  plot[m].setFormatFlags(flags);
>
>                  plot[m].setColor(Color.GREEN);
>                  plot[m].addPoints(xValuesInDegrees,yValues,errorBars,Plot.LINE);
>                  plot[m].setColor(Color.RED);
>                  plot[m].addPoints(xValuesInDegrees,yValues,Plot.LINE);
>
>                  plot[m].setColor(Color.BLUE);
>                  plot[m].addPoints(xValuesInDegrees,yMinValues,Plot.LINE);
>                  plot[m].addPoints(xValuesInDegrees,yMaxValues,Plot.LINE);
>
>                  plot[m].setColor(Color.RED);
>                  plot[m].drawLine(1.0,-2.0,1.0,2.0);
>
>                  plot[m].setColor(Color.BLUE);
>                  plot[m].drawLine(2.0,-2.0,2.0,2.0);
>
>                  plot[m].setColor(Color.GREEN);
>                  plot[m].drawLine(6.0,-2.0,6.0,2.0);
>
>                  plot[m].setColor(Color.GREEN);
>                  plot[m].drawLine(0.0,0.0,9.75,0.0);
>
>                  plot[m].setLimits(0.0, 9.75, minMPOD, maxMPOD);
>              }
>
>
> --
> Kenneth Sloan
> [hidden email]
> Vision is the art of seeing what is invisible to others.
>
>
>
>
>
>> On 13 Sep 2018, at 04:57 , Michael Schmid <[hidden email]> wrote:
>>
>> Hi Kenneth,
>>
>> yes there is an odd behavior with the sequence how the curves are drawn in plots:
>>
>> The easiest way to get the expected result is not giving any data when creating the plot (constructor in Java, Plot.create in the macro language). Then, simply add the data sets one by one, always setting the color etc. before adding the data set. These data sets are drawn in the sequence they are added; the last one on top of the previous ones.
>>
>> If you specify a data set in the constructor (or in a macro, in Plot.create), this data set is plotted last (on top of the others). The color for this data set can be specified before you *show* the plot.
>> This is only briefly mentioned in the constructor of the Plot class:
>>    Note that the data xValues, yValues passed with the constructor are plotted last, with the settings (color, lineWidth) at the time when 'draw' or 'getProcessor' is called.
>>
>> The reason for this odd behavior is how the Plot class in ImageJ has evolved over time, and straightening this out would cause incompatibility with previous versions.
>>
>>
>> Michael
>> ________________________________________________________________
>> On 12/09/2018 19:44, Kenneth Sloan wrote:
>>> Thank you for the reply.  This code was embedded in a much larger program, and I was on deadline.
>>> I have worked around it by deciding to NOT try to markup a single plot in the array of plots, and all
>>> seems well.
>>>
>>> When I return home (in about 48 hours) I'll see if I can produce a minimal program that duplicates the problem.
>>>
>>> For now, all I know is that simply commenting out ONE line causes the problem to go away, and re-inserting that line causes the problem to reappear.  What is particularly puzzling is that code doing a "setColor" in one plot is changing the contents of
>>> subsequent plots!  Not just the colors - the values plotted.
>>>
>>> More sometime over the weekend...
>>>
>>> --
>>> Kenneth Sloan
>>> [hidden email]
>>> Vision is the art of seeing what is invisible to others.
>>>
>>>
>>>
>>>
>>>
>>>> On 12 Sep 2018, at 16:02 , Wayne Rasband <[hidden email]> wrote:
>>>>
>>>>> On Sep 12, 2018, at 7:44 AM, Kenneth Sloan <[hidden email] <mailto:[hidden email]>> wrote:
>>>>>
>>>>> Here is a snippet of Java code
>>>>
>>>> Snippets are not very helpful. We need a minimal working example so we can reproduce the problem,  something like the example at Help>Examples>JavaScript>Dynamic Plot, which generates and displays an array of plots.
>>>>
>>>> -wayne
>>>>
>>>>>    private Plot[] plot;
>>>>>
>>>>>     ...
>>>>>
>>>>>
>>>>>        for(int m=0;m<nMeridians;m++)
>>>>>      {
>>>>>
>>>>>          ...compute means[], stdDevs[], etc
>>>>>
>>>>>
>>>>>           String title = "MPOD spatial distribution (" + mLabel[m] + ")";
>>>>>           String xLabel = "Radius(') - " + mLabel[m];,
>>>>>           String yLabel = "MPOD";
>>>>>           float[] xValues = new float[nBins];
>>>>>           float[] xValuesInDegrees = new float[nBins];
>>>>>           float[] yValues = new float[nBins];
>>>>>           float[] errorBars = new float[nBins];
>>>>>           double toMiddleOfBin = (lowerBounds[1]-lowerBounds[0])/2.0;
>>>>>           for(int b=0;b<nBins;b++)
>>>>>               {
>>>>>                   xValues[b] = (float)(lowerBounds[b] + toMiddleOfBin);
>>>>>                   xValuesInDegrees[b] = (float)a((double)xValues[b]);
>>>>>                   yValues[b] = (float)means[b];
>>>>>                   errorBars[b] = (float)stdDevs[b];
>>>>>               }
>>>>>
>>>>>           plot[m] = new Plot(title,xLabel,yLabel,xValuesInDegrees,yValues);
>>>>>           plot[m].addErrorBars(errorBars);
>>>>>           plot[m].addPoints(xValuesInDegrees,yMinValues,Plot.LINE);
>>>>>           plot[m].addPoints(xValuesInDegrees,yMaxValues,Plot.LINE);
>>>>>
>>>>>           if(m==0)
>>>>>             {
>>>>>                 plot[m].setColor(Color.GREEN);          /*  PROBLEM */
>>>>>                 plot[m].drawLine(1.0,-2.0,1.0,2.0);
>>>>>             }
>>>>>           plot[m].setLimits(0.0, 9.75, minMPOD, maxMPOD);
>>>>>       }
>>>>>
>>>>>    ...
>>>>>
>>>>>
>>>>> These plots are later assembled into a Stack, like so:
>>>>>
>>>>>
>>>>>                 ImageStack plotStack = null;
>>>>>                 for(int m=0;m<nMeridians;m++)
>>>>>                     {
>>>>>                         plot[m].setSize(plotWidth, plotHeight);
>>>>>                         ImageProcessor plotImageProcessor = plot[m].getImagePlus().getProcessor();
>>>>>                         if(0==m)
>>>>>                             plotStack
>>>>>                                 = new ImageStack(plotImageProcessor.getWidth(),
>>>>> plotImageProcessor.getHeight());
>>>>> plotStack.addSlice(mLabel[m],plotImageProcessor);
>>>>>                     }
>>>>>                 ImageWindow.setNextLocation(plotLocationX, plotLocationY);
>>>>>                 ImageWindow plotImageWindow
>>>>>                     = new ImageWindow(
>>>>>                                       new ImagePlus("MPOD spatial distribution plots",plotStack));
>>>>>                 plotImageWindow.show();
>>>>>
>>>>>
>>>>> If I comment out the line marked "PROBLEM", then everything works as expected.  I get 9 different plots.  Only plot[0]
>>>>> has a vertical line at 1.0.  Everything is in BLACK.
>>>>>
>>>>> If I leave it as written above, several things happen:
>>>>>    a) there is a vertical line, in GREEN, at 1.0 (good!)
>>>>>    b) the main x,y values and the error bars are also in GREEN (bad)
>>>>>    c) all 9 plots are identical!  (very, very bad)
>>>>>
>>>>> I just ran tests with ONLY the line marked "PROBLEM" in and out. Nothing else changed.  I'm confused.
>>>>>
>>>>> Is there something obvious that I'm doing wrong?
>>>>>
>>>>> Eventually, I want to add color coding, and a few more vertical lines to mark special values.  I'm going to add the vertical lines, now (in BLACK), but I'd really like to color code them.  But..."setColor" seems to do something strange.
>>>>>
>>>>> Help, please!
>>>>>
>>>>> --
>>>>> Kenneth Sloan
>>>>> [hidden email]
>>>>> Vision is the art of seeing what is invisible to others.
>>>>>
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html