I have the following scatter graph:
As you can see by the figure, I've defined a graph where each size of the grid is 5. I guess the grid follows the interval of the axis itself but I haven't quite figured out how change that (for example with a size 1), any help would be greatly appreciated!
The grid spacing is determined by the tick marks on the axes. Set the properties 'XTick' and 'YTick' of the axes to control the spacing of the grid, as follows:
set(gca, 'XTick', [50:75])
set(gca, 'YTick', [52:2:75])
Related
couldn't find this:
I would like to plot a line on X and Y axis that always fits to 100% to the width or height of the figure.
figure; hold on;
plot(rand(1,100));
line(xlim,[.5 .5],'Color','red');
line([50 50],ylim,'Color','red');
pause(.5)
xlim([1 200]);% lines should now automatically extend
with grid on it's possible to get a grid that scales automatically, however it seems impossible to only limit the grid to the X/Y axes. Ideas?
after scaling:
what I would prefer:
The functions xline
and yline
were introduced in MATLAB R2018b, and do exactly as you need.
Furthermore, it is possible to add a (text) label to the line.
figure;
plot([1:10]);
set(gca, 'position', [0 0 1 1]);
Whatever I tried I couldn't stabilize the colors of two diagrams. When the values decrease from 0 - 30 to 0-1 the colormap always adapts to new values. In the figures attached, I need to stabilize the first color scale 0-30, and second legend should by all dark blue as well as the surface.
Disregard the y-values.
Thank you so much for all your help and advices.
The partial code is below.
args = {time,freq,abs(cfs).^2};
surf(args{:},'edgecolor','none');
view(0,90); axis tight;
shading interp; colormap(parula(128));
h = colorbar;
I tried this but didn't work.
set(h,'ylim',[0 100]);
yal=linspace(1,100);
set(h,'ytick',yal);
Try adding this instead:
caxis([0 100]);
https://www.mathworks.com/help/matlab/ref/caxis.html
I've been trying to get Matlab to change the labelling on my contourf plots for about an hour now. When I go to change XTickLabel or XTick, it simply removes my x-axis altogether! The frustrating and infuriating thing is that I'm doing exactly what all the help pages and help forums are asking me to do - I honestly don't understand why this is not working.
Hence, I am here.
My plotting code (knowledge of the function shouldn't be required - the code is rather intense. It is, however, a 2D contourf plot with valid data and ranges - the axes are the issue, not the graph):
contourf(time,f,power,levels)
colormap(jet(levels))
set(gca,'XTickLabelMode','manual')
set(gca, 'XTick', 0:23);
set(gca, 'XTickLabel', {'0';'1';'23'});
xlabel('Time (UT)')
ylabel('Frequency (Hz)')
caxis([0,8])
axis([0 StopTime 0 0.1])
Any help would be greatly appreciated!
Solved:
I realized that the 'XTick' relied on current values of the array I was using to define the x-axis. I can't just assume matlab will evenly space a new array (at least, if there's a way to do that, I don't know). So, since I have 85,680 data points on my X-axis, I simply rescaled it by:
set(gca, 'XTick', 0:3570:85680)
set(gca, 'XTickLabel', num2cell(0:24))
Moral of the story: Matlab doesn't let you arbitrarily stick a new axis over an old one using these two functions.
You have a final axis([0 StopTime 0 0.1])) command which clears your plot, by creating a fresh new axis. That's why all your existing plots are gone. Try removing it:
contourf(time,f,power,levels)
colormap(jet(levels))
set(gca,'XTickLabelMode','manual')
set(gca, 'XTick', 0:23);
set(gca, 'XTickLabel', {'0';'1';'23'});
xlabel('Time (UT)')
ylabel('Frequency (Hz)')
caxis([0,8])
Now the question becomes: are your ticks sensibly placed for the data you are representing? Without knowing the data I cannot answer this for you. So the ball is in your court now. ;)
You can use cell arrays to define the ticks and tick-labels and then use them with set function call, to make it more elegant -
xtick_label_cellarr = num2cell(0:24)
xtick_cellarr = linspace(0,85680,numel(xtick_label_cellarr))
set(gca, 'XTick',xtick_cellarr)
set(gca, 'XTickLabel',xtick_label_cellarr)
I want to plot a series of x and y coordinates with axes like those that are produced by wblplot. How can I achieve that? I can't use wblplot.
http://www.mathworks.se/help/stats/wblplot.html
Use semilogx to scale X axis logarithmically or for usual plot change its property
set(gca, 'XScale', 'log');
Change ticks and grid line spacing
set(gca, 'YTick', vectorOfYvalues);
set(gca, 'YGrid', vectorOfYvalues);
More on axes properties in documentation.
I plot a single trace in MATLAB with plot(). I'd like to add a right-y axis with a different set of tick marks (scaled linearly). Is this possible?
There are a number of good suggestions on this closely related question, although they deal with a more complicated situation than yours. If you want a super-simple DIY solution, you can try this:
plot(rand(1, 10)); % Plot some random data
ylabel(gca, 'scale 1'); % Add a label to the left y axis
set(gca, 'Box', 'off'); % Turn off the box surrounding the whole axes
axesPosition = get(gca, 'Position'); % Get the current axes position
hNewAxes = axes('Position', axesPosition, ... % Place a new axes on top...
'Color', 'none', ... % ... with no background color
'YLim', [0 10], ... % ... and a different scale
'YAxisLocation', 'right', ... % ... located on the right
'XTick', [], ... % ... with no x tick marks
'Box', 'off'); % ... and no surrounding box
ylabel(hNewAxes, 'scale 2'); % Add a label to the right y axis
And here's what you should get:
You may try this submission to MATLAB File Exchange - PLOT2AXES.
PLOT2AXES example http://www.mathworks.com/matlabcentral/fx_files/7426/2/plot2axes.png
Jiro's solution is good (file Exchange function), however, it does not allow to use Matlab's built-in plot functions (bar, scatter, etc.), and you have to use plot2axes instead. Matlab's own help gives the solution to have two axes on any type of plots:
ax2 = axes('Position',get(ax1,'Position'),...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none',...
'XColor','k','YColor','k');
Look at: http://www.mathworks.com/help/techdoc/creating_plots/f1-11215.html
From matlab 2016 and onwards there is an option to define on what axis one plots:
yyaxis left
plots...
yyaxis right
plots...
source:
https://se.mathworks.com/help/matlab/ref/yyaxis.html
Open MATLAB Help with F1 and take a look at the functions below function plot which you mentioned, there you will see plotyy. This is what you probably need.
UPDATE: actually plotyy is NOT the answer to the question as pointed by gnovice.
I was able to do it with the following after plotting the left axis graph:
yyaxis right
ylabel('Right axis label')
plot(x,y1) % plot your right axis graph
Hope it helps.