Plot 3D histogram using bar3 - matlab

I'm currently trying to plot the output of hist3 using bar3. This is a simple example:
vec_x = [1 2 4 5 7 8 9 3 8 7 2]';
vec_y = [1 3 9 5 7 8 1 3 2 9 2]';
vec_bin_edges = 0:9;
hist3([vec_x vec_y], 'Edges', {vec_bin_edges, vec_bin_edges});
mat_joint = hist3([vec_x vec_y], 'Edges', {vec_bin_edges, vec_bin_edges});
figure
bar3(mat_joint, 1);
axis tight
In order to demonstrate my issue, I made two pics of both figures:
This one is the output of hist3([vec_x vec_y], 'Edges', {vec_bin_edges, vec_bin_edges});
This one is the output of bar3(mat_joint, 1);
As you can see, the bar3 function does not really "bin" the data values as hist3 does, so the bars are shifted slightly in their positions. My question is now, whether it's possible to make the bar3 plot look exactly like the hist3 plot. My motivation to do so is, that I need to modify the mat_joint matrix and plot it again, which is not possible using hist3.
EDIT: The different colors are not important, it's just about the bin positions

ok, I figured it out:
set(gca, 'xtick', [1.5:1:10.5]);
set(gca, 'ytick', [1.5:1:10.5]);
vec_bin_labels = 1:10;
vec_string_bin_labels = reshape(cellstr(num2str(vec_bin_labels(:))), size(vec_bin_labels));
set(gca, 'xticklabel', vec_string_bin_labels);
set(gca, 'yticklabel', vec_string_bin_labels);

Related

How to plot several vectors qith quiver3 in matlab

Im trying to plot the elements of a given matrix
A = [ 1 2 3; 3 4 5, 5 6 7];
using quiver3, so all of these are in the same figure using:
figure(3);
hold on;
quiver3(0,0,0,A(1,1),A(1,2),A(1,3),0);
quiver3(0,0,0,A(2,1),A(2,2),A(2,3),0);
quiver3(0,0,0,A(3,1),A(3,2),A(3,3),0);
with the next result
instead of something like this
How can tell to quiver3 that plots all the vectors at the same time, or is there anyother command that can do this?

How to plot a multidimensional array in matlab?

I have a table as follows
system:index 2017_06_18 2017_06_19 2017_06_20 2017_06_21
2 612.8099664 1174.656713 1282.083251 815.3828357
3 766.4103726 1345.135952 1322.726083 749.998993
4 765.0230453 1411.669136 1350.437586 610.9541838
5 553.5858458 1374.14789 1152.086957 566.7924468
6 466.9780908 1311.903756 1060.494001 559.1982264
7 257.1162602 1270.182385 988.5455285 562.9224932
8 230.6611542 1310.971988 1001.548768 502.3266959
I want to plot a 2d-colormap representing system:index as y axis, dates as x axis and values under dates as colors. I tried with the following code but it did not give what I want.
clear
clc
filename = 'TurbidityDailyMean.xlsx';
data = xlsread(filename,'TurbidityDailyMean','A1:E8');
figure;
hold on
for i = 2:5
y = data(:,1);
x = data(:,i);
plot(x,y)
end
I need to map a colormap as mentioned above. But from what I tried it gives something else. And another fact is that I can't insert system:index and dates row into matlab with relevant data.
Thanks to my supervisor Dr.Kavinda,enter image description here the solution I adopted from him was as follows
data1 = csvread('TurbidityDailyMean.csv',1,1);
[mm,nn]=size(data1)
xx=ones(mm,nn);
yy=ones(mm,nn);
for j=1:nn
xx(:,j)=j;
end
for i=1:mm
yy(i,:)=i;
end
zz=data1;
h1=pcolor(xx,yy,zz);
shading flat
set(gcf,'color',[1,1,1])
axis([1 5 1 7]);
jet2=jet;
jet2(1,:)=1.0;
colormap(jet2)
caxis([0.0 0.13])
hold on
xlabel('Julian Day');
ylabel('y (?~1 km)');
colorb=colorbar;
set(get(colorb,'ylabel'),'String','Normalized Red Band Reflectance','fontsize',15,
'color', 'k');
set(get(colorb,'xlabel'),'fontsize',20, 'color', 'k');
grid on

How can I plot multiple 3D arrays on a single plot?

