How to minimize the white space from boxchart in MATLAB? - matlab

I would like to minimize the whitespace of the figure (boxchart). I have tried to get a transparent background using built-in function exportgraphics():
https://it.mathworks.com/help/matlab/creating_plots/save-figure-with-minimal-white-space.html
But it doesn't work. Instead, it gives the following error:
Warning: Background transparency is not supported; using white instead.
I wonder if someone help me to remove the white space from the figure?
Thank you in advance.
clc; clear; close;
% data
D1= [0.944790802 0.557698361 0.373906779 2.046704837 2.301581619 1.527444403 1.209060357 0.255009648 0.006522648 1.391635043 0.020485623 3.06E-05 2.219570211 0.031292703 1.4617617 0.709825512 0.140968679 0.39152202 0.118959281 0.604265713 0.108340057 1.254792134 0.187867762 0.391214936 0.527749647 0.284452271 0.137486525 1.50698204 0.398872235 1.09E-05 6.030073741 0.343347524 2.66E-05 0.244522229 1.828567513 0.384888011 0.150066426 0.458832813 0.039189943 1.356957639 0.162054596 0.547751882 3.835735492 0.038394762 0.661566595 0.13553248 0.270139424 1.451613168 0.044339379 0.413170264 1.795609121 0.109564539 0.201333355 1.207746569 0.812164457 0.003902586 3.508953738 0.024859534 1.193314027 ]';
D2= [0.523944609 1.423099939 1.869691969 0.920446948 1.977619794 0.781737256 0.95977679 1.100075857 0.017899515 4.533151907 0.03790224 2.80E-05 3.432894823 0.120809324 2.359138901 1.530822669 0.24903375 3.017043435 0.357091919 1.775763839 0.067044341 2.383827733 0.496209584 1.016302313 2.17366118 0.718760335 0.316183758 1.662342228 0.658886019 1.57E-05 1.798207987 2.194711665 3.12E-05 0.777365181 1.31022147 1.803898883 0.142538755 1.633823624 0.095730818 1.917168272 1.066430117 1.570445025 0.67080804 0.028328869 0.265164055 1.313050736 0.988211581 1.355540089 0.180799139 1.559261863 2.765900536 0.275343017 0.828076679 1.749404233 1.276678701 0.003919396 4.740058349 0.059366823 1.978131529 ]';
D3 = [7.503974554 2.43552972 8.907981809 10.82563814 11.09197623 18.18846472 6.084768631 5.464296776 4.067161596 14.26678079 2.792424541 0.572435047 2.733751731 3.559015982 2.505767574 5.449188972 2.786168085 12.06056623 9.470572167 3.116171421 0.458594772 0.434307287 3.447147643 5.200432201 6.18563517 19.19753749 3.656691267 4.967834173 10.29317107 2.121656326 1.394653476 2.790630109 3.152455521 2.082149563 5.232312376 1.259368399 6.269558563 3.251263155 3.898594351 10.54554845 10.20258999 6.661139665 4.464739104 1.665067753 8.608715167 8.084037724 2.402608378 7.60967001 7.554079841 6.05757598 3.768961429 3.502224334 1.027846476 1.537041705 17.67476646 1.538015623 15.93866248 6.015687282 6.338130064 ]';
data = [D1 D2 D3];
% Requires R2019b or later
t = tiledlayout(2,2, 'Padding', 'none', 'TileSpacing', 'compact');
%box chart
b=boxchart(data);
b.BoxFaceColor = [0 0.4470 0.7410];
b.WhiskerLineColor = [0.17 0.17 0.17];
b.MarkerColor = [0 0.4470 0.7410];%black
b.JitterOutliers = 'on';
b.MarkerStyle = '.';
xlabel('x')
ylabel('y ')
title('Transparent background')
% Requires R2020a or later
exportgraphics(t,'boxPlot.jpg','BackgroundColor','none')

If you wish to maximize chart space, you may manipulate the axis location, as seen in the following code. If exporting transparency is also required, use the PDF format.
figure()
b=boxchart(data);
b.BoxFaceColor = [0 0.4470 0.7410];
b.WhiskerLineColor = [0.17 0.17 0.17];
b.MarkerColor = [0 0.4470 0.7410];%black
b.JitterOutliers = 'on';
b.MarkerStyle = '.';
xlabel('x')
ylabel('y ')
ax.Position=[0.07,0.1,0.92,0.9];

Related

why I get the error: Undefined function or variable 'shapes'

