问题
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.

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.
回答1:
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):

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 infigure
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.
回答2:
try:
print('-painters','-dpng','myFile')
or
print('-painters','-dsvg','myFile')
Logic: from documentation appears that the model of rendering is changed from painter (what you intended) to OpenGL when saving the file. I suspect that OpenGL does not respect you intentions and forcing the renderer to painter will solve the problem. But it is not clear if painter rendering can be used for bitmap images and not only vector images, so first example can fail.
来源:https://stackoverflow.com/questions/30025595/moving-fill-background-to-the-bottom-after-saving-as-jpg