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
Related
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];
I am trying to apply a regularized optimization other than Lasso.
How can I plot the figure similar to the one produced by lassoPlot.m included in MATLAB as shown below if all data needed can be provide?
How to plot the I-shaped lines?
I read the lassoPlot.m but cannot find out how it is done.
If I had to create a plot like this manually, I'd do something like this:
function q53809665
DATASET = [
0.601240818 459.5714648 6.549320679
0.38951982 407.6789162 6.915203670
0.250128593 366.9277664 8.668936114
0.162048287 339.5657219 9.739510946
0.104984588 307.3415556 8.790018144
0.067415433 285.0615823 8.484338823
0.043675756 269.5982984 11.06798324
0.028295771 260.4386699 15.11267808
0.018170016 257.2895579 18.61737927
0.011771625 259.6377656 21.91891116
0.007626364 263.2320447 26.44502524
0.004897239 271.3708739 29.95587021
0.003172725 281.8307622 33.79278025
0.002055481 297.9101884 37.48077341
0.001319919 313.8919378 41.61931914
0.000855123 329.4338429 45.13891826
0.000554000 343.8029749 48.41955847
0.000355749 355.8266151 51.22206310
0.000230475 364.9182681 53.60367903
0.000149316 371.7376732 55.08113765
9.58825E-05 376.5047798 56.84605825
6.21184E-05 379.8791639 57.35674048
4.02440E-05 382.3739682 57.86464961];
figure();
hEB = errorbar(DATASET(:,1), DATASET(:,2), DATASET(:,3),'.', 'MarkerEdgeColor','r',...
'MarkerSize',10, 'Color', 0.7*[1 1 1]);
hEB.Parent.XDir = 'reverse';
hEB.Parent.XScale = 'log';
hold on;
plot(DATASET(9,1), DATASET(9,2), 'og', 'MarkerSize', 10);
plot(DATASET(7,1), DATASET(7,2), 'ob', 'MarkerSize', 10);
xline(DATASET(9,1), ':g');
xline(DATASET(7,1), ':b');
xlabel('Lambda');
title('Cross-Validated Devians of Lasso Fit');
legend('Deviance with Error Bars', 'LambdaMinDeviance','Lambda1SE');
Result:
I am creating a graph iteratively from various data sets, and I try to add legend to it, but it fails.
colors = {'g','b','m','k','c'};
subplot(2,3,iii);
hold all;
for jjj=1:length(aggregation_methods),
aggregate = all_aggregate{jjj};
std_agg = all_std_agg{jjj};
plot(coverages, aggregate, colors{jjj});
h1 = errorbar(coverages,aggregate,std_agg,colors{jjj});
set(h1,'linestyle','none')
end
plot(coverages, regular, 'r');
h1 = errorbar(coverages,regular,std_reg,'r');
set(h1,'linestyle','none')
title(datasets{iii});
xlabel('coverage');
ylabel('MSE');
legend('enhanced with clustering(k=4)','enhanced random split', 'regular we');
The graph seems to be generated just fine, it's just the legend that is failing.
I expect the colors of the legend to be (top-down): green, blue, red.
P.S. I also tried (and it didn't work):
legend({'enhanced with clustering(k=4)','enhanced random split', 'regular we'});
EDIT:
Inspired by this thread, I changed the code to use the 'DisplayName' property for each plot:
colors = {'g','b','m','k','c'};
names = {'enhanced with clustering(k=4)','enhanced random split', 'regular we'};
subplot(2,3,iii);
hold all;
for jjj=1:length(aggregation_methods),
aggregate = all_aggregate{jjj};
std_agg = all_std_agg{jjj};
plot(coverages, aggregate, colors{jjj}, 'DisplayName', names{jjj});
h1 = errorbar(coverages,aggregate,std_agg,colors{jjj});
set(h1,'linestyle','none')
end
%plot(coverages,aggregate,'g',coverages ,regular,'r');
%h1 = errorbar(coverages,aggregate,std_agg,'g');
%set(h1,'linestyle','none')
plot(coverages, regular, 'r', 'DisplayName', names{length(aggregation_methods) + 1});
h1 = errorbar(coverages,regular,std_reg,'r');
set(h1,'linestyle','none')
%axis([0 1 0 max(mean(rc{iii}))]);
title(datasets{iii});
xlabel('coverage');
ylabel('MSE');
legend('show');
However, it yields the following legend, but I'd like to get rid of these 'dataX' strings.
a=[
1 2 3
4 5 6
7 8 9
];
clist={'g','b','m'};
for i=1:3
pt(i)=plot(a(i,:),'Color',clist{i});
hold on;
end
b=[5 6 7];
pt(4)=plot(b,'Color','r');
legend([pt(1:2) pt(4)],{'line1','line2','line4'});
Im trying to plot trajectories on matlab
My data file (try.txt) that has the trajectories looks like this:
NumofTrajectories
TrajID1 #ofPoints x y x y....
TrajID2 #ofPoints x y x y....
example:
7
0 23 898.6 673.0 859.1 669.9 813.7 667.8 776.8 664.0 739.8 662.1 699.9 654.7 664.5 649.6 625.3 645.5 588.2 640.6 552.3 634.2 516.6 628.2 477.2 624.3 442.1 613.6 406.7 603.4 369.5 599.8 332.7 594.1 297.4 585.2 258.6 583.7 224.1 573.1 191.2 556.8 152.7 554.0 115.1 546.0 79.6 535.8
1 8 481.4 624.9 445.9 596.3 374.5 573.9 354.2 541.0 334.2 508.9 327.6 474.1 324.6 437.5 324.2 390.3
2 24 151.6 570.8 188.3 556.5 225.1 547.7 257.9 529.4 292.9 509.8 326.8 496.8 356.2 476.0 391.2 463.3 423.7 447.9 455.7 431.8 489.2 416.0 524.3 405.3 560.0 395.9 595.8 385.6 632.8 376.1 671.5 372.0 706.9 361.8 742.3 347.3 778.0 334.5 820.5 336.5 856.5 325.0 894.5 309.5 946.1 309.9 990.5 287.0
3 3 594.2 580.4 566.6 544.3 544.9 509.4
4 5 281.8 661.9 266.8 623.4 246.2 576.4 229.7 541.0 220.9 498.4
5 2 563.6 511.3 532.5 479.7
6 5 571.9 617.7 525.6 576.4 481.0 551.9 456.8 524.2 419.7 474.0
I'm trying to plot this on matlab
my code is as follows:
clc;
clear;
%read the input
importfile('try.txt')
%See how many trajectorys there are convert to number
nTraj=str2num(cell2mat(textdata));
%loop over the trajectories
for i = 1:nTraj
disp(data(i,1));
%print the current trajectory number of points
disp(data(i,2));
%get the x-y coordinates of each trajectory
current_traj=data(i,2);
for j=1:current_traj
points=data(i,3:j*2+2);
end
%print the x-y coordinates of each trajectory
%disp(points);
%seperate the x-y coordinates of each trajectory
x=points(1:2:length(points)-1)
y=points(2:2:length(points))
xlabel('latitude');
ylabel('longitude');
plot(x,y,'r');
grid on ;
hold on;
end
And function importfile:
function importfile(fileToRead1)
%IMPORTFILE(FILETOREAD1)
% Imports data from the specified file
% FILETOREAD1: file to read
DELIMITER = ' ';
HEADERLINES = 1;
% Import the file
newData1 = importdata(fileToRead1, DELIMITER, HEADERLINES);
% Create new variables in the base workspace from those fields.
vars = fieldnames(newData1);
for i = 1:length(vars)
assignin('base', vars{i}, newData1.(vars{i}));
end
The code sometimes works and usually gives me an error say:
Index exceeds matrix dimensions.
Error in ==> plotTrajectory at 23
points=data(i,3:j*2+2);
Can someone explain the error and tell me how to fix it?
The trajectories in try.txt have different lengths. importdata will use the first line to determine the length of your data. If another line is the longest, this line will be split over several lines of your imported matrix. At least this is what debugging shows. I would suggest you use another method to read your file. For me dlmread works:
%read the input
data = dlmread('try.txt', ' ');
% remove header
data = data(2:end,:);
%See how many trajectorys there are convert to number
nTraj=size(data,1);
You can replace your first lines with this code and remove your importfilefunction.
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.