Ignoring non-specified points in matlab plot - matlab

how can we ignore the non-specified values in x-axis label of a matlab plot?
for ex:
if my
x=[201:210];,y=rand(size(x));
I would like to display only those specified values of x such as 201,202,203..instead of with the intermediate values such as 201.5,202.5.. Basically I wanna get rid of these decimal values in my plot.
thanks in advance.
DURAI

If you don't want to plot them. You can just specify your variables used in plot.
plot(x(1:2:end),y(1:2:end))
this would plot only each second value. Obviously you can use logical indexing and any other means e.g. another index-array as well.
plot(x(x>10),y(x>10))
as another example. Just take care that you use the same commands in both variables otherwise you will get wrong results or an error (if number of points don't match up).
If you want to plot specific values you can use a for loop to loop over the values you want to use:
x=1:1:10;
y=115:15:250;
figure(2)
for x=[4,5,7]
display(x)
plot(x,y(x),'x'); hold on
end
If you want to just change the area which is displayed but use the whole set of data points you can use:
axis([xmin,xmax,ymin,ymax])
or if you want to change the labels of x-axis use:
set(gca, 'XTick',new_x_axis_steps, 'XTickLabel',new_x_labels(new_x_axis_steps))
where new_x_axis_steps is an array which defines the start, end point and the step and new_x_labels is what you want to write there (if you want to use strings), otherwise just use:
set(gca, 'XTick',new_x_axis_steps)

Related

Matlab histcounts show values on x-axis

I want to draw a histogram, using plot(histcounts(X,edges)).
It works fine, except that on the x-axis, the number of the bin is displayed, not the actual value the bin refers to.
To make a bit clearer what I mean, I append two plots. Both display the same data, but for the first one, I used plot(histcounts(X,edges)) and for the second hist(X,edges). The plot for which I used hist shows the x-axis the way I want it to look like, with the value the bin refers to. I would like the plot(histcount(...) to have the same x-axis, instead of showing the bin number.
Histogram using plot(histcounts):
Histogram using hist:
How can I change the x-axis to show this value instead of the bin number?
Thanks a lot!
If you have the edges, you can get the centres using
centres = edges(1:end-1)+ diff(edges)/2;
then the plot can be
plot(centres, histcounts(X,edges));
If you do not need to specify the edges you can get them using
[h_counts, edges] = histcounts(X);

Matlab: Format the decimals in contour labels

I want to cut the number of decimals in the following contour plot. I do:
[cc,hh] = contour(X,Y,Z,levels,'LineColor','k');hold on
texth = clabel(cc,hh,'FontSize',8);
which gets me the first contour with long labels. Then in order to cut the decimals I do:
for i = 1:size(texth); textstr=get(texth(i),'String'); textnum=str2double(textstr); textstrnew=sprintf('%0.0f', textnum) ; set(texth(i),'String',textstrnew); end
And this gives the second plot. As you see, there is a wide gap between the label and the contour lines which looks awful. Any ideas how to solve this?
Instead of modifying the result, create a contour plot with the levels you want, so you don't need to cheat the data.
Define levels as e.g. levels=997:1010
and then
contour(X,Y,Z,levels,'LineColor','k','ShowText','on');
Will create a contour plot with the text included and the levels being specifically the ones in the variable levels, in this case 997,998,999,...,1009,1010
If, as #David suggests, your levels variable already is a vector, then replace it by round(levels) as himself suggested.

how to plot graphic with the same Y bound

I have to plot graphics and compare them but it's misleading because of different y-axis upper bounds for each of them (they are all positive values).
Anyway I'm doing a cycle like this:
%% check for data files
for i=1:length(files)
load
plot(x,y)
end
I need to change my code if I want to save the maximum upper bound on y.
%% check for data files
for i=1:length(files)
load
%% compare upper bounds and get the maximum
end
%% plot cycle
But then how can I use this value to plot comparable but separated graphics.
I can't plot them in only one figure because they are many. Also tell me If you think of a possible optimization for my task.
ylim is what you are looking for, but if i understood you right you might also want to look at linkaxes.
You can use ylim. Check out these questions: 1, 2 or 3.
ylim([ymin ymax]) % or something like: set(gca,'Ylim',yLimits)
sets the axis limits in the current axes to the specified values

MATLAB: Plotting Contours at Specfic Points (x,y)

I'm currently producing a contour plot using
contour(x,y,z)
However, I would like to specify some additional contour lines to the ones provided.
I understand that I can use contour(x,y,z,v) where v is some vector containing values of the contour levels I would like but I don't really want to use this since I don't know exactly the levels.
Instead is it possible to plot the contour that goes through a specific point (x,y)?
Thanks.
You can overplot a second contour with a single, specific value for the contour, optionally specifying parameters like line width to make it obvious:
contour(x,y,z)
hold on
lev = z(n,m); % find the value you want in z
contour(x,y,z,lev,'Linewidth',2);

How to give different colors when I loop for plot in MATLAB?

I have some data say X with size (100,2). This X is composed of data for 10 categories (set of 10). Now I would like to look the pattern in the data for each category. For this I need to have different colors assigned to each category. I am trying to loop instead of doing 10 different plots. I tried the below.
hold on
for i=1:10:100
plot(X(i:i+9,1),X(i:i+9,2),'.')
end
hold off
This gave me a plot with same color. How can I assign different colors for different range?
The answers mentioning hold all are correct and useful for cycling through the colors specified by the ColorOrder axes property (even though just hold on is now equivalent to hold all). However, by default MATLAB only specifies a short list of colors (just 7 as of R2013b) to cycle through, and on the other hand it can be problematic to find a good set of colors for more data series. For 10 plots, you obviously cannot rely on the default ColorOrder, so a great way to define N visually distinct colors is with the "Generate Maximally Perceptually-Distinct Colors" (GMPDC) submission on the MATLAB Central File File Exchange. It is best described in the author's own words:
This function generates a set of colors which are distinguishable by reference to the "Lab" color space, which more closely matches human color perception than RGB. Given an initial large list of possible colors, it iteratively chooses the entry in the list that is farthest (in Lab space) from all previously-chosen entries.
For example, here are the colors generated when 25 are requested:
The GMPDC submission was chosen on MathWorks' official blog as Pick of the Week a few years ago in part because of the ability to request an arbitrary number of colors (in contrast to MATLAB's built in 7 default colors). They even made the excellent suggestion to set MATLAB's ColorOrder on startup to,
distinguishable_colors(20)
Of course, you can set the ColorOrder for a single axis or simply generate a list of colors to use in any way you like. For example, to generate 10 "maximally perceptually-distinct colors" and use them for 10 plots on the same axis (not using ColorOrder):
% Starting with X of size 100x2
X = reshape(X,10,10,2); % for clarity, column is category, row is observation
mpdc10 = distinguishable_colors(10) % 10x3 color list
hold on
for ii=1:10,
plot(X(:,ii,1),X(:,ii,2),'.','Color',mpdc10(ii,:));
end
Alternatively, using the ColorOrder axis property simplifies the process:
X = reshape(X,10,10,2); % for clarity, and to avoid loop
mpdc10 = distinguishable_colors(10) % 10x3 color map
ha = axes; hold(ha,'on')
set(ha,'ColorOrder',mpdc10)
plot(X(:,:,1),X(:,:,2),'-.') % loop NOT needed, 'Color' NOT needed
APPENDIX
To get the ColorOrder RGB array used for the current axis,
get(gca,'ColorOrder')
To get the default ColorOrder for new axes,
get(0,'DefaultAxesColorOrder')
Example of setting new global ColorOrder with 10 colors on MATLAB start, in startup.m:
set(0,'DefaultAxesColorOrder',distinguishable_colors(10))
The easiest solution is to replace hold on by hold all.
If you want more control you have to manually define your line specifications (more info here) and then pass them to plot:
linespec = {'b.', 'r-', 'g--o'}; % define your ten linespecs in a cell array
hold on
for i=1:10:100
plot(X(i:i+9,1),X(i:i+9,2),linespec{i})
end
hold off
hold on makes sure the new plot command adds to the plot instead of replacing it. However, each command works as if it were generating a fresh plot, including starting with the first line color (blue). If you want subsequent plots use different colors, use hold all instead. That way the standard 7 line colors are used in turn.
Since you have 10 lines to plot, you might want to specify the colors explicitly to make sure they are all different. For that, use the syntax
plot(..., 'Color', [r g b])