Candlestick-plot in Matlab and Octave - matlab

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.

Related

How to minimize the white space from boxchart in 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];

Duplicate Equation in MatLab ODE45 Solve

I'm trying to write a MatLab code which can extend the number of ODE's to solve for simply. This is the code that I currently have, for simplicity I am starting with a spring mass damper system.
clear;clc;close all;
tspan=[0:0.01:1];
x0=[1;0;-1;0;7;0;-7;0;5;0;-5;0;10;0;-10;0];
[t,x] = ode45(#Spring_Mass_Damper,tspan,x0);
figure(1)
plot(t,x(:,1));
hold on
plot(t,x(:,3));
hold on
plot(t,x(:,5));
hold on
plot(t,x(:,7));
hold on
plot(t,x(:,9));
hold on
plot(t,x(:,11));
hold on
plot(t,x(:,13));
hold on
plot(t,x(:,15));
grid on
xlabel('Time')
ylabel('Displacement(x)')
title('Displacement Vs Time')
function xp = Spring_Mass_Damper(t,x)
c1=10;
G=9.81;
m1=1;
k1=2000;
xp=[[x(2)];(-(c1/m1).*[x(2)]-(k1/m1).*[x(1)])-(G.*m1);
[x(4)];(-(c1/m1).*[x(4)]-(k1/m1).*[x(3)])-(G.*m1);
[x(6)];(-(c1/m1).*[x(6)]-(k1/m1).*[x(5)])-(G.*m1);
[x(8)];(-(c1/m1).*[x(8)]-(k1/m1).*[x(7)])-(G.*m1);
[x(10)];(-(c1/m1).*[x(10)]-(k1/m1).*[x(9)])-(G.*m1);
[x(12)];(-(c1/m1).*[x(12)]-(k1/m1).*[x(11)])-(G.*m1);
[x(14)];(-(c1/m1).*[x(14)]-(k1/m1).*[x(13)])-(G.*m1);
[x(16)];(-(c1/m1).*[x(16)]-(k1/m1).*[x(15)])-(G.*m1)];
end
The Main question I have regards this area of code:
xp=[[x(2)];(-(c1/m1).*[x(2)]-(k1/m1).*[x(1)])-(G.*m1);
[x(4)];(-(c1/m1).*[x(4)]-(k1/m1).*[x(3)])-(G.*m1);
[x(6)];(-(c1/m1).*[x(6)]-(k1/m1).*[x(5)])-(G.*m1);
[x(8)];(-(c1/m1).*[x(8)]-(k1/m1).*[x(7)])-(G.*m1);
[x(10)];(-(c1/m1).*[x(10)]-(k1/m1).*[x(9)])-(G.*m1);
[x(12)];(-(c1/m1).*[x(12)]-(k1/m1).*[x(11)])-(G.*m1);
[x(14)];(-(c1/m1).*[x(14)]-(k1/m1).*[x(13)])-(G.*m1);
[x(16)];(-(c1/m1).*[x(16)]-(k1/m1).*[x(15)])-(G.*m1)];
is there a simple way in which i can achieve the same number of systems(ODE's) without having to copy and paste the first equation in the block and manually change the x indexing?
This should also work:
xp = zeros(size(x,1),1)
for idx=1:size(x,1)
if mod(idx,2) == 0
xp(idx) = (-(c1/m1).*[x(idx)]-(k1/m1).*[x(idx-1)])-(G.*m1);
else
xp(idx) = x(idx);
end
end
The plotting routine can be simplified in a similar manner, like this:
figure(1)
hold on
for idx=1:2:size(x,2)
plot(t,x(:,idx));
end
grid on
xlabel('Time')
ylabel('Displacement(x)')
title('Displacement Vs Time')
Ifx length is an even number, then the code below could be a substitute to xp
xp = zeros(length(x), 1);
xp(1:2:end-1) = x(2:2:end);
xp(2:2:end) = -(c1/m1).*(x(2:2:end))-(k1/m1).*(x(1:2:end-1))-G.*m1;

How to plot a figure similar to the one produced by lassoPlot.m to specify a regularization parameter?

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:

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

Set hold on for some parts of a figure, not others

Below is my matlab code that plots a black line as it rotates around the origin. For the most part the code works, but because I use 'hold on', once I plot a line, it stays there for the entirety of the program.
What I'd like is something more like a second hand on a clock (only moving counter clockwise). For a particular value of i, the black line prints, and after i increments, that previous line is gone. However, I would like the red and blue lines and the plotted text to stay on the plot from iteration to iteration. I tried using the set() command and couldn't get it working, any ideas?
scalar = 3;
a = [3/scalar 2/scalar];
b = [-4/scalar .5/scalar];
starts = zeros(2,2);
ends = [a;b];
hold on
quiver(0,0,a(1),a(2),'color',[0 0 1],'linewidth',3);
quiver(0,0,b(1),b(2),'color',[1 0 0],'linewidth',3);
axisSize = 15/scalar;
axis([-axisSize axisSize -axisSize axisSize])
mintheta = [500, 500];
%waitforbuttonpress
for i=1:360
theta = [cos(2*pi()*i/360) sin(2*pi()*i/360)];
minDot = min(a*theta',-1*(b*theta'));
theta = theta/minDot;
hold on
if (minDot > 0)
if (norm(theta) < norm(mintheta))
mintheta = theta;
end
text(-4.5, 4, 'Drawing all Theta vectors,');
text(-4.5, 3.7, 'according to constraint that');
text(-4.5, 3.4, 'all dotProducts >= 1');
text(-4.5, 2.5, 'Yellow indicates a non-separating plane,');
text(-4.5, 2.2, 'SVM looks for shortest possible');
text(-4.5, 1.9, 'Theta (will be shown in green).');
quiver(0,0,theta(1),theta(2),'color',[0 0 0],'linewidth',5);
quiver(0,0,3/scalar,2/scalar,'color',[0 0 1],'linewidth',3);
quiver(0,0,-4/scalar,.5/scalar,'color',[1 0 0],'linewidth',3);
axis([-axisSize axisSize -axisSize axisSize])
else
quiver(0,0,cos(2*pi()*i/360),sin(2*pi()*i/360),'color',[1 1 0],'linewidth',5);
end
pause(.05);
end
quiver(0,0,mintheta(1),mintheta(2),'color',[0 1 0],'linewidth',3);
This should get you started:
% set up axis
axisSize = 5;
axis([-axisSize axisSize -axisSize axisSize])
cla
hold on
% draw quiver at initial position
q = quiver(0,0,cos(2*pi()*0/360),sin(2*pi()*0/360),'color',[0 0 0],'linewidth',5);
for i = 1:360
% modify data inside quiver plot
set(q,...
'xdata',0,...
'ydata',0,...
'udata',cos(2*pi()*i/360),...
'vdata',sin(2*pi()*i/360))
pause(.05);
end
Also, I would put the text outside the loop. It only needs to be created once, and MATLAB doesn't render stacks of identical text very nicely for some reason.