Remove whitespace right to colorbar

8 views (last 30 days)
MichailM
MichailM on 19 Nov 2021
Commented: MichailM on 19 Nov 2021
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])

Accepted Answer

Dave B
Dave B on 19 Nov 2021
Edited: Dave B on 19 Nov 2021
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 Comments
Dave B
Dave B on 19 Nov 2021
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 on 19 Nov 2021
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)

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!