I have multiple arrays of 3D data. How can I plot them all on a single plot? The size of the arrays are not equal.
For example:
array1_xy = [1 2;3 4;5 6]
array1_z = [10;20;30]
array2_xy = [2 4;5 6;4 6;4 5]
array2_z = [10;20;50;10]
array3_xy = [1 4;1 6;1 3;1 5;1 1;3 4]
array3_z = [10;20;30;10;80;30]
How can I plot them on a single 3D plot with different markers?
You want the hold function.
From the link above:
x = -pi:pi/20:pi;
plot(sin(x))
hold on
plot(cos(x))
hold off
This will plot sin(x) and on the same axes it'll plot cos(x).
If you want to plot your arrays with plot3 function, you can still use hold on; and plot them in the same graph.
I assume you want to do a xyz scatter plot(?) in that case, use plot3. Details go help plot3
Details about markers, go help plot
Follow code does what you want.
plot3(array1_xy(:,1),array1_xy(:,2),array1_z,'x'); hold on;
plot3(array2_xy(:,1),array2_xy(:,2),array2_z,'o');
plot3(array3_xy(:,1),array3_xy(:,2),array3_z,'p');

Removing the line between two specific data points in Matlab

I am going to draw a graph in Matlab. The graph is quite simple and I am using the plot function.
Suppose the data that I want to plot is (0:1:10). I also put markers on my graph. Then, we have a line that has markers on coordinates (0,0),(1,1),(2,2),... etc.
Now, I want to remove the line between (2,2) and (3,3) without deleting the whole line. That is to say, my purpose is to get rid of a particular segment of the line without loosing the entire line or any marker points.
How can I do that?
Removing the section of line after you have plotted it is difficult. You can see that the line is made up of one MATLAB object by the following code:
x = 1:10;
y = 1:10;
H = plot(x, y, '-o');
get(H, 'children')
ans =
Empty matrix: 0-by-1
We can see that the line has no children, so there are no 'subparts' that we can remove. However, there are some cheeky tricks we can use to try to achieve the same effect.
Plot two lines separately
...using hold on. See Victor Hugo's answer. This is the proper way of achieving our goal.
Plot two separate lines in one
MATLAB doesn'y plot points with a NaN value. By modifying the input vectors, you can make MATLAB skip a point to give the effect of a broken line:
x = [0 1 2 2 3 4 5 6 7 8 9];
y = [0 1 2 nan 3 4 5 6 7 8 9];
plot(x, y, '-o');
This is equivalent to plotting a line from [0, 0] to [2, 2], skipping the next point, then starting again at [3, 3] and continuing to [9, 9].
'Erase' part of the line
This is the nastiest way of doing it, but is a cheap hack that could work if you can't be bothered with changing your input arrays. First plot the line:
x = 1:10; y = 1:10;
plot(x, y, '-o');
Now plot a white line over the part you wish to erase:
hold on
plot([2 3], [2 3], 'w');
As you can see, the result doesn't quite look right, and will respond badly if you try to do other things to the graph. In short, I wouldn't recommend this method but it might come in useful in desperate times!
Try the following:
y = [0.2751 0.2494 0.1480 0.2419 0.2385 0.1295 0.2346 0.1661 0.1111];
x = 1:numel(y);
plot(x(1:4), y(1:4), '-x')
hold
plot(x(5:end), y(5:end), '-x')

Need help in plotting lines between points

I need help in plotting lines between points.
Suppose, I start with creating 6 random points-
x = rand(6,1);
y = rand(6,1);
So my points are (x(1),y(1)), (x(2),y(2)), (x(3),y(3)), (x(4),y(4)), (x(5),y(5)), (x(6),y(6))
Now I want to draw straight lines between the points 1 & 5, 2 & 6, 3 & 4
and plot them in a single diagram. So I get 3 straight lines.
Any help would be highly appreciated.
You can do this with one call to PLOT. If you reshape your x and y data into matrices with each column containing a set of coordinates for one line, then PLOT will draw a different colored line for each column:
index = [1 2 3; 5 6 4]; %# The index to reshape x and y into 2-by-3 matrices
plot(x(index),y(index)); %# Plot the lines
Here are two ways to do this:
First way, using hold on. These lines are separate, i.e if you turn one red, the others will stay blue.
%# plot the first line
plot([x(1);x(5)],[y(1);y(5)]);
hold on %# this will prevent the previous plot from disappearing
%# plot the rest
plot([x(2);x(6)],[y(2);y(6)]);
plot([x(3);x(4)],[y(3);y(4)]);
Second way, making use of the fact that NaN does not get plotted. These lines are grouped, i.e. if you turn one red, all will be red.
%# create array for plotting
xy = NaN(8,2);
%# fill in data
xy([1 2 4 5 7 8],1) = x([1 5 2 6 3 4]);
xy([1 2 4 5 7 8],2) = y([1 5 2 6 3 4]);
%# plot
plot(xy(:,1),xy(:,2))