# HG changeset patch # User Ben Abbott # Date 1283275430 14400 # Node ID a72d53df4fa67555f91a13d2c0bad0471b49eafa # Parent b0f2b9a2b681f47825fb688b0176d4b0d5508df0 Treatment of activepositionproperty for gnuplot. diff -r b0f2b9a2b681 -r a72d53df4fa6 scripts/ChangeLog --- a/scripts/ChangeLog Tue Aug 31 12:39:04 2010 -0400 +++ b/scripts/ChangeLog Tue Aug 31 13:23:50 2010 -0400 @@ -1,5 +1,8 @@ 2010-08-31 Ben Abbott + * plot/private/__actual_axis_position__.m, plot/__go_draw_axes__.m, + plot/axis.m, plot/colorbar.m, plot/daspect.m, plot/plotyy.m, + plot/subplot.m: Treatment of activepositionproperty for gnuplot. * plot/__go_draw_axes__.m: Missing part of last changeset. * plot/__go_draw_axes__.m: Fix specification of pointtype for patches (bug introduced in rev 2f9de135e7f9). diff -r b0f2b9a2b681 -r a72d53df4fa6 scripts/plot/__go_draw_axes__.m --- a/scripts/plot/__go_draw_axes__.m Tue Aug 31 12:39:04 2010 -0400 +++ b/scripts/plot/__go_draw_axes__.m Tue Aug 31 13:23:50 2010 -0400 @@ -47,41 +47,101 @@ nd = __calc_dimensions__ (h); - if (strcmpi (axis_obj.plotboxaspectratiomode, "manual")) - pos = __actual_axis_position__ (h); - else - pos = axis_obj.position; + if (strcmp (axis_obj.dataaspectratiomode, "manual") + && strcmp (axis_obj.xlimmode, "manual") + && strcmp (axis_obj.ylimmode, "manual")) + ## All can't be "manual" + axis_obj.plotboxaspectratiomode = "auto"; endif - if (__gnuplot_has_feature__ ("screen_coordinates_for_{lrtb}margin")) + + if (strcmp (axis_obj.dataaspectratiomode, "manual") + && strcmp (axis_obj.xlimmode, "manual") + && strcmp (axis_obj.ylimmode, "manual") + && (nd == 2 || all (mod (axis_obj.view, 90) == 0))) + ## FIXME - adjust plotboxaspectratio to respect other + fpos = get (axis_obj.parent, "position"); + apos = axis_obj.position; + endif + + pos = __actual_axis_position__ (h); + + if (strcmpi (axis_obj.dataaspectratiomode, "manual")) + dr = axis_obj.dataaspectratio; if (nd == 2 || all (mod (axis_obj.view, 90) == 0)) - x = [1, 1]; + dr = dr(1) / dr(2); else - ## 3D plots need to be sized down to fit in the window. - x = 1.0 ./ sqrt([2, 2.5]); + ## FIXME - need to properly implement 3D + dr = mean (dr(1:2)) / dr(3); endif - fprintf (plot_stream, "set tmargin screen %.15g;\n", - pos(2)+pos(4)/2+x(2)*pos(4)/2); - fprintf (plot_stream, "set bmargin screen %.15g;\n", - pos(2)+pos(4)/2-x(2)*pos(4)/2); - fprintf (plot_stream, "set lmargin screen %.15g;\n", - pos(1)+pos(3)/2-x(1)*pos(3)/2); - fprintf (plot_stream, "set rmargin screen %.15g;\n", - pos(1)+pos(3)/2+x(1)*pos(3)/2); else - ## FIXME -- nothing should change for gnuplot 4.2.x. - fprintf (plot_stream, "set tmargin 0;\n"); - fprintf (plot_stream, "set bmargin 0;\n"); - fprintf (plot_stream, "set lmargin 0;\n"); - fprintf (plot_stream, "set rmargin 0;\n"); + dr = 1; + endif + + if (strcmp (axis_obj.activepositionproperty, "position")) + if (__gnuplot_has_feature__ ("screen_coordinates_for_{lrtb}margin")) + if (nd == 2 || all (mod (axis_obj.view, 90) == 0)) + x = [1, 1]; + else + ## 3D plots need to be sized down to fit in the window. + x = 1.0 ./ sqrt([2, 2.5]); + endif + fprintf (plot_stream, "set tmargin screen %.15g;\n", + pos(2)+pos(4)/2+x(2)*pos(4)/2); + fprintf (plot_stream, "set bmargin screen %.15g;\n", + pos(2)+pos(4)/2-x(2)*pos(4)/2); + fprintf (plot_stream, "set lmargin screen %.15g;\n", + pos(1)+pos(3)/2-x(1)*pos(3)/2); + fprintf (plot_stream, "set rmargin screen %.15g;\n", + pos(1)+pos(3)/2+x(1)*pos(3)/2); + sz_str = ""; + else + fprintf (plot_stream, "set tmargin 0;\n"); + fprintf (plot_stream, "set bmargin 0;\n"); + fprintf (plot_stream, "set lmargin 0;\n"); + fprintf (plot_stream, "set rmargin 0;\n"); - fprintf (plot_stream, "set origin %.15g, %.15g;\n", pos(1), pos(2)); - fprintf (plot_stream, "set size %.15g, %.15g;\n", pos(3), pos(4)); - + if (nd == 3 && all (axis_obj.view == [0, 90])) + ## FIXME -- Kludge to allow colorbar to be added to a pcolor() plot + pos(3:4) = pos(3:4) * 1.4; + pos(1:2) = pos(1:2) - pos(3:4) * 0.125; + endif + + fprintf (plot_stream, "set origin %.15g, %.15g;\n", pos(1), pos(2)); + + if (strcmpi (axis_obj.dataaspectratiomode, "manual")) + sz_str = sprintf ("set size ratio %.15g", -dr); + else + sz_str = "set size noratio"; + endif + sz_str = sprintf ("%s %.15g, %.15g;\n", sz_str, pos(3), pos(4)); + endif + else ## activepositionproperty == outerposition + fprintf (plot_stream, "set origin %g, %g;\n", pos(1:2)) + sz_str = ""; if (strcmpi (axis_obj.dataaspectratiomode, "manual")) - r = axis_obj.dataaspectratio; - fprintf (plot_stream, "set size ratio %.15g;\n", -r(2)/r(1)); + sz_str = sprintf ("ratio %g", -dr); else - fputs (plot_stream, "set size noratio;\n"); + sz_str = "noratio"; + endif + sz_str = sprintf ("set size %s %g, %g;\n", sz_str, pos(3:4)); + endif + if (! isempty (sz_str)) + fputs (plot_stream, sz_str); + endif + + if (strcmp (axis_obj.plotboxaspectratiomode, "manual") + && strcmp (axis_obj.dataaspectratiomode, "manual")) + if (nd == 2 || all (mod (axis_obj.view, 90) == 0)) + dy = diff (axis_obj.ylim); + dx = diff (axis_obj.xlim); + ar = dx / dy; + if (ar > dr) + axis_obj.ylim = mean (axis_obj.ylim) + (ar/dr) * dy * [-1, 1] / 2; + elseif (ar < dr) + axis_obj.xlim = mean (axis_obj.xlim) + (dr/ar) * dx * [-1, 1] / 2; + endif + else + ## FIXME - need to implement 3D endif endif diff -r b0f2b9a2b681 -r a72d53df4fa6 scripts/plot/axis.m --- a/scripts/plot/axis.m Tue Aug 31 12:39:04 2010 -0400 +++ b/scripts/plot/axis.m Tue Aug 31 13:23:50 2010 -0400 @@ -184,6 +184,10 @@ elseif (strcmp (ax, "equal")) x = xlim; y = ylim; + if (strcmp (get (get (ca, "parent"), "__backend__"), "gnuplot")) + ## FIXME - gnuplot applies the aspect ratio activepostionproperty. + set (ca, "activepositionproperty", "position"); + endif set (ca, "plotboxaspectratio", [(x(2)-x(1)), (y(2)-y(1)), 1]); elseif (strcmpi (ax, "normal")) set (ca, "plotboxaspectratio", [1, 1, 1]) diff -r b0f2b9a2b681 -r a72d53df4fa6 scripts/plot/colorbar.m --- a/scripts/plot/colorbar.m Tue Aug 31 12:39:04 2010 -0400 +++ b/scripts/plot/colorbar.m Tue Aug 31 13:23:50 2010 -0400 @@ -106,6 +106,10 @@ end_unwind_protect if (! deleting) + ## FIXME - Matlab does not require the "position" property to be active. + ## Is there a way to determine the plotbox position for the gnuplot + ## backend with the outerposition is active? + set (ax, "activepositionproperty", "position"); obj = get (ax); obj.__my_handle__ = ax; position = obj.position; @@ -117,7 +121,7 @@ [pos, cpos, vertical, mirror] = ... __position_colorbox__ (loc, obj, ancestor (ax, "figure")); - set (ax, "activepositionproperty", "position", "position", pos); + set (ax, "position", pos); cax = __go_axes__ (get (ax, "parent"), "tag", "colorbar", "handlevisibility", "on", @@ -182,7 +186,8 @@ if (!isempty (ancestor (h, "axes")) && strcmp (get (ancestor (h, "axes"), "beingdeleted"), "off")) set (ancestor (h, "axes"), "position", orig_props.position, ... - "outerposition", orig_props.outerposition); + "outerposition", orig_props.outerposition, ... + "activepositionproperty", orig_props.activepositionproperty); endif endif endfunction @@ -192,7 +197,8 @@ (isempty (gcbf()) || strcmp (get (gcbf(), "beingdeleted"),"off")) && ishandle (get (h, "axes"))) set (get (h, "axes"), "position", orig_props.position, ... - "outerposition", orig_props.outerposition); + "outerposition", orig_props.outerposition, ... + "activepositionproperty", orig_props.activepositionproperty); endif endfunction @@ -265,8 +271,13 @@ else scale = [scale, 1]; endif - obj.position = obj.position .* [1, 1, scale]; - off = 0.5 * (obj.position (3:4) - __actual_axis_position__ (obj)(3:4)); + if (strcmp (obj.activepositionproperty, "position")) + obj.position = obj.position .* [1, 1, scale]; + off = 0.5 * (obj.position (3:4) - __actual_axis_position__ (obj)(3:4)); + else + obj.outerposition = obj.outerposition .* [1, 1, scale]; + off = 0.5 * (obj.outerposition (3:4) - __actual_axis_position__ (obj)(3:4)); + endif else off = 0.0; endif @@ -581,6 +592,7 @@ %! shading ("interp") %! axis ("tight", "square") %! colorbar () +#%! axes('color','none','box','on','activepositionproperty','position') %!demo %! clf diff -r b0f2b9a2b681 -r a72d53df4fa6 scripts/plot/daspect.m --- a/scripts/plot/daspect.m Tue Aug 31 12:39:04 2010 -0400 +++ b/scripts/plot/daspect.m Tue Aug 31 13:23:50 2010 -0400 @@ -88,7 +88,7 @@ %! plot (x, cos (x), x, sin (x)) %! axis square %! daspect ([1 1 1]) -%! title ("axis limits should be [0, 4, -2, 2]") +%! title ("square plot-box with axis limits [0, 4, -2, 2]") %!demo %! x = 0:0.01:4; @@ -96,5 +96,23 @@ %! plot (x, cos (x), x, sin (x)) %! axis ([0 4 -1 1]) %! daspect ([2 1 1]) -%! title ("axis box should be square") +%! title ("square plot-box with axis limits [0, 4, -1, 1]") +%!demo +%! x = 0:0.01:4; +%! clf +%! plot (x, cos (x), x, sin (x)) +%! axis square +%! set (gca, "activepositionproperty", "position") +%! daspect ([1 1 1]) +%! title ("square plot-box with axis limits [0, 4, -2, 2]") + +%!demo +%! x = 0:0.01:4; +%! clf +%! plot (x, cos (x), x, sin (x)) +%! axis ([0 4 -1 1]) +%! set (gca, "activepositionproperty", "position") +%! daspect ([2 1 1]) +%! title ("square plot-box with axis limits [0, 4, -1, 1]") + diff -r b0f2b9a2b681 -r a72d53df4fa6 scripts/plot/plotyy.m --- a/scripts/plot/plotyy.m Tue Aug 31 12:39:04 2010 -0400 +++ b/scripts/plot/plotyy.m Tue Aug 31 13:23:50 2010 -0400 @@ -113,6 +113,8 @@ endif end_unwind_protect + set (ax, "activepositionproperty", "position") + if (nargout > 0) Ax = ax; H1 = h1; diff -r b0f2b9a2b681 -r a72d53df4fa6 scripts/plot/private/__actual_axis_position__.m --- a/scripts/plot/private/__actual_axis_position__.m Tue Aug 31 12:39:04 2010 -0400 +++ b/scripts/plot/private/__actual_axis_position__.m Tue Aug 31 13:23:50 2010 -0400 @@ -33,23 +33,48 @@ h = axis_obj.__my_handle__; endif - ## When using {rltb}margin, Gnuplot does not handle the specified - ## aspect ratio properly, so handle it here. - if (__calc_dimensions__ (h) == 2 - || all (mod (axis_obj.view, 90) == 0)) - aspect_ratio_2d = axis_obj.plotboxaspectratio(1:2); - else - ## FIXME -- this works for "axis square", but has not been - ## thoroughly tested for other aspect ratios. - aspect_ratio_2d = [max(axis_obj.plotboxaspectratio(1:2)), ... - axis_obj.plotboxaspectratio(3)/sqrt(2)]; - endif + ## Get figure size in pixels orig_fig_units = get (axis_obj.parent, "units"); orig_fig_position = get (axis_obj.parent, "position"); unwind_protect set (axis_obj.parent, "units", "pixels") fig_position = get (axis_obj.parent, "position"); + unwind_protect_cleanup + set (axis_obj.parent, "units", orig_fig_units) + set (axis_obj.parent, "position", orig_fig_position) + end_unwind_protect + ## Get axes size in pixels + if (strcmp (axis_obj.activepositionproperty, "position")) pos_in_pixels = axis_obj.position .* fig_position([3, 4, 3, 4]); + else + pos_in_pixels = axis_obj.outerposition .* fig_position([3, 4, 3, 4]); + endif + + nd = __calc_dimensions__ (h); + + if (strcmp (axis_obj.plotboxaspectratiomode, "auto") + && strcmp (axis_obj.dataaspectratiomode, "manual") + && strcmp (axis_obj.xlimmode, "manual") + && strcmp (axis_obj.ylimmode, "manual") + && (nd == 2 || all (mod (axis_obj.view, 90) == 0))) + ## Force plotboxaspectrato to a manual value + dx_dy_dz = [diff(axis_obj.xlim), diff(axis_obj.ylim), diff(axis_obj.zlim)]; + dx_dy_dz = dx_dy_dz / min (dx_dy_dz(dx_dy_dz>0)); + axis_obj.plotboxaspectratiomode = "manual"; + axis_obj.plotboxaspectratio = dx_dy_dz ./ axis_obj.dataaspectratio; + endif + + if (strcmp (axis_obj.plotboxaspectratiomode, "manual")) + ## When using {rltb}margin, Gnuplot does not handle the specified + ## aspect ratio properly, so handle it here. + if (nd == 2 || all (mod (axis_obj.view, 90) == 0)) + aspect_ratio_2d = axis_obj.plotboxaspectratio(1:2); + else + ## FIXME -- this works for "axis square", but has not been + ## thoroughly tested for other aspect ratios. + aspect_ratio_2d = [max(axis_obj.plotboxaspectratio(1:2)), ... + axis_obj.plotboxaspectratio(3)/sqrt(2)]; + endif orig_aspect_ratio_2d = pos_in_pixels(3:4); rel_aspect_ratio_2d = aspect_ratio_2d ./ orig_aspect_ratio_2d; rel_aspect_ratio_2d = rel_aspect_ratio_2d ./ max (rel_aspect_ratio_2d); @@ -61,10 +86,10 @@ pos_in_pixels = pos_in_pixels + dy*[0.0, 0.5, 0.0, -1.0]; endif pos = pos_in_pixels ./ fig_position([3, 4, 3, 4]); - unwind_protect_cleanup - set (axis_obj.parent, "units", orig_fig_units) - set (axis_obj.parent, "position", orig_fig_position) - end_unwind_protect - + elseif (strcmp (axis_obj.activepositionproperty, "position")) + pos = axis_obj.position; + else + pos = axis_obj.outerposition; + endif endfunction diff -r b0f2b9a2b681 -r a72d53df4fa6 scripts/plot/subplot.m --- a/scripts/plot/subplot.m Tue Aug 31 12:39:04 2010 -0400 +++ b/scripts/plot/subplot.m Tue Aug 31 13:23:50 2010 -0400 @@ -151,7 +151,9 @@ else pos = subplot_position (rows, columns, index, "outerposition", units); pos2 = subplot_position (rows, columns, index, "position", units); - tmp = axes ("outerposition", pos, "position", pos2); + tmp = axes ("outerposition", pos, + "position", pos2, + "activepositionproperty", "position"); endif unwind_protect_cleanup