Bar graph starting from zero - matlab

I have a vector with 11 elements. I want to display the graph as in this picture:
but my graph starts from first month. How can I make it start from the zeroth month?

You can use both an x and y input argument for bar.
ax = axes();
x = 0:11;
bar(x,y);
If that doesn't give you the plot you want you can also use the Xlim, XTick, and XTickLabel properties of to control how the x axis looks.
set(ax, 'Xlim', [0,11])
set(ax, 'XTick', [0:11])

Related

Semilogx plot issues

Quite simply, both plots are semilogx, although the range of x values isn't 10^0, 10^1, etc, as is on the bottom plot (which is what I desire), and the grid isn't what I am looking for. I need to be able to see the x intercept, which is why I need the x axis to look like that of the bottom graph but I don't know how to do it.
this is the desired graph, but I don't know how to achieve it in matlab. It's been a very long time since I've used this software
Here is the code for starters...
Exposure = [53.19, 79.79, 132.98, 212.77, 455.93];
Cure_depth = [10.88, 14.56, 19.19, 23.45, 30.36];
semilogx(Exposure,Cure_depth, '-*')
grid on
You could use the stairs function with XSale set to log.
Example:
figure;
ax = gca;
stairs(ax, Exposure, Cure_depth, '-');
set(ax, 'XScale', 'log');
grid(ax, 'on');
hold(ax, 'on');
plot(ax, Exposure, Cure_depth, '-*');
xlabel(ax, 'Exposure (mJ/cm^2)');
title(ax, 'Cure Depth vs. Exposure');
ylabel(ax, 'Cure Depth (mils)');

How to set xticklables to certain places in the graph in matlab

I have two vectors that contain the average temperatures of two cities. I am plotting those vectors to a graph. I have 398 temperatures in both vectors so my xticklables are naturally from 1 to 398. I am trying to change my xticklables so they would show the month and the year like this: 01/2017. however I seem to be unable to write anything to my xtick. Here is my code for xtickmodification:
ax.XTick=[1,32,62,93,124,154,185,215,246,277,305,336,366,397];
ax.XTicklabels={'05/2014','06/2014','07/2014','08/2014','09/2014',
'10/2014','11/2014','12/2014','01/2015','02/2015','03/2015','04/2015','05/2015','06/2015'};
ax.XTick contains the vector element that starts a new month. So how can i get the ax.XTicklabels-line to my graph?
use XTickLabel property instead of XTicklabels. in addition, you can set XTickLabelRotation property if the labels are long strings:
x = rand(398,1);
h = plot(x);
ax = gca;
ax.XTick=[1,32,62,93,124,154,185,215,246,277,305,336,366,397];
ax.XTickLabel={'05/2014','06/2014','07/2014','08/2014','09/2014',...
'10/2014','11/2014','12/2014','01/2015','02/2015','03/2015','04/2015','05/2015','06/2015'};
ax.XTickLabelRotation = 45;
Have you tried the following
set(gca, 'xtick', ax.XTick);
set(gca, 'xticklabels', ax.XTicklabels);

Why is the actual value of an axis position different than the derived value in MATLAB suplot figure?