I'm working in matlab 2016.
I have a program code. It needs to work correctly.
Here is the program:
clear all;
addpath('..');
training_files = dir('cootes/*.bmp.mat');
for i=1:numel(training_files)
load(sprintf('cootes/%s', training_files(i).name));
app = imread(sprintf('cootes/%s', training_files(i).name(1:end-4)));
% Map RGB colors to [0,1]
appearances(:,:,:,i) = double(app) ./ 255;
shapes(:,:,i) = xy2ij(annotations, size(app,1));
end
load('cootes/triangulation.mat');
test_img = 2;
one_out = [1:test_img-1, test_img+1:size(shapes,3)];
AAM = build_model_2d(shapes(:,:,one_out), appearances(:,:,:,one_out), 'triangulation', triangulation);
fprintf('\n******************************************************* 2D FITTING *******************************************************\n\n');
disp 'Figure 1: leave-one-out fitting result (red mesh) using as intialization a random shape from the training set (blue mesh).'
disp 'Figure 2: reconstructed appearance.'
disp 'Usage: Hit a random key to use a different initialization shape. Use CTRL+C to quit.'
fprintf('\n');
while 1
init_shape = one_out(round(rand()*(numel(one_out) - 1) + 1));
try
[ fitted_shape fitted_app ] = fit_2d(AAM, shapes(:,:,init_shape) + repmat([-5 -5], [size(shapes, 1) 1 1]), appearances(:,:,:,test_img), 20);
figure(1)
imshow(appearances(:,:,:,test_img));
hold on;
triplot(AAM.shape_mesh, shapes(:,2,init_shape), shapes(:,1,init_shape), 'b');
triplot(AAM.shape_mesh, fitted_shape(:,2), fitted_shape(:,1), 'r');
hold off;
figure(2)
imshow(fitted_app);
pause;
catch ME
fprintf('Fitting diverged: %s\n', ME.message);
end
end
After starting the program appears the following error:
Undefined function or variable 'shapes'.
Error in annotate_test (line 29)
AAM = build_model_2d(shapes, appearances, 'triangulation', triangulation);
Сan anyone explain why the variable 'shapes' is not defined. Although clearly spelled out:
shapes(:,:,i) = xy2ij(annotations, size(app,1));
Please help, I'm newby and don't understand where can be the error.
training_files = dir('cootes/*.bmp.mat'); %error is here
try this.
training_files = dir(fullfile(mypath,'*.bmp.mat')); % should work if you have .bmp.mat files in that path

attaching customized colormap to geoshow in matlab

I am trying to plot a world map in mollweide projection using geoshow command. However, I am not able to modify the colors in the plot based on cutomized colormap values. Most likely this is an issue with how axesm and geoshow commands are used together, Please help me on this. See the ref. code below:
G = rand(180,360);
G(1:90,:)=-1*G(1:90,:);
R = georasterref('RasterSize',size(G),...
'Latlim',[-90 90], 'Lonlim', [-180 180],'ColumnsStartFrom','north');
% ref this link: http://stackoverflow.com/questions/34727526/matlab-plot-raster-map-with-custom-colormap/34740112#34740112
%setting up graphics parameters
my_colormap = [254 204 92
253 141 60
240 59 32
189 0 38]/255 ;
startval=min(min(G));
endval=max(max(G));
nElements =size(my_colormap,1);
stepSize=(endval-startval)/(nElements-1);
breaks = startval:stepSize:endval;
labels = textscan(num2str(round((breaks*100))/100),'%s') ;
labels = labels{1};
[~,indices] = histc(G,breaks);
%actual graphics
figure
hm=axesm ('mollweid', 'Frame', 'on', 'Grid', 'off');
geoshow(G,R);%geoshow(indices,R);
colormap(my_colormap);
set(gca,'color','none');
set(gca,'box','off','xtick',[],'xcolor','none')
set(gca,'box','off','ytick',[],'ycolor','none')
hc=colorbar('location','southoutside');
caxis([breaks(1) breaks(length(breaks))])%caxis([0 length(breaks)-1])
hcP = [.7 .28 .2 .02];
set(hc,'position',hcP);
h.YTickLabel = labels ;
one of my colleague pointed me to the solution and am posting it here for other's reference:
geoshow(G,R,'DisplayType','texturemap')

Box plot with Line in Matlab

