I have some problems with figures in Matlab. I divided my bar plot into two figures since I have totally 171 bars. I took the first half of the data at first (figure 1) and then the second half (figure 2). But then I got a problem with Xticks. Now both start from zero, but I would want the second half (figure 2) to be from 86 to 171 (or with intervals of 10 so that they are from 80 to 180, for instance). I tried set(gca, ‘XLim’, [86 171] to the second figure, but what happened was that bars in that figure ended up outside the figure, which I hadn’t thought before... Any hints how to solve the problem with the Xticks/dividing the figure?
I also have another question about Xticks! I would want to move them downwards in the figure, since I have added text (or actually other numbers which correspond to different bars) right above every bar. I made the figures to fit the whole screen by “set(gcf, 'Position', get(0,'Screensize'));”, but Xticks should be moved downwards so that Xticks and and other numbers are not on top of each other. I would like to learn how to solve these problems, but it seems like I need help from someone who has more experience!
The x ticks are specified by the X argument to bar().
n = 171;
x = randi(20, n);
subplot(2,1,1)
bar(1:85, x(1:85))
subplot(2,1,2)
bar(86:171, x(86:171))
Related
I have multiple fitobjects that I want to plot in the same figure. All curves are fitted from the same five points on the x-axes (ranging from 0.25 to 2.25). If I plot them individually the curve correctly spans only between does values, however, as soon as I add hold on to plot them in the same figure the first fitobject is plotted correctly, while the second spans from -Inf to +Inf, if I change the order of fitobjects the problem remains. So the first curve is always shown correctly the following ones are not. I am not even setting any axes properties yet. Also the two fitobjects have the same Boundaries for the fit.
Example code:
plot(fitobj{1}, 'r'); hold on %curve from 0.25 to 2.25
plot(fitobj{2}, 'b'); %curve is shown from -Inf to +Inf
Do you know why this is happening? Thank you
I am running a simulation in Matlab 2015 which creates data each round, so I am plotting a number of single errorbar series individually. Here's a sample code and the output plot:
figure
xlim([0 30])
hold on
errorbar(1,2.0,1.9,16.5,'x')
errorbar(15,2.0,1.9,16.5,'x')
errorbar(25,2.0,1.9,16.5,'x')
errorbar(10,2.0,1.9,16.5,'x')
errorbar(30,2.0,1.9,16.5,'x')
errorbar(5,2.0,1.9,16.5,'x')
errorbar(20,2.0,1.9,16.5,'x')
The weirdness is that the whisker length appears to be dependent on my X value. As you can see it increases from left to right, even though I did not plot them from left to right. The single-sided width of each whisker is 0.01*x.
Why is this happening, and how can I fix it? This doesn't seem like intended functionality. Ideally I would just manually set the width of each whisker, but I can't find a way to do that. There was a function posted on the MFX a while back, but that was made for R2007b and no longer works. The errorbar series properties do not give any indication that the whiskers can even be affected at all. Any help here would be greatly appreciated!
Is there a way to only plot half the nyquist plot in MATLAB? Nyquist usually has the two lines that go in opposite directions (one going up, and one going down). I only want to plot the one going down.
I know that I can only show the negative axis to "hide" the positive bit of the plot, but I want to show the whole chart, but only have the one line:
Okay, I got the answer. Use nyquistplot instead of nyquist and set the option ShowFullContour to off:
% This is an example contour
G = tf(80, [1 9 30 40]);
h = nyquistplot(G);
setoptions(h, 'ShowFullContour', 'off');
I have plotted a figure with multiple lines on it, and I have noticed that the lines for the plot overlap the x-axis when they are zero. Is there a way that I can essentially get the x-axis to plot on the top, rather than the lines?
Here is a MWE that does the same thing (I haven't put my exact code up as my dataset is quite big).
xdata=1:1:10;
ydata=[1;0.8;0.6;0.4;0.2;0;0;0;0;0];
line(xdata,ydata)
After I plot the lines (multiple per plot in my case), I do various other things with the axes so I get what I need (including adding a secondary set of axes). None of this seems to make any difference as to whether the x-axis is plotted on top of the lines or not.
I did have a search online but couldn't find anything to do with this.
The answer given by Luis is a nice workaround, but the official way to solve this problem is to use the layer property of the axis object, see the manual. To plot the axis on top of the data you do
set(gca,'Layer','top')
To automatically do this for all your plots, you can put the following line in your startup.m:
set(0,'DefaultAxesLayer','top')
This kind of answers you do not make up yourself, I only discovered this trick after asking more or less the same question on comp.soft-sys.matlab many years ago. See also this SO question.
After having plotted all your lines, plot a line on the x axis with the same color as the axis:
hold on
a = axis; %// gives xmin xmax ymin ymax
cx = get(gca,'Xcolor'); %// color of x axis
plot([a(1) a(2)], [a(3) a(3)], 'color', cx)
If the lines also overlap with the y axis and you also want that axis to appear on top, add the following:
cy = get(gca,'Ycolor'); %// color of y axis
plot([a(1) a(1)], [a(3) a(4)], 'color', cy)
I am using a plotyy in matlab to plot two data set in a same figure. The left and right axis are of the different range. But I just find that on the right axis, there seems to be two different set of scale shown. I think one of them is really from the left axis.
t=-1:0.02:1;
y=sin(t);
y1=2*sech(t);
[AX, H] =plotyy(t, y, t, y1);
ylim(AX(2), [0 3.25]);
set(AX(2), 'YTickMode', 'auto')
After searching that online, I found that to turn off the box will solve the problem too. But the problem is to turn the box off will case the top horizontal line gone also. Is that anyway to remove the extra scale and keep the frame? Thanks.
It is possible and not terribly difficult, here's an illustrative example figure based on your test code
What I've done is to add a third axis (in this case I accomplished this by taking a shortcut - I called plotyy twice resulting in an extra blue line in the first axis and an extra second axis with a green line).
The idea is to turn the bounding box off for both the first and second axes, and then to turn it on for the third. That results in the top axis giving you the left vertical axis, the second the right vertical axis, and the third the top horizontal axis.
I don't think there's simple a way to do what you want. If you turn off the box (to get rid of the blue ticks on the right hand side) then the top horizontal line will disappear:
set(AX(1), 'Box','off')
If you want you could re-draw the line back in with:
line([-1, 1], [1, 1])
Or more generally:
lims = get(AX(1),{'XLim','YLim'});
line(lims{1}, lims{2}([2 2]))