I have an Nx3 matrix in matlab and I'd like to make a 3 dimensional bar graph out of it, where the X and Y axes are determined by the values of first and second columns of the matrix, the height of each bar is the third column in the matrix, and the number of bars is determined by N.
In other words, if "data" is the matrix then:
data(:, 1) % values of X-axis
data(:, 2) % values of Y-axis
data(:, 3) % values of each Z-axis bar
and there should be one bar for each 1:length(data)
How can I do this in MATLAB?
Secondly, as a variant of this, how can I do the same thing, but this time histogram the bars into N bins in each X, Y, Z dimension? I.e. instead of a bar for each point, just histogram the data into those bins in every dimension, and plot a bar for each bin.
thanks very much for your help.
Regarding your 1st question, you can achieve something similar to your request, by:
stem3 (data(:,1), data(:,2), data(:,3), 'marker', 'none', 'linewidth',10)
Not exactly bars, but produces a similar effect.
To plot 'real' bars (such as the ones bar3 plots), I think you'll have to use low-level graphics function such as surface (which is used by bar3 to plot bars).
Regarding your 2nd question, I'm not sure I understand --- if you calculate a histogram for each dimension, you end up with 4-dimensional data --- the bin location for each dimension + the hist count itself. What exactly do you want to plot?
Related
I want to create a 3x3 matrix of plots in MATLAB where each element is a subplot of some corresponding 1000 samples (suppose values are stored in a 3x3x1000 matrix A). I want the subplots to be spaced very close together, and the y axis numbers/labels to only show up for the left subplots and the x axis numbers/labels to only show up for the bottom subplots.
For example, something similar to plotmatrix, except subplot ij would plot A(i,j,:).
plotmatrix example
Is there an easy way to do this in MATLAB? Thanks!
The axes properties of the subplots can be modified to achieve this, but an easier way would be to use a FEX submission called panel. Have a look at its example output:
subplot can do that for you in MATLAB.
h = subplot(3,3,1)
Will split the current figure in a 3 x 3 matrix and creates an axes(area where you plot something) in the 1st cell of the matrix. h is the "handle" to the axes which you can then use to modify the xlabels and ylabels in any way.
Documentation for subplot
Documentation for Axes
I am trying to make a contour plot using the following matlab code:
x=linspace(-10,10);
y=linspace(-10,10);
[X,Y]=meshgrid(x,y);
Z=X.^3-Y.^3;
figure
[c,h]=contour(X,Y,Z,[3]);
clabel(c,h)
This gives me the wrong picture actually:
I really don't understand what goes wrong here, because when I do [c,h]=contour(X,Y,Z,[3 0]) for example, it does give me the correct contour plots for the levels 3 and 0, I need help.
If you only give a single number to contour there, it interprets it as the number of contour lines you want and picks the levels automatically. From the docs:
contour(Z,v) draws a contour plot of matrix Z with contour lines at the data values specified in the monotonically increasing vector v. To display a single contour line at a particular value, define v as a two-element vector with both elements equal to the desired contour level. For example, to draw contour lines at level k, use contour(Z,[k k]). Specifying the vector v sets the LevelListMode property to manual.
e.g. to get a single contour at "3", you need to do it this way instead:
figure
[c,h]=contour(X,Y,Z,[3,3]);
clabel(c,h)
The fourth argument of contour can be two things.
If it is an array of numbers (more than 1) then its the contour value you want to show. Else, if its a single value, its the amount of contour lines you want to show.
Example:
x=linspace(-10,10);
y=linspace(-10,10);
[X,Y]=meshgrid(x,y);
Z=X.^3-Y.^3;
figure
subplot(121)
[c,h]=contour(X,Y,Z,[10]);
clabel(c,h)
subplot(122)
[c,h]=contour(X,Y,Z,[1000 -1000 50 -70 3 0]);
clabel(c,h)
What I want to do is very simple, I just cannot seem to get MATLAB to do it. I would like to plot contours using my 2D data set.
My data set is large; 2 x 844240. I can do a scatter plot just fine,
scatter(Data(1,:), Data(2,:));
Reading through the forums I found Scatter plot with density in Matlab, where a hisogram was plotted. This would suffice, however, I would like to overlay the plots.
The issue is that they have different axis, my scatter data has an axis of [0 0.01 0 2500]; whereas the histogram is [0 100 0 100].
Is there a way to change the axis values of the histogram without modifying the image?
Thanks!
If I understand correctly, you are using hist3 to construct a histogram and then using imagesc to plot it. You can use the second output argument of hist3 to get the histogram bin centers, and then pass those on to imagesc, e.g.
nBins_x = 100;
nBins_y = 100;
[counts, bin_centers] = hist3(Data, [nBins_x nBins_y]);
x_bin_centers = bin_centers{1};
y_bin_centers = bin_centers{2};
imagesc(x_bin_centers, y_bin_centers, counts)
A couple other notes:
In your case, you will need to transpose your [2 x N] matrix when passing it to hist3, which expects an [N x 2] matrix.
imagesc puts the first axis (which I've been calling the "x" axis) on the vertical axis and the second on the horizontal axis. If you want to flip it, you can use:
imagesc(y_bin_centers, x_bin_centers, counts')
If you want to specify the histogram bins explicitly (e.g. to match your scatterplot) you can specify that in the arguments to hist3:
x_bin_centers = linspace(0, .01, 100);
y_bin_centers = linspace(0, 2500, 100);
counts = hist3(Data, {x_bin_centers, y_bin_centers};
And if you want a contour plot, you can use (note that contour takes the axes arguments in a different order than imagesc):
contour(x_bin_centers, y_bin_centers, counts');
If you are unhappy with the jaggedness of the contours, you may consider using a kernel density estimate instead of a histogram (check out ksdensity) (oops, looks like ksdensity is 1-D only. But there are File Exchange submissions for bivariate kernel density estimation).
Usually, trisurf is used to plot nodal quantity on a triangular mesh. However, I would like to plot some element-wise quantity using the following command.
trisurf(t,p(:,1),p(:,2),elewise_quantity,'edgecolor','k','facecolor','interp');
Where elewise_quantiy should be of the same dimension as p(:,1) or p(:,2), so I create elewise_quantity by associating the element-wise quantity to each node of that element.
In this specific case, the 8 triangular element in the middle are associated with 1 and all other elements are 0-valued. There are 10*10 little squares and 10*10*2 little triangulars.
The problem is, as shown in the picture, trisurf can't produce the effect I want. What I expect is an "exact element-wise description", i.e sharp transition at the edge
Also notice that at each corner, the display is different, this's due to the specific orientation of the triangular, is there an elegant way to deal with it?
If you need sharp discontinuities across elements, I'd recommend not displaying nodal values. Calculate element values at internal integration points (e.g. element average at centroid) and assign that value to the element. Ask Matlab to color each element according to its centroidal or integration point value.
You can set per-triangle colors using this method:
%// Example data
X = rand(100,1);
Y = rand(100,1);
Z = X+Y;
T = delaunay(X,Y);
C = mean(Z(T),2);
%// Plot the data
hh = trisurf(T,X,Y,Z);
set(gca, 'CLim', [min(C), max(C)]);
set(hh,'FaceColor', 'flat', ...
'FaceVertexCData', C, ...
'CDataMapping', 'scaled');
If you also don't want the see the triangles that appear at the boundary, you won't get this behavior from a single call to trimesh. You would need to compute a per-triangle quantity and then drop those you don't want to plot according to the per-triangle quantity.
I am looking for help for my particular problem.
I have a contour plot created from XYZ data. This plot contains 2 broad peaks with one more intense than the other.
When the most intense peak is aligned with the Y axis, I can perform a fitting of every YZ curve at each X values. I usually do a gaussian fit to plot the peak center on the same graph.
In some cases I need to perform the same fitting but no along the Y axis direction (in this case I just plot YZ scan at every different X values) but along another arbitrary direction.
For the moment the only way I found is the following:
-plot the contour plot and find for the position of the most intense peak
-if the position is not aligned with the Y axis, then rotate all the datas and plot again the contour
-perform the YZ gaussian fit for every X value
- Rotate the resulting XY position to go back to the original plot
-plot the XY position as a line on the original contour plot
this is quite long and requires a lot of memory. i would like ot know if there is a more elegant/faster way.
Thanks for your help
David
I take it you want to extract data from the (x,y,z) data along some arbitrary line in order to make a fit. A contour plot will show only part of the data, the full z(x,y) data can be shown with imagesc etc. Say you want the data along line defined by two points (x1,y1) -> (x2,y2). According to the eq of the line, the line y=a*x+b the slope a is (y2-y1)/(x2-x1) and b=y1-a*x1. For example, I'll select (x,y) coordinates in the following contour:
Create data and end points:
m=peaks(100);
x1=11 ; x2=97;
y1=66; y2=40;
Thus the line parameters are:
a=(y2-y1)/(x2-x1);
b=y1-a*x1;
and the line is:
x=x1:x2;
y=round(a*x+b);
select the proper (x,y) elements using linear indexing:
ind=sub2ind(size(m),y,x)
plot:
subplot(2,1,1)
contour(m,10); hold on
line([x1 x2],[y1 y2],'Color',[1 0 0]);
subplot(2,1,2)
plot(m(ind))
You can now use vec=m(ind) to fit your function.