exporting patch:
# HG changeset patch
# User Daniel J Sebald <daniel.sebald@ieee.org>
# Date 1512515528 21600
#      Tue Dec 05 17:12:08 2017 -0600
# Node ID 0aafba67d11d761fb659af63877c48131f0e17b6
# Parent  222addbadf46f46805ea505171e2ea86243beec2
Simplify gnuplot toolkit scripts for image/non-image data plots (bug #52589).

* __gnuplot_draw_axes__.m: Drop the semicolon from the image data "with"
  clause and remove lines of code that subtract that semicolon later.  Move
  the test for removing background and foreground rectangles prior to all
  plot/splot commands and make it only dependent on any image present.
  Eliminate conditions in which the fprintf() is identical after the above
  changes.

diff --git a/scripts/plot/util/private/__gnuplot_draw_axes__.m b/scripts/plot/util/private/__gnuplot_draw_axes__.m
--- a/scripts/plot/util/private/__gnuplot_draw_axes__.m
+++ b/scripts/plot/util/private/__gnuplot_draw_axes__.m
@@ -607,7 +607,7 @@ function __gnuplot_draw_axes__ (h, plot_
         titlespec{data_idx} = "title \"\"";
         usingclause{data_idx} = sprintf ("binary array=%dx%d scan=yx origin=(%.15g,%.15g) dx=%.15g dy=%.15g using %s",
             x_dim, y_dim, x_origin, y_origin, dx, dy, format);
-        withclause{data_idx} = sprintf ("with %s;", imagetype);
+        withclause{data_idx} = sprintf ("with %s", imagetype);
 
       case "line"
         if (strcmp (get (obj.parent, "type"), "hggroup"))
@@ -1637,6 +1637,20 @@ function __gnuplot_draw_axes__ (h, plot_
   fputs (plot_stream, "unset colorbox;\n");
 
   if (have_data)
+    for i = 1:data_idx
+      % Images can be obscured by background or foreground image
+      if (is_image_data (i))
+        if (bg_is_set)
+          fputs (plot_stream, "if (GPVAL_TERM eq \"qt\") unset obj 1;\n");
+          bg_is_set = false;
+        endif
+        if (fg_is_set)
+          fputs (plot_stream, "unset obj 2; \\\n");
+          fg_is_set = false;
+        endif
+        break;
+      endif
+    endfor
     if (nd == 2)
       plot_cmd = "plot";
     else
@@ -1672,13 +1686,6 @@ function __gnuplot_draw_axes__ (h, plot_
       fprintf (plot_stream, "%s \"-\" %s %s %s \\\n", plot_cmd,
                usingclause{1}, titlespec{1}, withclause{1});
     elseif (is_image_data (1))
-      if (numel (is_image_data) > 1 && is_image_data(2))
-        ## Remove terminating semicolon
-        n = max (strfind (withclause{1}, ";"));
-        if (! isempty (n))
-          withclause{1} = withclause{1}(1:n-1);
-        endif
-      endif
       fprintf (plot_stream, "%s \"-\" %s %s %s \\\n", plot_cmd,
                usingclause{1}, titlespec{1}, withclause{1});
     else
@@ -1690,41 +1697,8 @@ function __gnuplot_draw_axes__ (h, plot_
         fprintf (plot_stream, ", \"-\" %s %s %s \\\n",
                  usingclause{i}, titlespec{i}, withclause{i});
       elseif (is_image_data (i))
-        if (! is_image_data (i-1))
-          fputs (plot_stream, "; ");
-          if (bg_is_set)
-            fputs (plot_stream, "if (GPVAL_TERM eq \"qt\") unset obj 1;\n");
-            bg_is_set = false;
-          endif
-          if (fg_is_set)
-            fputs (plot_stream, "unset obj 2; \\\n");
-            fg_is_set = false;
-          endif
-          if (numel (is_image_data) > i && is_image_data(i+1))
-            ## Remove terminating semicolon
-            n = max (strfind (withclause{i}, ";"));
-            if (! isempty (n))
-              withclause{i} = withclause{i}(1:n-1);
-            endif
-          endif
-          fprintf (plot_stream, "%s \"-\" %s %s %s \\\n", plot_cmd,
-                   usingclause{i}, titlespec{i}, withclause{i});
-        else
-          ## For consecutive images continue with the same plot command
-          fprintf (plot_stream, "%s \"-\" %s %s %s \\\n", ",",
-                   usingclause{i}, titlespec{i}, withclause{i});
-        endif
-      elseif (is_image_data (i-1))
-        if (bg_is_set)
-          fputs (plot_stream, "if (GPVAL_TERM eq \"qt\") unset obj 1;\n");
-          bg_is_set = false;
-        endif
-        if (fg_is_set)
-          fputs (plot_stream, "unset obj 2; \\\n");
-          fg_is_set = false;
-        endif
-        fprintf (plot_stream,"%s \"-\" binary format='%%float64' %s %s %s \\\n",
-                 plot_cmd, usingclause{i}, titlespec{i}, withclause{i});
+        fprintf (plot_stream, "%s \"-\" %s %s %s \\\n", ",",
+                 usingclause{i}, titlespec{i}, withclause{i});
       else
         fprintf (plot_stream, ", \"-\" binary format='%%float64' %s %s %s \\\n",
                  usingclause{i}, titlespec{i}, withclause{i});
