Matlab plotting a value which is referenced by another 2 values - matlab

In every iteration I get these values. For instance;
a is 2, b is 3, gg(a,b) is 70
a is 2, b is 4, gg(a,b) is 72
a is 2, b is 5, gg(a,b) is 76
I want to plot these in a one plot like 'a' in the x axes, 'b' in the y axes and gg(a,b) is the value which is referenced by a and b. I also want to show gg(a,b) values in a colormap. I tried but not achieved yet. Could you help please?
Here is what I've tried. I don't want 3d but don't know how to plot. Lets say gg is a matrix includes 20 columns and 5 rows.
gg=rand(5,20);
for a=1:5
for b=1:20
hold on
scatter3(gg(a,b),a,b)
xlabel('gg(a,b)'), ylabel('a'), zlabel('b')
colormap(jet)
view(3)
end
end

Here is a (simpler) code for both:
gg = rand(5,20);
[a,b] = ndgrid(1:5,1:20);
figure
scatter(a(:),b(:),[],gg(:))
colormap(jet)
xlim([0 6])
xlabel('a')
ylabel('b')
colorbar
figure
colormap(jet)
imagesc(1:5,1:20,gg.')
xlabel('a')
ylabel('b')
axis xy
colorbar
which creates:

I think it should be gg=rand(5,20);

Related

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

Shift Plot Data Along X Axis Matlab

I cannot get Matlab to plot a a second time series to specific points along the x axis.
My data are two time series. Time series A is a 5 X 1 and time series B is a 7 X 1. I need A to plot on xticklabels 1-5. Then, with 'hold on', I need time series B to be shifted to the right to plot on xticklabels 6:12. I keep getting the second plot to plot directly over the first plot without the shift occurring. I've tried among other things -->
set(gca,'XTick',[6 7 8 9 10 11 12]);
and it displays x axis numbers shifting but the data does not plot in positions 6:12. Any help is much appreciated. I've seen some online answers but can't seem to get it correct.
In Matlab, you can plot something using plot(xArray, yArray);. If you want to shift the plot along the x axis, you could use plot(xArray + amountToShift, yArray);.
As I believe shifting is not what your real problem is, I've added an example where data gets plotted in the way you described:
A = [1, 2, 2, 1, 3];
tA = 1:5;
B = [3, 5, 2, 1, 2, 7, 5];
tB = 6:12;
plot(tA, A);
hold on;
plot(tB, B);

MATLAB: how to customize non linear X axis (for example ticks at 1,2,3,4,5,20,100,'string')

I'm using the MATLAB plot feature to compare two vectors. I would like my X axis to represent 1 through 7, and then 14, 21, and then a category at the end for points with undetermined X values..(I'm also not sure how to represent these numberless point (they have Y values, just no X values) I could assign a large number outside any of my X values (1000) to these points, do the 1-7,14,21,1000 and then change the 1000 label to my 'string for un-numbered points'. ??
Here is a way to do it based on the example by The Mathworks here.
The trick is to create 2 x axis with different ranges and make one of them transparent. You can then play around with its properties. I modified a bit their code but kept most of their comments because they explain well the steps.
For the demo I used scatter to represent the points and colored the "good" points in red and the other point (here only 1) in green. You can customize all this of course. For instance I used a value of 100 and not 1000 for the x value of the 2nd axes, but I'll let you figure out how to modify it all to change the output as you wish.
clear
clc
close all
%// Create axes 1 and get its position
hAxes1 = axes;
axes_position = get(hAxes1, 'Position');
%// Create axes 2 and place it at the same position than axes 1
hAxes2 = axes('Position', axes_position);
%// Your data go here.
x = [1 7 14 21 100];
y = rand(1, length(x));
%// Plot the two sections of data on different axes objects
hPlot1 = scatter(hAxes1, x(1:4), y(1:4),40,'r','filled');
hold on
hPlot2 = scatter(hAxes2, x(end), y(end),40,'g','filled');
%// Link the y axis limits and fontsize property of the axes objects
linkaxes([hAxes1 hAxes2], 'y');
linkprop([hAxes1 hAxes2], 'FontSize');
%// Set the x range limits and tick mark positions of the first axes object
set(hAxes1, 'XLim', [1 25], ...
'XTick', [1 7 14 21])
%// Set the x range limits and tick mark positions for the second axes object.
%// Also set the background color to 'none', which makes the background
%// transparent.Add the label for the "super points".
set(hAxes2, 'Color', 'none', ...
'YTickLabel', [], ...
'XLim', [95 100], ...
'XTick', 100,'XTickLabel',{'Super Points'})
And the output:
EDIT
If you wish to add a fit line, I suggest adding a 3rd axes at the same position than the other 2, make it transparent and remove its X- and Y-ticks.
i.e. Add something like this:
hAxes3 = axes('Position', axes_position,'Color','none','YTick',[],'XTick',[]);
And in the call to polyfit/polyval, in my example you want to get only the first 4 elements (i.e. the red ones).
Hence:
p = polyfit(x(1:4),y(1:4),1);
y_poly = polyval(p,x(1:4));
And then plot the line on hAxes3:
hPlot3 = plot(x(1:4),y_poly)
Output:

Specifc values over Axis in matlab

I want to plot 3D graph where Y-axis has only values 9, 99 , 999. I tried to use
Y= [9, 99 , 999];
set(gca,'YTickLabel',Y);
set(gca,'YTick',Y);
I want to have 3 points 9 at the beginning, 99 in the middle, and 999 at the end. Is it possible to do that?. I tried also with Ylim, but couldn't help
You can change your y-axis to a logarithmic scale.
Use:
set(gca, 'YScale', 'log');
If you also set
ylim([9 999]);
as you had pointed out, you should get the desired result.
Another solution:
F = [1 2 3];
Y = [9, 99 , 999];
Y_ = [9, (999-99)/2 , 999];
figure;
subplot(1,2,1)
plot(Y,F,'*-');
set(gca,'XTickLabel',Y,'XTick',Y);
subplot(1,2,2)
plot(Y_,F,'o-');
set(gca,'XTickLabel',Y,'XTick',Y_);
You can see the difference:

Matlab Indefinite Category Axis

I'm interested in making a plot that has a category axis (e.g. set(gca,'YTickLabel', {'A', 'B', 'C'});)
However, I don't know the number of categories ahead of time. In fact, the categories are a vector of nonconsecutive numbers that I nevertheless want to plot consecutively as categories.
E.g. Vector = [5 7 9 2 6]
where Vector is a vector of integers and can be of any size.
I want to make an image() of a 2d matrix, where the Y axis has these integers applied to it as labels or categories for each tick. How can I do that?
I was able to figure it out, here is an example:
ydata=[5 7 9 2 6];
Z=rand(size(ydata,2),10); %random numbers where Y dimension is size of ydata
figure
imagesc(Z) %Make the image of Z
set(gca,'YTick',1:1:size(ydata,2)); % Set only ONE tick
% mark for each value of ydata
set(gca,'YTickLabel', ydata); %Label the tick marks with the values in ydata.