I have this script to obtain subplot with bar, but I would rather that the bars were side by side and with two y axes. How can I do? I have in this case the bars one above the other.
The result is shown in figure below.
% Create figure
figure1 = figure('NumberTitle','off','Name','Figure','Color',[1 1 1]);
%bar plot e scatter 2011
subplot1=subplot(3,2,1,'Parent',figure1)
x1 = (1:5)';
y1 = tweetsvsnewsS3.somma_1_2;
y2 = tweetsvsnewsS3.tweets_1;
yyaxis left
p1 = bar(x1,y1, 'BarWidth', 0.25);
p1(1).FaceColor = [0.56 0.10 0.74];
yyaxis right
p12=bar(x1,y2,'BarWidth', 0.25)
p12(1).FaceColor = [0.41 0.28 0.79];
box(subplot1,'on');
hold(subplot1,'off');
set(subplot1,'XGrid','on','XMinorGrid','on','XMinorTick','on','YGrid','on',...
'YMinorGrid','on','YMinorTick','on');
set(gca,'xtick',1:12,...
'xticklabel',{'Nov 24','Nov 25','Nov 26','Nov 27','Nov 28'})
ylabel('count');
title('Data','FontSize',12)
I tried also this script but I didn't have the second y axis. In figure there is the result.
% Create figure
figure1 = figure('NumberTitle','off','Name','Figure','Color',[1 1 1]);
%bar plot e scatter 2011
subplot1=subplot(3,2,1,'Parent',figure1)
x1 = (1:5)';
y1 = tweetsvsnewsS3.somma_1_2;
y2 = tweetsvsnewsS3.tweets_1;
p1 = bar(x1,[y1,y2],'BarWidth', 0.80);
p1(1).FaceColor = [0.56 0.10 0.74];
p1(2).FaceColor = [0.41 0.28 0.79];
box(subplot1,'on');
hold(subplot1,'off');
set(subplot1,'XGrid','on','XMinorGrid','on','XMinorTick','on','YGrid','on',...
'YMinorGrid','on','YMinorTick','on');
set(gca,'xtick',1:12,...
'xticklabel',{'Nov 24','Nov 25','Nov 26','Nov 27','Nov 28'})
ylabel('count');
title('Data','FontSize',12)
Your first script is the right one. You can have the bar plotted side by side if you set empty data in each dataset so that we can see the data behind. Note that you must also set the facecolor at the right position in your dataset.
[...]
yyaxis left
p1 = bar(x1,[y1 zeros(size(y1))]);
p1(1).FaceColor = [0.56 0.10 0.74];
yyaxis right
p12=bar(x1,[zeros(size(y2)) y2]);
p12(2).FaceColor = [0.41 0.28 0.79];
[...]
Related
As shown in figure, how to make curve 1 to touch X-axis with slope made by last bottom two points?
Code to generate above plot
X1 = linspace(2,3,20);
Y1 = linspace(1/1000, 1,20);
Y1Dash = flip(Y1);
X2 = linspace(14,15,30);
Y2 = linspace(1/10000, 1,30);
Y2Dash = flip(Y2);
figure;semilogy(X1, Y1Dash, "-r*");hold on;grid on;
semilogy(X2, Y2Dash, "-r*");hold off;
legend("curve1", "Curve2");
Description of the program is as below:
I take 3 coordinates from a file and I drew a triangle
I want to plot a grid and if the grid points are in a triangle, I want to plot a black circle, otherwise a red circle.
The method I used for checking if the point is inside a triangle is, if the point (xco,yco) is inside the triangle, the sum of areas of small triangles it makes with three other points is equal to the area of the triangle.
So my if statement is if the total area = area of triangle -> plot black circle, otherwise red circle.
The problem is, even though some points made the Total area to be equal to the area of the triangle plot black circle isn't plotted and red circle is plotted instead.
It does seem random and I can't figure out this simple problem.
So could you help me with the plotting the the points?
figure()
% Loading the data from .mat file
A = load('triangle_a.mat','pt1');
B = load('triangle_a.mat','pt2');
C = load('triangle_a.mat','pt3');
% Assigning values of array from .mat into each variable
x1 = A.pt1(1,1);
y1 = A.pt1(1,2);
x2 = B.pt2(1,1);
y2 = B.pt2(1,2);
x3 = C.pt3(1,1);
y3 = C.pt3(1,2);
% Drawing coordinates of a triangle on a grid
plot(x1, y1,'or');
hold on
plot(x2, y2,'or');
hold on
plot(x3, y3,'or');
hold on
% Joining three coordinates to make a triangle
plot ([x1,x2],[y1,y2],'-b');
plot ([x1,x3],[y1,y3],'-b');
plot ([x3,x2],[y3,y2],'-b');
xmin = A_coor(1,1);
xmax = B_coor(1,1);
ymin = A_coor(1,2);
ymax = C_coor(1,2);
xgrid = xmin-1:0.5:xmax+1;
ygrid = ymin-1:0.5:ymax+1;
tri_x = [x1 x2 x3];
tri_y = [y1 y2 y3];
area = polyarea(tri_x,tri_y);
% Making a grid
for x = 1:1:numel(xgrid)
for y = 1:1:numel(ygrid)
xco = xgrid(1,x);
yco = ygrid(1,y);
aa = [xco, x2, x3];
bb = [yco, y2, y3];
cc = [x1, xco, x3];
dd = [y1, yco, y3];
ee = [x1,x2,xco];
ff = [y1,y2,yco];
area1 = polyarea(aa,bb);
area2 = polyarea(cc,dd);
area3 = polyarea(ee,ff);
totarea = area1 + area2 + area3;
if totarea == area
plot(xco,yco,'ok');
else
plot(xco,yco,'.r');
end
end
end
My code worked after I changed the condition of if statement for making a grid section. (Thanks to Hoki for suggestion)
Before
if totarea == area
After
if abs(area-totarea)<0.002;)
You can determine whether a point is inside an arbitrary polygon by using the MATLAB function inpolygon.
I'm trying to plot a few waveforms onto the same plot in Matlab; brand new to it. I've tried plotting them all together with plot() but it doesn't scale them all appropriately. How would I go about scaling them? I read something online that uses hold on, but I'm getting the same issue I believe. What's the simple solution here?
t1 = 0:0.1:1000;
y1 = t1.^5-5*t1.^3+4*t1;
plot(t1, y1)
hold on
t2 = 0:0.0001:0.01;
y2 = -8*exp(-1000*t2) + 3;
plot(t2, y2)
hold on
t3 = 0:0.0001:0.6;
y3 = exp(-10*t3).*cos(100*t3);
plot(t3, y3)
hold on
%plot(t1, y1, t2, y2, t3, y3)
Matlab is doing what you ask it to do: plotting everything to the same axis system (by the way, you only need to use hold on once, it is active until you change the axis or command hold off)
You have three options
define the limits of the axis explicitly, either separately using xlim([xmin xmax]) and ylim([ymin ymax]) or jointly using axis([xmin xmax ymin ymax]) (in your case e.g. axis([0 0.6 0 3.3])
you may want to use separate axis in one plot, see yyaxis yyaxis left / yyaxis right to activate the axes. Note that this only provides two different axes scales
use subplots! (as Cris Luengo already stated)
That would be in your case
% 1st subplot
t1 = 0:0.1:1000;
y1 = t1.^5-5*t1.^3+4*t1;
ax(1) = subplot(3,1,1);
plot(t1, y1)
% 2nd subplot
t2 = 0:0.0001:0.01;
y2 = -8*exp(-1000*t2) + 3;
ax(2) = subplot(3,1,2);
plot(t2, y2)
% 3rd subplot
t3 = 0:0.0001:0.6;
y3 = exp(-10*t3).*cos(100*t3);
ax(3) = subplot(3,1,3);
plot(t3, y3)
% link all axes (but only x direction)
linkaxes(ax,'x')
% your axes limits are so large that you probably don't want to link them
I am plotting two arrays in one plot. The bar plot must show two y-axis as well as the bars next to each other. The problem occurs when I want to implement both requirements.
I can either plot the bars together with for example
Y = [5,2; 8,7; 9,8; 5,5; 4,3];
figure
bar(Y)
Or I can create two y-axis (which I do currently with my data):
y = [lr_flights2018, lr_income2018];
yyaxis left
b = bar(1:length(y),lr_flights2018);
ylabel('Life Rating/flights ratio')
yyaxis right
p = bar(1:length(y),lr_income2018);
ylabel('Life Rating/income ratio')
set(gca, 'XTick', 1:length(y))
set(gca,'XTickLabel',{countries{:,1}})
xtickangle(90)
title('Correlations with life rating');
In the latter yyaxis separates the plots which results in the two plots stacked together. I want the plots to stand side by side for each bin as can be seen in
this example.
You can do this by manipulating the x position and the bar width
I manipulated your Y data as shown below, and made some new labels for the countries which you didn't provide in your example
figure;
Y = [5,2; 8,7; 9,8; 5,5; 4,3];;
lr_flights2018 = Y(:,1);
lr_income2018 = Y(:,2);
y = [lr_flights2018, lr_income2018];
yyaxis left
b = bar((1:length(y))+0.125,lr_flights2018, 'barwidth', 0.25);
ylabel('Life Rating/flights ratio')
yyaxis right
p = bar((1:length(y))-0.125,lr_income2018, 'barwidth', 0.25);
ylabel('Life Rating/income ratio')
set(gca, 'XTick', 1:length(y))
str = strread ( sprintf ( '%i\n', [1:5] ), '%s', 'delimiter', '\n' )
set(gca,'XTickLabel',str)
xtickangle(90)
title('Correlations with life rating');
I know there is a lot of similar questions on Stack-Overflow but I didn't find the one that I am specifically interested so I am going to ask again.
I am creating a graph where a line plot overlays with a stacked bar plot. I am NOT using plotyy. I am following the instruction provided here by creating a second axis system: http://www.mathworks.com/help/matlab/ref/plotyy.html
Basically, I plot my first data set, get the location of current axis, create a second axis at the same location, move y-axis to the right, x-axis to the top, and plot my second/third data set based on the new axis.
My data set is basically:
x
y1 (axis 1),y2,y3 (axis 2)
While I was able to plot y1,y2,y3 at two axis using all line style, I cannot get it to work with y2 and y3 being bar style. The second axis is somehow stuck with the first axis, instead moving to top-right. Also the first data set line just disappears.
Another small question I also have is how to remove the x-axis for the second axis (since they are essentially the same). I searched online and they said to set xtick to []. But I am getting error: Invalid or deleted object with command
set(ax1,'YTick',[])
Thank you very much.
As pointed out I don't have code uploaded, here you go ;)
% this script predicts
% user prompt
prompt = {'Stock Name:','Cost per share($):','Current Value ($):','Holdings (shares):','Est. High ($)','Tolerance ($):'};
user_input = inputdlg(prompt);
% process user input
if isempty(user_input)
stockname = 'APPLE.INC';
x0 = 125.82;
xn = 129.91;
N0 = 80;
xt = 135;
tol = 20;
else
[stockname,x0,xn,N0,xt,tol] = user_input{:};
x0 = str2num(x0);
xn = str2num(xn);
xt = str2num(xt);
N0 = str2num(N0);
tol = str2num(tol);
end
% calculate sale-rebuy threshold
xt = linspace(x0-tol,xt+tol,10);
[x0,xn,y,N0,Ny] = sale_rebuy(x0,xn,xt,N0);
profit_rebuy = Ny.*(xt-y);
profit_nosale = N0*(xt-x0);
% plotting
figure
line(xt,y,'Color','r','LineStyle','--');
ax1=gca;
set(ax1,'XColor','r');
set(ax1,'YColor','r');
ax1_pos = get(ax1,'Position');
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none');
profit = [profit_rebuy;profit_nosale]';
%bar(ax2,xt,profit,'stacked');
line(xt,profit_rebuy,'Parent',ax2,'Color','k','LineStyle',':');
title(stockname);
xlabel(ax1,'final price');
xlabel(ax2,'final price');
ylabel(ax1,'rebuy price');
ylabel(ax2,'profit');
% This is the function
function [x0,xn,y,N0,Ny] = sale_rebuy(x0,xn,xt,N0)
y = (xn.*xt)./(xt-x0+xn);
Ny = xn.*N0./y;
x0 = x0;
xn = xn;
N0 = N0;
end