Remove whitespace right to colorbar

7 次查看(过去 30 天)
I am trying to find a way to remove the whitespace that is right to the colorbar but it seems to be rather tricky. Any ideas?
Thanks!
close all;
clear all;
clc;
A = 1;
f = 50;
T = 1/f;
w = 2*pi*f;
x1 = linspace(0,T,1000)';
y1 = A.*sin(w.*x1);
x2 = 1:10;
y2 = rand(10);
f1 = figure(1);
yyaxis left
p1 = plot(x1,y1);
ylabel('Var 1')
yyaxis right
p2 = plot(x2,y2,'o');
ylabel('Var 2')
xlabel('X var')
c = colorbar;
c.Label.FontSize = 7;
c.Color = [0 0 0];
c.Ticks = [0:0.2:1];
c.Label.String = 'Density (normalised)';
ax = gca;
ax.FontName = 'Garamond';
ax.FontSize = 7;
set(gcf,'units','centimeters','position',[10 10 6.93 6.93/2])

采纳的回答

Dave B
Dave B 2021-11-19
编辑:Dave B 2021-11-19
Do you mean in an export or as displayed in the figure window?
  • For the export, if you use the newer exportgraphics instead of print, it'll just do this. This is maybe the best thing about exportgraphics compared to print/saveas!
  • For the figure window, you can get rid of most of the whitespace by using tiledlayout and nexttile (with a 1x1 layout) setting the Padding property to tight. It doesn't get rid of all of the whitespace but the majority of it.
Here's what I get with your code and: exportgraphics(gcf,'foo.png'). This is with your position set and default dpi, so it's low res. If you select it you'll see the image boundary runs tightly alongside the colorbar label.
And here's tiledlayout/nexttile
t=tiledlayout(1,1,'Padding','tight');
nexttile
A = 1;
f = 50;
T = 1/f;
w = 2*pi*f;
x1 = linspace(0,T,1000)';
y1 = A.*sin(w.*x1);
x2 = 1:10;
y2 = rand(10);
f1 = figure(1);
yyaxis left
p1 = plot(x1,y1);
ylabel('Var 1')
yyaxis right
p2 = plot(x2,y2,'o');
ylabel('Var 2')
xlabel('X var')
c = colorbar;
c.Label.FontSize = 7;
c.Color = [0 0 0];
c.Ticks = [0:0.2:1];
c.Label.String = 'Density (normalised)';
ax = gca;
ax.FontName = 'Garamond';
ax.FontSize = 7;
set(gcf,'units','centimeters','position',[10 10 6.93 6.93/2])
annotation('rectangle','Position',[0 0 1 1],'LineWidth',2) % for highlighting the figure boundary
  3 个评论
Dave B
Dave B 2021-11-19
It's really unfortunate the exportgraphics doesn't support svg. Sadly I can't think of a workaround other than to use another program to convert from a file format like .eps to .svg (I use adobe illustrator for this kind of thing but I suspect Ghostscript, which is free, might be able to do it). Sorry I don't have something better to offer!
MichailM
MichailM 2021-11-19
I tried inkscape but it also gets inconsistent when I import the .svg. The actual plots I am working on contain tens of thousands of points for the Var 2. It is a little be odd that Matlab does not maintain a fixed gap right to the colorbar for dfferent figure size. For example, the size I want is 6.93 x (6.93/2) and the output is the one below
When I scale up everything on the figure 4 times then I have this (I set the dispay size at 25%)
It is an option to export the file 4 times larger than the required size and then scale it down in Word/LaTeX. The drawback is that for the second case the exported svg file size is 1195kB while for the first 211kB (for the datasets I am actually working on).
Anyway, exportgraphics did the job for emf. Thank you very much again!
close all;
clear all;
clc;
A = 1;
f = 50;
T = 1/f;
w = 2*pi*f;
x1 = linspace(0,T,1000)';
y1 = A.*sin(w.*x1);
x2 = 1:10;
y2 = rand(10);
scale_factor = 4;
fs = scale_factor*7;
lw = (scale_factor/2)*5;
f1 = figure(1);
yyaxis left
p1 = plot(x1,y1);
ylabel('Var 1')
yyaxis right
p2 = plot(x2,y2,'o','MarkerSize',lw);
ylabel('Var 2')
xlabel('X var')
c = colorbar;
c.Label.FontSize = fs;
c.Color = [0 0 0];
c.Ticks = [0:0.2:1];
c.Label.String = 'Density (normalised)';
ax = gca;
ax.FontName = 'Garamond';
ax.FontSize = fs;
set(gcf,'units','centimeters','position',[10 10 scale_factor*6.93 scale_factor*6.93/2])
annotation('rectangle','Position',[0 0 1 1],'LineWidth',2)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Orange 的更多信息

产品


版本

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by