I'm trying to plot some data with two y-axes, but I get a lot of extra space - as you can see in the attached image.
My code is the following; the strange thing is that the last value in t is 50, not 60 like the plot would seem to show.
yyaxis left
plot(t,i_test*1000,'b-',t,iout*1000,'k-','LineWidth',2)
ylabel('Current (mA)')
yyaxis right
plot(t,vin,'r-','LineWidth',2)
ylabel('Voltage (V)')
xlabel('Time (s)')
Solution:
Adding axis tight like this fixed the issue. Thanks!
yyaxis left
plot(t,i_test*1000,'b-',t,iout*1000,'k-','LineWidth',2)
ylabel('Current (mA)')
axis tight
Related
I have an issue with changing the plotting order when I am plotting on two different y-axes. I want the plot on the left axis to be infront of the plot on the right yaxis. I already treid to change the order of calling the plotting functions, as you can see below. Further, I looked into this post:
Similar StackOverflow Question
But the accepted answer did not help me. I guess the issue is that I only change the order on one axis.. Also the command uistack only results in an error for I guess the same reason. Any help is highly appreciated.
My code:
V = horzcat((-2:0.1:2), (2:-0.1:-2));
t = (0:0.01234568:1.01);
T = (0:25:2025);
figure(30);
yyaxis right
plot(t ,V, 'Linewidth',3); hold all;
xlabel('Time / s');ylabel('Voltage / V');
set(gca,'FontSize',fontsize, 'LineWidth',2,'TickLength',[0.025 0.025])
xticks([0., 0.25, 0.5, 0.75, 1]);
ylim([-2, 2]),
ax = gca;
ax.XAxis.MinorTick = 'on';
ax.XAxis.MinorTickValues = (0:0.05:1);
set(ax.YAxis, 'MinorTick', 'on')
set(ax.YAxis, 'MinorTickValues', (-2:0.25:2))
yyaxis left
top = plot(t ,T, 'Linewidth',3); hold all;
xlabel('Time / s');ylabel('Temperature / K');
ax = gca;
%ax.YAxis.MinorTick = 'on';
%ax.YAxis.MinorTickValues = (250:50:650);
set(ax.YAxis, 'MinorTick', 'on')
set(ax.YAxis, 'MinorTickValues', (250:50:650))
chH = get(gca,'Children');
set(gca,'Children',[chH(end);chH(1:end-1)]);
uistack(top, 'top');
What it looks like now:
As you can see the orange line is in front of the blue one, however I would like to have it the other way around.
Any help is highly appreciated.
That's because the get(gca,'Children') returns only one line object! And it is the blue one. So this code actually does nothing.
But I believe the problem comes from the yyaxis method. According to MATLAB docs:
Tips
...
The Children property of the Axes object only contains the children for the active side. To access all the children for both sides, use the allchild function.
Axes Properties
Axes properties related to the y-axis have two values. However, MATLABĀ® gives access only the value for the active side. For example, if the left side is active, then the YLim property of the Axes object contains the limits for the left y-axis. However, if the right side is active, then the YLim property contains the limits for the right y-axis.
What I get from above description is that when you call yyaxis, another axes object is created an placed above the original one. But they are somehow internally coupled, so you can access their properties, including their separated lists of children, by activating them after calling yyaxis again.
I do not manage to change the order of plotted elements in this figure.
I would like to plot the line plot over the filled area.
The line should always be visible.
figure
yyaxis left
h = plot([0 10],[0 10],'LineWidth',5);
yyaxis right
f = fill([3 3 6 6],[0 10 10 0],'r');
% this does not work
h.ZData = ones(size(h.XData));
f.ZData = zeros(size(f.XData));
% using area instead of fill does not help
% plotting yyaxis right before yyaxis left does not help
I found a work-around solution by changing what is plotted in yyaxis left and yyaxis right.
The line is now plotted on top of the area.
However, the position of the axes is also switched.
I would like to keep it the way it is now (line should belong to left axis, area should belong to the right axis) since it looks nicer when I label it this way:
figure
yyaxis right
h = plot([0 10],[0 10],'LineWidth',5,'color','b');
yyaxis left
f = fill([3 3 6 6],[0 1 1 0],'r');
I managed to get it working with a combination of the hint from #LuisMendo and changing the 'ZData' attribute of my filled areas. Thank you all for your help!
Hello I would like to display an axis centered on [0,0,0] of a scatter 3 plot.
Is there a way to do this using the line function?
http://au.mathworks.com/help/matlab/ref/line.html
Not quite sure how to interpret it. Would greatly appreciate a hand.
If you just need the axes try adding:
hold on;
line(xlim,[0,0],[0,0]);
line([0,0],ylim,[0,0]);
line([0,0],[0,0],zlim);
After your scatter plot.
EDIT:
If you want to place the axes with origin at another point, you can do as follows:
hold on;
line(xlim,[origin(2),origin(2)],[origin(3),origin(3)]);
line([origin(1),origin(1)],ylim,[origin(3),origin(3)]);
line([origin(1),origin(1)],[origin(2),origin(2)],zlim);
Where origin is the point where you want the axes to meet.
I have a simple plot in Matlab but as you can see from the screenshot. There is a lot of white space on the right side of the graph after the end of the data series.
Any idea how to get rid of this white space and make the plot go right to the edge of the figure? Here is my code:
Plotx = plot(x);
hold on
PlotState = plot(Y);
set(Plotx,'Color','black','LineWidth',2.5);
set(PlotState,'Color','red','LineWidth',2.5);
set(gca, 'XTick',(1:3:62))
labels = time;
set(gca,'XTickLabel',labels(1:3:62))
grid on
This usually works for me:
axis tight;
xlim('auto');
You must select your figure, go back to the console and use those commands so they affect the last active figure.
EDIT: The above line should automatically make your graph axis very tight on your data. For more fine control, you may want to define the axis limits manually:
axis([xmin,xmax,ymin,ymax])
I found a solution. I manually adjust the limit of the x-axis, using:
set(gca,'XLim',[0 63])
i am trying to plot "scatter" on top of "imagesc" which dosen't work. However, i can plot "scatter" figure separately. I even tried "hold all" instead of "hold on". Can someone help me out? Thank you.
figure(2)
imagesc(lat1,height,scatter0')
hold on;
scatter(lat1,top2,'k')
title('2012_12_4')
colormap(colors)
axis xy
It is probably x-y limits that are not matched, or that the z-values that are not properly normalized to plot side by side. The normalization is important because imagesc and scatter will share the same colormap. Other than that, your code works nicely for me. For example, I'm normalizing in the range [0,1] the "z" values of both plots:
load seamount
m=peaks(200);
m=(m-min(m(:)))./(max(m(:))-min(m(:)));
imagesc([0.996 1.0005],[1,1.012],m);
hold on ;
z=(z-min(z(:)))./(max(z(:))-min(z(:)));
scatter(x./max(x(:)),y./max(y(:)),5,z);