Matlab保存绘图时出现异常图形,求原因分析
Hey there! Let's figure out why your MATLAB plots are turning out wonky when you save them. Based on the code snippet you shared, here are the most likely causes and easy fixes to try:
Common Culprits & Fixes
Your save method might be the issue
Using the figure window's save button can lead to inconsistent results, especially with custom markers or fitted lines. Switch to theprintcommand—it's far more reliable for high-quality outputs. For example:% Save a high-res PNG (300 DPI) print(gcf, 'my_plot.png', '-dpng', '-r300'); % Save a vector PDF (great for papers/reports) print(gcf, 'my_plot.pdf', '-dpdf', '-vector');Graphics renderer glitches
MATLAB's default renderer (eitherpaintersoropengl) can sometimes mess up rendering for plots with markers and fitted lines. Try switching renderers before saving:% Use vector renderer for crisp lines/text set(gcf, 'Renderer', 'painters'); % Or use OpenGL if you have transparency/complex elements set(gcf, 'Renderer', 'opengl');Incomplete axis settings
Your code cuts off atset(gca, 'YTick',[0.48...—if you don't finish defining your y-axis ticks, MATLAB might render the axis incorrectly (think overlapping labels or weird ranges). Make sure to complete that line, e.g.:set(gca, 'YTick', [0.48, 0.5, 0.52]); % Add full tick values hereFont compatibility problems
You're usingCalibrias your font—if MATLAB can't find this font (common on non-Windows systems or if the font file is corrupted), it'll substitute a default font, which can cause text shifts or overlaps. Test with a system-native font likeArialto rule this out:set(gca,'FontName','Arial');Layer/hold state inconsistencies
The order of yourhold onand plot commands might be causing rendering layers to misbehave. Try restructuring your code to keep things clean:hold on; % Plot scatter points first plot(x,y,'o','MarkerSize',7,'MarkerFaceColor',[0.8 0.8 0.8],'MarkerEdgeColor',[0 0 0]); % Fit and plot the line with color set directly [myfit,gof,output] = fit(x,y,'poly1'); myfitplot = plot(myfit, 'Color','k'); % Set axis properties all in one go set(gca,'FontName','Calibri','FontSize',11,'XTick',[0.5,0.52,0.54],'YTick',[0.48, 0.5, 0.52]); hold off;
内容的提问来源于stack exchange,提问作者swagat patnaik