I want to merge a line plot with 12 box plot. I can plot both of them in a figure. But the line plot position is not matching with Box plot. Please suggest. Figure is attached
Code:
clc
clear
[data header]=xlsread('Dry and Wet Spell Length.xlsx');
RCP26_JHT=(mean(data(1:4:16,:),1));
RCP26_QIN=mean(data(2:4:16,:),1);
RCP26_SCA=mean(data(3:4:16,:),1);
RCP26_WOL=mean(data(4:4:16,:),1);
RCP45_JHT=mean(data(18:4:33,:),1);
RCP45_QIN=mean(data(19:4:33,:),1);
RCP45_SCA=mean(data(20:4:33,:),1);
RCP45_WOL=mean(data(21:4:33,:),1);
RCP85_JHT=mean(data(35:4:50,:),1);
RCP85_QIN=mean(data(36:4:50,:),1);
RCP85_SCA=mean(data(37:4:50,:),1);
RCP85_WOL=mean(data(38:4:50,:),1);
ANUSPLIN_JHT=mean(data(52,1:23));
ANUSPLIN_QIN=mean(data(53,1:23));
ANUSPLIN_SCA=mean(data(54,1:23));
ANUSPLIN_WOL=mean(data(55,1:23));
AN_Data=repmat([ANUSPLIN_JHT,ANUSPLIN_QIN,ANUSPLIN_SCA,ANUSPLIN_WOL]',1,3);
B=reshape(AN_Data',1,12);
y=1:12;
p=plot(B,'k-s', 'MarkerFaceColor','k');
legend(p,'Obs','Location','north');
hold on
xlim = get(gca,'XLim');
positions=[1,1.25,1.5, 2,2.25,2.5 3 3.25 3.5, 4 4.25,4.5];
boxplot([RCP26_JHT',RCP45_JHT',RCP85_JHT',RCP26_QIN',RCP45_QIN',RCP85_QIN',RCP26_SCA',RCP45_SCA',RCP85_SCA', RCP26_WOL', RCP45_WOL',RCP85_WOL'],'positions', positions);
color = ['b','c','y','b','c','y','b','c','y','b','c','y'];
h = findobj(gca,'Tag','Box');
for j=1:length(h)
patch(get(h(j),'XData'),get(h(j),'YData'),color(j),'FaceAlpha',.5);
end
legend('Obs','RCP 8.5', 'RCP 4.5', 'RCP 2.6');
h1=findobj(gca,'tag','Outliers');
delete (h1);
set(gca,'xtick',[mean(positions(1:3)) mean(positions(4:6)) mean(positions(7:9)) mean(positions(10:12)) ]);
set(gca,'xticklabel',{'JHT','QIN','SCA','WOL'});
set(gca,'XLim',xlim);
hold off

How to draw a group by boxplot in matlab

I have a three groups such as
Group 1={x1,x2}
x1=[1,2,2,3,5,3]
x2=[2,5,4,5,8,6]
Group 2={x3,x4}
x3=[2,8,9,2,1,6]
x4=[5,4,3,22,11,6]
Group 2={x5,x6}
x5=[10,12,22,4]
x6=[12,15,4,25]
I want to draw them into char by boxplot function by group. I found the way to resolve it. But it cannot draw. Could you help me please?
x1=[1,2,2,3,5,3];
x2=[2,5,4,5,8,6];
g1={x1,x2};
%group2
x3=[2,8,9,2,1,6];
x4=[5,4,3,22,11,6];
g2={x3,x4};
%group3
x5=[10,12,22,4];
x6=[12,15,4,25];
g3={x5,x6};
G=cat(1,g1,g2,g3);
class={1,2,3}
positions = [1 1.25 2 2.25 3 3.25];
boxplot(G,class, 'positions', positions);
set(gca,'xtick',[mean(positions(1:2)) mean(positions(3:4)) mean(positions(5:6)) ])
set(gca,'xticklabel',{'Group1','Group2','Group3'})
color = ['c', 'y', 'c', 'y'];
h = findobj(gca,'Tag','Box');
for j=1:length(h)
patch(get(h(j),'XData'),get(h(j),'YData'),color(j),'FaceAlpha',.5);
end
c = get(gca, 'Children');
hleg1 = legend(c(1:2), 'Feature1', 'Feature2' );
Your problem is that the grouping variables were constructed in the wrong way. Use this sample:
x1=[1,2,2,3,5,3];
x2=[2,5,4,5,8,6];
g1=[x1,x2];
v1=[repmat('A',1,numel(x1)),repmat('B',1,numel(x2))];
%group2
x3=[2,8,9,2,1,6];
x4=[5,4,3,22,11,6];
g2=[x3,x4];
v2=[repmat('A',1,numel(x3)),repmat('B',1,numel(x4))];
%group3
x5=[10,12,22,4];
x6=[12,15,4,25];
g3=[x5,x6];
v3=[repmat('A',1,numel(x5)),repmat('B',1,numel(x6))];
%%
G=[g1,g2,g3]
vg1 = [repmat('1',1,numel(v1)),repmat('2',1,numel(v2)),repmat('3',1,numel(v3))];
vg2=[v1,v2,v3] ;
%%
clc
boxplot(G', {vg1';vg2'}, 'factorseparator',1 , 'factorgap',30,...
'colorgroup',vg2','labelverbosity','majorminor');

Candlestick-plot in Matlab and Octave

I want to write a script for a candlestick-plot that I can run in Octave as well as in Matlab.
So far I use the following code:
D=[ '15-Jul-2013'
'16-Jul-2013'
'17-Jul-2013'
'18-Jul-2013'
'19-Jul-2013'
'22-Jul-2013'
'23-Jul-2013'
'24-Jul-2013'];
O=[25.93 26.39 26.37 26.75 25.82 25.99 26.10 26.32];
H=[26.43 26.75 26.78 26.77 26.11 26.13 26.30 26.53];
L=[25.60 26.01 26.30 26.12 25.60 25.72 25.97 26.05];
C=[26.28 26.32 26.65 26.18 25.88 26.05 26.13 26.51];
datapoints=length(C);
hold on;
for i=1:datapoints
plot([i i],[L(i) H(i)],'linewidth',2,'Color','k');
if C(i)>O(i)
plot([i i],[O(i) C(i)],'linewidth',5,'color','r');
else
plot([i i],[O(i) C(i)],'linewidth',5,'color','g');
end
end
hold off;
grid on;
xlim([0 datapoints+1]);
y=get(gca,'ylim');
ymin=int16(y(1)-0.5);
ymax=int16(y(2)+0.5);
ylim([ymin ymax]);
XTick=zeros(1,length(datapoints));
j=1;
for i=1:1:datapoints
XTick(j)=i;
j=j+1;
end
set(gca,'XTick',XTick,'XTickLabel','')
pos = get(gca,'Position');
set(gca,'Position',[pos(1), .15, pos(3) .75])
for i=1:length(XTick)
hText = text(XTick(i), double(ymin), D(XTick(i),:));
set(hText,'Rotation',45,'HorizontalAlignment','right','VerticalAlignment','top');
end
The resulting plot looks quite nice in Matlab but looks very terrible in Octave. How can I make the plot looks nice in both programms?
The solution here was to use the fill function instead of plot.
The following code leads to similar results in octave and matlab independent of the plotting interface:
D=[ '15-Jul-2013'
'16-Jul-2013'
'17-Jul-2013'
'18-Jul-2013'
'19-Jul-2013'
'22-Jul-2013'
'23-Jul-2013'
'24-Jul-2013'];
O=[25.93 26.39 26.37 26.75 25.82 25.99 26.10 26.32];
H=[26.43 26.75 26.78 26.77 26.11 26.13 26.30 26.53];
L=[25.60 26.01 26.30 26.12 25.60 25.72 25.97 26.05];
C=[26.28 26.32 26.65 26.18 25.88 26.05 26.13 26.51];
colorDown = 'g';
colorUp = 'r';
colorLine = 'k';
date = (1:length(O))';
% w = Width of body, change multiplier to draw body thicker or thinner
% the 'min' ensures no errors on weekends ('time gap Fri. Mon.' > wanted
% spacing)
w=.1*min([(date(2)-date(1)) (date(3)-date(2))]);
%%%%%%%%%%%Find up and down days%%%%%%%%%%%%%%%%%%%
d=C-O;
l=length(d);
hold on
%%%%%%%%draw line from Low to High%%%%%%%%%%%%%%%%%
for i=1:l
line([date(i) date(i)],[L(i) H(i)],'Color',colorLine, 'linewidth',2)
end
%%%%%%%%%%draw white (or user defined) body (down day)%%%%%%%%%%%%%%%%%
n=find(d<0);
for i=1:length(n)
x=[date(n(i))-w date(n(i))-w date(n(i))+w date(n(i))+w date(n(i))-w];
y=[O(n(i)) C(n(i)) C(n(i)) O(n(i)) O(n(i))];
fill(x,y,colorDown)
end
%%%%%%%%%%draw black (or user defined) body(up day)%%%%%%%%%%%%%%%%%%%
n=find(d>=0);
for i=1:length(n)
x=[date(n(i))-w date(n(i))-w date(n(i))+w date(n(i))+w date(n(i))-w];
y=[O(n(i)) C(n(i)) C(n(i)) O(n(i)) O(n(i))];
fill(x,y,colorUp)
end
datapoints=length(C);
xlim([0 datapoints+1]);
y=get(gca,'ylim');
ymin=int16(y(1)-0.5);
ymax=int16(y(2)+0.5);
ylim([ymin ymax]);
XTick=zeros(1,length(datapoints));
j=1;
for i=1:2:datapoints
XTick(j)=i;
j=j+1;
end
set(gca,'XTick',XTick,'XTickLabel','')
pos = get(gca,'Position');
set(gca,'Position',[pos(1), .15, pos(3) .75])
for i=1:length(XTick)
hText = text(XTick(i), double(ymin), D(XTick(i),:));
set(hText,'Rotation',45,'HorizontalAlignment','right','VerticalAlignment','top');
end
hold off
Here's what I get with Octave 3.6.2 (I have used screen captures to illustrate what it actually looks like on the screen):
Graphics toolkit qt
Graphics toolkit gnuplot
Graphics toolkit fltk
The bodies of the candles are vertical in all 3 cases.