Plotting ticks and custom grid lines at the same time - matlab

I am trying to make a figure in Matlab that has grid lines at some custom places, but I also want to write ticks at regular intervals. Currently I produced the following graph, with the grid lines in the right position:
plot(myData);
xlabel('Frequency');
ylabel('Maginute');
set(gca, 'XTick', listOfTheoreticalValues);
set(gca,'XGrid', 'on');
set(gca, 'XTickLabel', '');
The problem I am facing now, however, is that I can't put normal, equally spaced ticks on the x-axis, let alone with marking values, because that would immediately add extra grid lines too. Is there a way to separate the two things from each other?

As Hugh Nolan suggested, manually adding grid lines is one way to solve the problem. The following code will do the trick:
%Grid line locations
x_lines = listOfTheoreticalValues;
y_limits = [lower_y_limit; upper_y_limit]; %Insert desired y-limits here
y_grid = repmat(y_limits, 1, numel(x_lines));
x_grid = [x_lines; x_lines];
plot(x_grid, y_grid, ':', 'color', [1,1,1]/2); %First plot grid lines
hold on
plot(myData); %Then plot data to draw data on top of grid lines
xlabel('Frequency');
ylabel('Maginute');

Related

MATLAB: Plot multiple lines with different color intensity per line

I want to plot a series of lines, with each line being colored according to a certain weight. I stumbled accross this, but the problem is, I am plotting multiple things on one graph, where only one of the plot statements should be color weighted.
I'm using hold on; hold off;, but I only see the last graph. Thus, the final result is, I only see the color weighted plot statements, but not the other plot statements that should be plotted due to hold on; hold off;.
Here is my code, where upper*p is an n x 1 vector representing the intensity of each of the n lines:
cmap = colormap;
con_min = min(upper*p)*25;
con_max = max(upper*p)*25;
ind_upper = ceil((size(cmap,1)-1)*((con_min:con_max)'-con_min+1)/(con_max-con_min));
subplot(2,3,2);
hold on;
title('Gains');
set(gca,'ColorOrder',cmap(ind_upper,:),'NextPlot','replacechildren');
plot(flat','c');
plot(lower','c');
plot(upper');
set(gca, 'xtick',1:labelFreq:num_tickers, 'xticklabel', tickers(1:4:num_tickers));
ylabel('bp change on day');
colorbar;
caxis([con_min con_max]);
hold off;
It seems the "upper" part is plotted correctly according to the color weights, but the "flat" and "lower" graphs do not show. I think it's because the set(gca,...) statement is causing the graphs to reset.

Plot with multiple axes but only one legend

The following code produces a plot with two lines reffering to the left y-axis and one line referring to the right y-axis. Both plots have their own legend, but I want only one legend listing all 3 lines. I tried to just put the 'y1','y2' strings into the 2nd legend-command as well, but that didn't work out.
line(x,y1,'b','LineWidth',2)
line(x,y2,'Color',[0,0.6,0.5],'LineWidth',2)
legend('y1','y2');
ax1 = gca;
ax2 = axes('Position',ax1.Position,'YAxisLocation','right', 'Color','none','YColor',[255,127,80]/256);
linkaxes([ax1,ax2],'x');
line(x,y3,'Parent',ax2,'LineWidth',2,'Color',[255,127,80]/256)
legend('y3')
This is a tricky problem since the legend are somehow connected to the axes. Since you will be creating 2 axes, hence there will be 2 legends. However there is a trick to achieve what you want. Firstly, plot all line on the same axes, then run legend. Then create 2nd axes and then move the third line to the 2nd axes. So your code should look like this:
% line
line(x,y1,'b','LineWidth',2)
line(x,y2,'Color',[0,0.6,0.5],'LineWidth',2)
l3=line(x,y3,'LineWidth',2,'Color',[255,127,80]/256)
% legend
legend('y1','y2','y3');
% 2nd axes
ax1 = gca;
ax2 = axes('Position',ax1.Position,'YAxisLocation','right', 'Color','none','YColor',[255,127,80]/256);
linkaxes([ax1,ax2],'x');
% move l3 to 2nd axes
set(l3,'Parent',ax2);
If you want to use 'DisplayName', it meant to be used with line
line(x,y1,'Color','b','LineWidth',2,'DisplayName','y1');
line(x,y2,'Color',[0,0.6,0.5],'LineWidth',2,'DisplayName','y2');
legend('show');

Individual line and axes styles with plotyy

I tried creating a plot with two YAxis like this:
x=linspace(0,20);
y1=linspace(10,10);
y2=x.^2;
y3=y2-y1;
[hAx,hLine1,hLine2]=plotyy([x',x'],[y1',y2'],x,y3);
Now i have two problem with this code:
I can change the Linestyles of the two hLines using hLine1.LineStyle = ':'; for example, but i can not change the styles of the two lines, that hLine1 consists of. Does anyone know how to do this?
I can't use hLine2.YLim = [0 100] to manually adjust the y-limits shown on the 2nd y-axis.
After I couldn't solve the problem using the plotyy, I searched the MATLAB documentation and found another way of implementing my plot, which I thought might be easier to handle:
x=linspace(0,20);
y1=linspace(10,10);
y2=x.^2;
y3=y2-y1;
figure
hold on;
line(x,y1,'Color','r')
line(x,y2,'Color','y')
ax1 = gca;
ax2 = axes('Position',ax1.Position,'YAxisLocation','right');
line(x,y3,'Parent',ax2,'Color','b')
The problem here is, that it doesn't even show the first and the second line, but only the third and i don't know why. I would prefer getting the problem solved using the plotyy, but if that's not possible I would appreciate a solution for the 2nd piece of code as well.
I think you haven't noticed that the outputs of plotyy are arrays of objects, and not single objects.
x=linspace(0,20);
y1=linspace(10,10);
y2=x.^2;
y3=y2-y1;
[hAx,hLine1,hLine2]=plotyy([x',x'],[y1',y2'],x,y3);
hLine1(1).LineStyle = '--';
hLine1(2).LineStyle = ':';
% either this
ylim( hAx(2), [0 110] );
% or alternatively
f=gcf; ylim( f.Children(2), [0 110] );
You're not seeing the first two lines because axes backgrounds are white by default. Setting the Color property of the second axes object to 'none' should give you what you're looking for:
x=linspace(0,20);
y1=linspace(10,10);
y2=x.^2;
y3=y2-y1;
figure
hold on;
line(x,y1,'Color','r')
line(x,y2,'Color','y')
ax1 = gca;
ax2 = axes('Position',ax1.Position,'YAxisLocation','right', 'Color', 'none');
line(x,y3,'Parent',ax2,'Color','b')
EDIT: I'd also recommend checking out linkaxes if you're going to be zooming/panning your axes and want to keep some or all of the axes synchronized.

MATLAB: Plotting Time on Xaxis - overlapping label

I am having difficulties on plotting time on the xaxis. I have some overlapping labels. See below:
This is my code:
time=datenum(0,0,0,0,0,timeinseconds);
labs=1:10:length(time);
figure(3);
plotyy(time,xvalue,time,dens);
datetick('x','HH:MM');
set(gca,'XTick',time(labs),'XTickLabel',time(labs));
legend('xval','CDF');
title('Crash on Oct.10 2008 at 15:59pm');
xlabel('Time');
First, why are the labels overlapping with the old ones? And secondly, how I can get the label to rotate 90degrees? I tried some other matlab functions to turn the labels but none seem able to tackle time format labels.
Calling plotyy you create two axis objects. Your overlap problem does probably come from the fact that you modify only one set of those axis, while leaving the other as it was originally set up.
One option is handling both of the created axis when you call plotyy by:
[AX, H1, H2] = plotyy( time, xvalue, time, dens);
Your first option here is setting up both of the axis, contained within the array of handlers AX, via changing the'XTick' propriety as:
set( AX(1), 'XTick', time(labs), 'XTickLabel', time(labs));
set( AX(2), 'XTick', time(labs), 'XTickLabel', time(labs));
But you also have the option of leaving the labels for the second axis empty, replacing the second line above:
set( AX(1), 'XTick', time(labs), 'XTickLabel', time(labs));
set( AX(2), 'XTick', time(labs), 'XTickLabel', []);
The official documentation of plotyy and Using Multiple X- and Y-Axes can be of further help for you.
If you take a look on the example there, namely, plotyy documentation:
figure
x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);
[AX,H1,H2] = plotyy(x,y1,x,y2,'plot');
and as you did before, try to modify only the AX(2), which is equivalent to what you got writing gca:
set(AX(2),'XtickLabel',1:0.1:20)
you will observe that the same overlapping error takes place.
With respect to rotating the labels 90 deg, I'm afraid that's not currently supported by Matlab. However, you can probably get that done using one of the available packages on FileExchange. Either xticklabelrotate or Rotate Tick Label could be the one.

How to make 1-D plots in MATLAB?

How can I make plots in MATLAB like in below?
I won't need labels, so you can ignore them. I tried using normal 2D plot, by giving 0 to y parameter for each data points. It does help, but most of the plot remains empty/white and I don't want that.
How can I solve this problem?
Edit:
This is how I plot(playing with values of ylim does not help):
hold on
for i=1:120
if genders(v_labels(i)) == CLASS_WOMAN
plot(v_images_lda(i,:) * w_lda,0,'r*');
else
plot(v_images_lda(i,:) * w_lda,0,'b.');
end
end
title('LDA 1D Plot');
ylim([-0.2 0.2]);
hold off
One way to do this would be to adjust the 'XLim', 'YLim', and 'DataAspectRatio' properties of the axes so that it renders as essentially a single line. Here's an example:
data1 = rand(1,20)./2; %# Sample data set 1
data2 = 0.3+rand(1,20)./2; %# Sample data set 2
hAxes = axes('NextPlot','add',... %# Add subsequent plots to the axes,
'DataAspectRatio',[1 1 1],... %# match the scaling of each axis,
'XLim',[0 1],... %# set the x axis limit,
'YLim',[0 eps],... %# set the y axis limit (tiny!),
'Color','none'); %# and don't use a background color
plot(data1,0,'r*','MarkerSize',10); %# Plot data set 1
plot(data2,0,'b.','MarkerSize',10); %# Plot data set 2
And you will get the following plot:
Here's one way to reproduce your figure using dsxy2figxy and annotate. dsxy2figxy can be hard to find the first time, as it is not really in your path. It is part of the MATLAB package and is provided in the example functions. You can reach it by searching for it in the help docs and once you find it, open it and save it to a folder in your path.
h1=figure(1);clf
subplot(4,1,1);
hold on
xlim([0.2,1]);ylim([-1,1])
%arrow
[arrowX,arrowY]=dsxy2figxy([0.2,1],[0,0]);
annotation('arrow',arrowX,arrowY)
%crosses
x=[0.3,0.4,0.6,0.7,0.75];
plot(x,0,'kx','markersize',10)
%pipes
p=[0.5,0.65];
text(p,[0,0],'$$\vert$$','interpreter','latex')
%text
text([0.25,0.5,0.65],[1,-1,-1]/2,{'$$d_i$$','E[d]','$$\theta$$'},'interpreter','latex')
axis off
print('-depsc','arrowFigure')
This will produce the following figure:
This is sort of a hackish way to do it, as I've tricked MATLAB into printing just one subplot. All rasterized formats (jpeg, png, etc) will not give you the same result, as they'll all print the entire figure including where the non-declared subplots should've been. So to get this effect, it has to be an eps, and it works with it because eps uses much tighter bounding boxes... so all the meaningless whitespace is trimmed. You can then convert this to any other format you want.
Ok so the closest I have come to solving this is the following
hax = gca();
hold on
for i=1:120
if genders(v_labels(i)) == CLASS_WOMAN
plot(v_images_lda(i,:) * w_lda,0,'r*');
else
plot(v_images_lda(i,:) * w_lda,0,'b.');
end
end
set(hax, 'visible', 'off');
hax2 = axes();
set(hax2, 'color', 'none', 'ytick', [], 'ycolor', get(gcf, 'color');
pos = get(hax, 'position');
set(hax2, 'position', [pos(1), pos(2)+0.5*pos(4), pos(3), 0.5*pos(4)]);
title('LDA 1D Plot');
hold off
So in short, I hid the original axis and created a new one located at 0 of the original axis, and as I couldn't remove the y axis completely I set it's color to the background color of the figure.
You can then decide if you also want to play with the tick marks of the x-axis.
Hope this helps!
Very naive trick but a useful one.
Plot in 2d using matlab plot function. Then using edit figure properties compress it to whichever axis you need a 1D plot on !! Hope that helps :)