Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving fill background to the bottom after saving as jpg

When I use fill or viscircles functions to plot circles with background to the plot, in figure it appears on the top of the plot, as it was intended, but after saving as jpg or png, the background moves to the bottom of the plot and is not visible anymore.

enter image description here

How do I fix this?

note: it's not because white is transparent color. I tried gray, I tried red, both are behaving the same as white.

like image 852
user50222 Avatar asked Oct 20 '22 12:10

user50222


1 Answers

Please consider the following example (made on MATLAB 2015a):

figure(); h=cell(1);
%% viscircles method:
subplot(1,2,1);
plot([0 1],[0,1]); set(gca,'Color',0.8*[1 1 1]); axis square;
h{1} = viscircles([0.5,0.5], 0.1,'EdgeColor','k','DrawBackgroundCircle',false);
get(h{1},'Children')

%//  Line with properties:
%//
%//              Color: [0 0 0]
%//          LineStyle: '-'
%//          LineWidth: 2
%//             Marker: 'none'
%//         MarkerSize: 6
%//    MarkerFaceColor: 'none'
%//              XData: [1x182 double]
%//              YData: [1x182 double]
%//              ZData: [1x0 double]

%% Annotation method:
subplot(1,2,2);
plot([0 1],[0,1]); set(gca,'Color',0.8*[1 1 1]); axis square;
pos = get(gca,'position');
h{2} = annotation('ellipse',[pos(1)+pos(3)*0.5 pos(2)+pos(4)*0.5 0.1 0.1],...
                  'FaceColor','w')
%//  Ellipse with properties:
%//
%//        Color: [0 0 0]
%//    FaceColor: [1 1 1]
%//    LineStyle: '-'
%//    LineWidth: 0.5000
%//     Position: [0.7377 0.5175 0.1000 0.1000]
%//        Units: 'normalized'

When saving the output to .jpg I get the following result (which is identical to the preview within the figure):

Comparison of two circle-drawing methods

Notice the FaceColor property in the 2nd code cell, which is absent in the 1st object. The problem seems to be that the viscircles function you used is not supposed to draw a shape with a background, rather, it draws a line. It is unclear to me why you see the preview the way you do (i.e. with a background).

Sidenote: the 'DrawBackgroundCircle' option for viscircles merely draws some white background for the outline.

You should try some alternate ways to plot filled circles, such as:

  • Using annotation objects as in my example above. Note that these require you to provide coordinates in figure units (and not axes'!).
  • Using filled polygons:

    N=20; c=[0.5,0.5]; r=0.1; color = [1 1 1];
    t = 2*pi/N*(1:N);
    hold all; fill(c(1)+r*cos(t), c(2)+r*sin(t), color);
    

    [ snippet is based on a comment of this submission ]

  • Plotting with giant markers:

    plot(0.5,0.5,'.r','MarkerSize',70);
    

    or

    scatter(0.5,0.5,70,'r','filled');
    

    [ snippet is based on this answer ]. As mentioned in the link, the downside here is that marker size doesn't change with zoom.

  • As a very last resort, you could save the image in some vectoric format (such as .emf), edit it (with something like InkScape), bring the background circles to a higher layer and export it.
like image 91
Dev-iL Avatar answered Oct 22 '22 22:10

Dev-iL