How do I control axis limits in Plots.jl such that axis starts and ends at a tick? - plots.jl

Only using Plots, how do I control axis limits? I want that the axis always starts and ends at a tick, not the default expansion by some small space. I couldn't find anything useful in plotattr(:Series).
Code is the standard example:
using Plots
x = 1:10; y = rand(10);
plot(x,y)
Can you help? Thanks.

I guess that
plot!(
xlims=(floor(minimum(x)),ceil(maximum(x))),
ylims=(floor(minimum(y)),ceil(maximum(y)))
)
does the trick.

Related

Set y axis max value with Matlab

I'm trying to draw plots with Matlab and the problem is that i want to fix the maximum value of y-axis to 8 . To help you understand me, look at this first example :
you can see that the maximum y value is 8. but when i try to draw this graph :
its maximum y value is 6 . i want to fix it for all examples to 8.
how can i do it?
here's my code for now :
data=importdata('C:/Users/Eden/Desktop/Calcul_accel/fichier_final.txt');
fig = figure(1);
x=data(:,2)
y=data(:,3)
p=plot(x,y)
set(p,'Color','red');
xlabel('Time(milliseconds)','FontSize',12,'FontWeight','bold','Color','b');
ylabel('Acceleration(g unit)','FontSize',12,'FontWeight','bold','Color','b')
thank you very much
Use ylim if you just want to modify the y axis.
Therefore, do this once your plot is already open:
ylim([0 8]);
This overrides the auto-scaling of the axes so that y always spans between 0 to 8.
In general, #eigenchris mentioned to use axis, which allows you to change the dynamic range of what is viewable in a plot for both the x and y axes. However, since you only want to change how the y-axis is visualized, one call to ylim is enough.

Distance between axis number and axis in MATLAB figure

I struggle a little bit with overlapping axis numbers of the y and x axis like it is shown in the image. I'd like to keep the size of the numbers and therefore think that simply shifting the numbers away from the axis itself would be an appropriate way to handle this issue.
Is there a possibility to do that?
Thanks in advance,
Joe
Here is a little workaround using text annotations. Basically you clear the current XTick labels and replace them with similar labels, but you can specify the distance from the axis:
clc
clear
close all
x = 1:20;
hPlot = plot(x,sin(x));
set(gca,'xaxisLocation','top');
set(gca,'XTickLabel',[]); %// Clear current XTickLabel
ylim = get(gca,'YLim'); %// Get y limit of the plot to place your text annotations.
for k = 2:2:20
text(k,ylim(2)+0.1,num2str(k),'HorizontalAlignment','Center') %// Play with the 'ylim(1) -0.1' to place the label as you wish.
end
Giving this:
Of course now it's exaggerated and you can do the same for the y axis if you want (using the 'XLim' property of the current axis,gca).

Finding the limits of automatically-adjusted axes in a MATLAB plot

I'm creating a 2d MATLAB plot. I'm setting the limits of my x axis, and letting my y axis auto-adjust (by setting its limits to [-inf inf]). After creating my plot, I need to check what my y axis has auto adjusted to (as I'm going to create a heatmap to put under my plot).
Unfortunately, ylim (and similar functions) only produce [-inf inf], not whatever the axes have adjusted to.
Some code which reproduces this problem (much more simply than my actual code) is:
function createplot(xbounds)
x = xbounds(1):0.5:xbounds(2);
y = x.^2;
plot(x,y);
axis([xbounds,-inf,inf]);
createplot([0,10])
which produces a parabolic plot with y limits = [0,100]. However, ylim = [-inf, inf].
Any help would be appreciated!
/ Wilbur
As #Shai suggested, axis can give info regarding the ylimits without the need to set them to [-inf,inf] or use axis to set the x-axis bounds:
xbounds=[1 10]
x = xbounds(1):0.5:xbounds(2);
y = x.^2;
plot(x,y);
xlim([xbounds(1) xbounds(2)]);
v=axis
v =
1 10 0 100
Looking at #natan's answer I think the solution to your problem is
Do not use [-inf inf] for auto-adjusting axis limits.
If you want Matlab to auto adjust some of your axes limits and manually set others, then you should use either xlim, ylim or zlim for the specific axis you wish to set and leave all the other unchanged so Matlab can set them automatically.
This way you will not override the values Matlab assigns to those axes and you will be able to read them using axis, xlim, ylim or zlim.
Please see #natan's answer for corrected code.

correct axis range (matlab)

how can i solve the problem of these two images automatically (with a code adaptable to different data) without having to fix the axes range for each plot (i have millions of these plots)?
problem: axis range should be smaller
problem: axis range should be bigger
also, i need axis to be correctly labeled from the first value to last (see example in comment please)
any help is highly appreciated. thank you so so much.
To set axis limit and visualize chart better you can use axis command like axis([xmin xmax ymin ymax]) where parameters set chart borders. It should help you. More information is here:
http://www.mathworks.se/help/matlab/ref/axis.html
In order to have a complete bounding box use box on.
In order to avoid large empty space around a plot (or no space at all) use xlim and ylim. Try the following:
figure
plot(x,y)
box on
x1 = min(x);
x2 = max(x);
dx = x2-x1;
y1 = min(y);
y2 = max(y);
dy = y2-y1;
fc = 10/100 % this is a factor of 10% of empty space around plot
xlim([x1-dx*fc x2+dx*fc])
ylim([y1-dy*fc y2+dy*fc])
If you want to have a tick value appear at the start and the end of the axis, you could either force it by set(gca,'Xtick',[values]), where values are those ticks you want to show; or by floor and ceil of the xlim and ylim min and max limits above.
Hope this is what you need

Fixing the Radial Axis on MATLAB Polar Plots

I'm using polar plots (POLAR(THETA,RHO)) in MATLAB.
Is there an easy way to fix the range for the radial axis to say, 1.5?
I'm looking for something analogous to the xlim, ylim commands for cartesian axes. Haven't found anything in the docs yet.
this worked for me... i wanted the radius range to go to 30, so i first plotted this
polar(0,30,'-k')
hold on
and then plotted what i was actually interested in. this first plotted point is hidden behind the grid marks. just make sure to include
hold off
after your final plotting command.
Here's how I was able to do it.
The MATLAB polar plot (if you look at the Handle Graphics options available) does not have anything like xlim or ylim. However, I realized that the first thing plotted sets the range, so I was able to plot a function with radius range [-.5 .5] on a [-1 1] plot as follows:
theta = linspace(0,2*pi,100);
r = sin(2*theta) .* cos(2*theta);
r_max = 1;
h_fake = polar(theta,r_max*ones(size(theta)));
hold on;
h = polar(theta, r);
set(h_fake, 'Visible', 'Off');
That doesn't look very good and hopefully there's a better way to do it, but it works.
Simple solution is to make a fake graph and set its color to white.
fake=100;
polar(0,fake,'w');
hold on;
real=10;
polar(0,real,'w');
In case anyone else comes across this, here's the solution:
As Scottie T and gnovice pointed out, Matlab basically uses the polar function as an interface for standard plots, but with alot of formatting behind the scenes to make it look polar. Look at the values of the XLim and YLim properties of a polar plot and you'll notice that they are literally the x and y limits of your plot in Cartesian coordinates. So, to set a radius limit, use xlim and ylim, or axis, and be smart about the values you set:
rlim = 10;
axis([-1 1 -1 1]*rlim);
...that's all there is to it. Happy Matlabbing :)