I've got a figure with two subplots being displayed. I want to get the axis position of each plot so I am using the figure's children information:
subplot(211)
% Plot stuff here
...
subplot(212)
% Plot stuff here
...
% Get Axes Position information
f =gcf;
% Bottom Plot Axis
disp(f.Children(2).Position)
% Top Plot Axis
disp(f.Children(4).Position)
The console displays:
Bottom plot:
0.1300 0.1100 0.7750 0.3412
Top plot:
0.1300 0.5838 0.7750 0.3412
This seems correct given all else. When I run the plot to generate the figure and then inspect the axes' information via 'Show Plot Tools and Dock Figure->Inspector: matlab.graphics.axis.Axes' option the width value for each axis's position is not 0.7750. The other position values are as listed but the width is different for both the top and bottom plots. The top becomes [0.13 0.5838 0.682 0.341] and the bottom becomes [0.13 0.11 0.67 0.341].
Does anyone know why this is and how to get the "real" position values and not the incorrect displayed/printed ones?
Useful info: MATLAB R2014b
EDIT UPDATE:
Here is a MWE that produces the behavior.
clear all; close all; clc;
figure;
subplot(211)
hold on
x1 = [ -1 -0.998 -0.996 -0.994 -0.992];
y = [0.000324249267578125 -0.000370025634765625 -3.4332275390625e-005 -0.000186920166015625 -0.000110626220703125];
plot(x1, y, '-', 'MarkerSize', 10)
set(gca, 'FontName', 'Interstate-Light', 'FontSize', 7)
set(gca, 'XTickLabel', [])
set(gca, 'XLabel', [])
grid on
ylabel('Frequency Error (Hz)', 'FontName', 'Interstate-Bold')
legend({'FE'}, 'Location', 'NorthEastOutside', 'FontName', 'Interstate-Light', 'Box', 'off')
subplot(212)
hold on
x1 = [ -1 -0.998 -0.996 -0.994 -0.992];
y = [-0.010614013299346 -0.0417663566768169 0.0235949698835611 -0.0502893067896366 0.0316442884504795];
plot(x1, y, '-', 'MarkerSize', 10)
% Overide X-axis ticks to align with data
XTick = unique(x1);
grid on;
xlabel('Time (s)', 'FontName', 'Interstate-Bold')
ylabel('Frequency Error (Hz)', 'FontName', 'Interstate-Bold')
set(gca, 'FontName', 'Interstate-Light', 'FontSize', 7)
legend({'RFE'}, 'Location', 'NorthEastOutside', 'FontName', 'Interstate-Light', 'Box', 'off')
% Get figure object
f = gcf;
% Set fontsize to 16 for readability in final pdf
set(findall(f, '-property','FontSize'),'FontSize',16)
f.PaperUnits = 'inches';
f.PaperPosition = [0 0 20 14];
f.PaperPositionMode = 'manual';
% Get axis position info
disp('Second plot:')
disp(f.Children(2).Position)
disp('First Plot:')
disp(f.Children(4).Position)
I'm working with R2012b and I've been able to reproduce the problem and also (almost) been able to understand what's happening.
I do not know if it is also applicable to more "recent" versin of MatLab.
The problem seems related to the default configuration of the window Show Plot Tools and Dock Figure
With this configuration, actually the position of the two axes (read in the Property editor are different with respect to the "original ones:
original pos ax 1= [0.1300 0.5838 0.7750 0.3412]
new pos ax1=[0.1300 0.629 0.7750 0.268 ]
Nevertheless, if you minimize the Property editor tab and look at the Position of the two axes, they are restored to the "original" one values.
You can, the "slide up" the tab and the position still remain the "original onne.
I'do not know if this is actually an answer to you question; what I can say is that the right position position are the "original" ones.
It could be a sort of bug in the visualization in the default configuratin od the Show Plot Tools and Dock Figure window.
Hope this helps.

MATLAB: how to customize non linear X axis (for example ticks at 1,2,3,4,5,20,100,'string')

I'm using the MATLAB plot feature to compare two vectors. I would like my X axis to represent 1 through 7, and then 14, 21, and then a category at the end for points with undetermined X values..(I'm also not sure how to represent these numberless point (they have Y values, just no X values) I could assign a large number outside any of my X values (1000) to these points, do the 1-7,14,21,1000 and then change the 1000 label to my 'string for un-numbered points'. ??
Here is a way to do it based on the example by The Mathworks here.
The trick is to create 2 x axis with different ranges and make one of them transparent. You can then play around with its properties. I modified a bit their code but kept most of their comments because they explain well the steps.
For the demo I used scatter to represent the points and colored the "good" points in red and the other point (here only 1) in green. You can customize all this of course. For instance I used a value of 100 and not 1000 for the x value of the 2nd axes, but I'll let you figure out how to modify it all to change the output as you wish.
clear
clc
close all
%// Create axes 1 and get its position
hAxes1 = axes;
axes_position = get(hAxes1, 'Position');
%// Create axes 2 and place it at the same position than axes 1
hAxes2 = axes('Position', axes_position);
%// Your data go here.
x = [1 7 14 21 100];
y = rand(1, length(x));
%// Plot the two sections of data on different axes objects
hPlot1 = scatter(hAxes1, x(1:4), y(1:4),40,'r','filled');
hold on
hPlot2 = scatter(hAxes2, x(end), y(end),40,'g','filled');
%// Link the y axis limits and fontsize property of the axes objects
linkaxes([hAxes1 hAxes2], 'y');
linkprop([hAxes1 hAxes2], 'FontSize');
%// Set the x range limits and tick mark positions of the first axes object
set(hAxes1, 'XLim', [1 25], ...
'XTick', [1 7 14 21])
%// Set the x range limits and tick mark positions for the second axes object.
%// Also set the background color to 'none', which makes the background
%// transparent.Add the label for the "super points".
set(hAxes2, 'Color', 'none', ...
'YTickLabel', [], ...
'XLim', [95 100], ...
'XTick', 100,'XTickLabel',{'Super Points'})
And the output:
EDIT
If you wish to add a fit line, I suggest adding a 3rd axes at the same position than the other 2, make it transparent and remove its X- and Y-ticks.
i.e. Add something like this:
hAxes3 = axes('Position', axes_position,'Color','none','YTick',[],'XTick',[]);
And in the call to polyfit/polyval, in my example you want to get only the first 4 elements (i.e. the red ones).
Hence:
p = polyfit(x(1:4),y(1:4),1);
y_poly = polyval(p,x(1:4));
And then plot the line on hAxes3:
hPlot3 = plot(x(1:4),y_poly)
Output:

How can I combine multipe x-axis while using the same y-axis?

I am trying to plot the wake velocity deficient behind an object at different streamwise positions (pos.1, 2 and 3) behind the object.
A rough sketch of how the graph should look like is shown below. The x-axis represents velocity and the y-axis is the coordinate normal to the flow.
How can I restart the x-axis such that the data of each position is plotted in it's own space, resembling it's actual position in the flow.
Easiest solution I think is to create all plots you need in horizontally arranged subplots, and then "beautify" according to your level of perfectionism ^_^
The "beautifications" I did in this case are:
For the axes in each subplot, switch the 'box' option off
Set the ytick to [] and color the y-axes white, of every y-axis except the leftmost one
create another axes object in the background without any axes labels, so that it appears that the subplots are really one plot
Here's the code:
%// Some bogus data
y = 0:0.1:4*pi;
x1 = sin(y);
x2 = sin(3*y);
x3 = sin(2*y).*sin(5*y);
%// Initialize figure window
figure(1), clf, hold on
%// Plot each plot on its own axes
subplot(1,3,3), hold on
plot(x3,y)
set(gca,...
'ytick' , [],...
'ycolor', 'w',...
'box' , 'off');
subplot(1,3,2)
plot(x2,y)
set(gca,...
'ytick' , [],...
'ycolor', 'w',...
'box' , 'off')
subplot(1,3,1)
plot(x1,y)
set(gca,...
'box', 'off') %// NOTE: don't kill these axes
%// Background axes
P = axes('parent', 1, 'xtick', [], 'ytick', []);
uistack(P, 'down', 10)
You could consider plotting your data on a single x-axis with an offset and changing the label of the x-axis ticks.
Consider your x-vector to be x_pos1 for the first position, your second and third would be similar but with an addition of an offset. E.g. offset = 15; x_pos2 = x_pos1+offset;
You can obtain and change your x-axis tick label by:
get(gca, 'xticklabel')
set(gca, 'xticklabel', yourLabelHere)