Matlab: Format the decimals in contour labels - matlab

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.

Related

Aligning histogram plots

I have a given data set, and I want to compare the histograms of this data when represented as a bar histogram and a line histogram. Specifically, I want to use
myhist = histogram(mydata)
to get the bar histogram, and plot on the same figure a line histogram using
mylinehist = plot(myhist.Values)
However, when I do that, I get the following figure
It seems like the line histogram mimics the shape of the bar histogram, but offsets it by a certain amount on the x-axis. Is there a way to align the two so I can have them overlapping? I tried using a command like
align([mylinelist,myhist],'Left','None')
but to no avail. Thanks!
You need to specify the x-axis values for your line plot. These should be the midpoints of your histogram bins.
Try:
midpts = myhist.BinEdges + (myhist.BinWidth / 2);
plot(midpts(1:myhist.NumBins), myhist.Values);

Smooth Excel-like plot with correct legend in Matlab

I have some sparse data and want to plot them as markers connected by a smooth, interpolated line - like the default behaviour of Microsoft Excel.
There are solutions to this problem easily found on the internet, but I find them unsatisfactory. What they do is: plot the sparse data as one data set drawing it as markers without lines, interpolate it with a method of choice and plot the interpolation as the second data set, with lines without markers.
The problem with these tricks is that in the legend the two data sets will be listed separately. I would expect a single data set depicted in the legend as a line crossing through a marker.
Is it possible in Matlab?
If you want to plot an interpolated line there are lots of ways to do that. You can try generating an interpolated line using the matlab interp1() function.
Let's create x and y data with no NaN.
x = randn(1,10)
y = randn(1,10)
If you want 1000 data points where previously you only had a few, that's pretty easy:
x2 = min(x):(max(x)-min(x))/1000:max(x)
y2 = interp1(x,y,x2,'cubic')
and you can plot your data and spline using
plot(x,y,'r+')
hold on
plot(x2,y2,'r-')
A custom legend is straightforward when you use handle graphics. You can plot a dummy data set with a red line passing through a marker using
h(1) = plot(NaN,NaN,'r-+')
lstring{1} = 'Data';
You can then add a legend that points to this data set using
legend(h,lstring)
You'll end up with something that looks roughly like this:
The nice thing about using handle graphics (i.e. the h) is you can throw whatever data series you want into the legend as h(end+1) and lstring{end+1}.

Ignoring non-specified points in matlab plot

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)

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 plot 2D data with different colors and markers

Im faced with a problem where i need to plot a two dimensional data with different colors and markers.
We are given with 2 array, namely points (n x 2 dimension) and Label (n x 1 dimension). Im not sure about the number of unique values in the Label array but maximum could be 10. I would like to plot the points with different color and markers based on its corresponding Label value.
Can any one help me in this regard
Use gscatter, which does a scatter plot, using a group (Label in your case) to plot in different colours/makers.
GSCATTER(X,Y,G,CLR,SYM,SIZ) specifies the colors, markers, and
size to use. CLR is either a string of color specifications or
a three-column matrix of color specifications. SYM is a string
of marker specifications. Type "help plot" for more information.
For example, if SYM='o+x', the first group will be plotted with a
circle, the second with plus, and the third with x. SIZ is a
marker size to use for all plots. By default, the marker is '.'.
So you can specify colours like 'rgcmykwb' to do red for the first group, green for the second, etc or [] just to have Matlab sort it out.
By default Matlab uses the same marker for each group, so you need to specify what markers you want to be used for each group. If you do '.ox+*sdv^<>ph' you'll just cycle along all the markers that Matlab has.
n=50;
% make nx2 matrix of random points.
points = random('unif',0,1,n,2);
% make nx1 matrix of random labels from {1,2,...,5}
labels=round(random('unif',1,5,n,1));
% plot. Let Matlab sort out the colours and we will specify markers.
gscatter(points(:,1),points(:,2),labels,[],'ox+*sdv^<>ph.')
It looks a bit like this: