This question already has answers here:
Show two different plots in one plot
(2 answers)
MATLAB - Plot multiple data sets on a scatter plot
(3 answers)
Closed 3 years ago.
I have a heat map of values generated by "pcolor" in MATLAB. I would like to plot a line plot on top of that.
I haven't found a proper solution in any measure yet.
The following code generates a "heat map" sort of output
hc = pcolor(middle_long, middle_height, middle_no2);
set(hc, 'Edgecolor', 'none');
c = colorbar;
caxis([0 0.015]);
axis([min(middle_long(:,1)) max(middle_long(:,1)) 0 1000])
The following code generates a line plot
plot(longflag, hflag)
The following are figures of the individual plot types that I would like to join, with an "example" of the final product I'd like listed afterwards:
Try something like this. Note the hold on part, which prevents plot from deleting the image produced by pcolor:
pcolor(rand(10))
colormap bone
axis xy
hold on
plot([1 10], [10 1], 'r')
Related
This question already has answers here:
Getting a second row of xlabels in matlab graph?
(1 answer)
add data label to a grouped bar chart in matlab
(1 answer)
Closed 7 months ago.
I have two types of plots.
In fist case I need to label individual bar graphs in the grouped bar plot. Here is the example
a=4.54,88.63,27.27,77.27,54.54;31.81,61.36,38.63,68.18,54.54;54.54,61.36,59.09,54.54,50;68.18,27.27,56.81,34.09,50;90.90,11.36,68.18,15.90,40.90];
b=0.40,0.55,0.70,0.85,1;1.39,1.54,1.69,1.84,1.99;2.340,2.49,2.64,2.79,2.94;3.36,3.51,3.66,3.81,3.96;4.29,4.44,4.59,4.74,4.89];
figure,
hold on
for i=1:5
bar(b(i,:),a(:,i))
end
figure,
hold on
for i=1:3
plot(b(i,:),a(:,i))
end
For the bar plot, I like to lable on the horizontal as shown in figure, the numbers are stored in an other matrix, say b
2) Similarly, I also want to XTickLables using values in b for the line plot.
you have first to define xticks and XTickLables
for example you can write:
a=[....];
b=[....];
c=[0.5,1,1.5,2,3];
D = {'0.5','1','1.5','2','3'};
figure,
hold on
for i=1:5
bar(b(i,:),a(:,i));
xticks(i) = c(i); %% first you define the ticks
XTickLables{i} = d(i); %% second you define the labels
end
for the exact same plot like the one you showed, I think you need two nested for loops.
more on that here: https://de.mathworks.com/help/matlab/ref/xticklabels.html
I would like to create the nearest closed contour right now when I do contour its broken in chunks. Here is the picture of contour:
I wanted a closed contour around the center.
I need something like this
axes(handles.axes1);
cla(handles.axes1);
hold on;
pcolor(xC,yC,z_Diff); shading flat
colormap(handles.axes1, 'jet');
freezeColors(handles.axes1)
[C,hfigc] = contour(xC,yC,z_Diff,[thresh_crater thresh_crater]);
set(hfigc, ...
'LineWidth',1.0, ...
'Color', [1 0 0]);
hold off;
I am trying enclose a box around the crater area which include the hole and outer piled stuff:
I am getting the results below based on largest contour area:
Here is the image I want it to be like this:
This will be problem if i have more than 1 crater and i want to box them.
Is there an easy command to have a plot like the blue line in the picture (excel)? Matlab defaults to produce something like the line in red. The only way I know to do this is to issue a plot command for each segment of the line:
for i=2:n-1
plot([data(i-1,1) data(i,1)],[data(i-1,2) data(i,2)],'-b'); hold on;
end
You can just plot the entire array and let plot automatically draw straight line segments between each of the points. This is the default behaviour when plotting things in MATLAB. MATLAB plotting smooth lines is not the default behaviour when the plot is produced, so I'm not sure where you're getting that information.
You would need to perform some sort of spline interpolation to get the red line, but you desire the blue curve and so plotting the entire array in a single plot command should suffice.
It's as simple as:
plot(data(:,1), data(:,2), '-b');
Just to be sure that we're on the same page, I'm going to reproduce your data then use the above command to plot the data so you can see for yourself that the behaviour you desire is achieved:
data = [0 0; 1 1; 2 4; 3 6; 4 4]; %// Your data reconstructed
plot(data(:,1), data(:,2), '-b'); %// Main plotting code
%// Some extras
xlim([0 4.5]);
ylim([0 7]);
grid;
I've added in some extra code to get the plot to look like your example. I've made the x-axis limits go up to 4.5 and the y-axis limits go up to 7. I've also placed a grid in the plot.
We get:
This question already has answers here:
3D scatter plot with 4D data
(2 answers)
Closed 7 years ago.
I have a an nx4 matrix where each row is an observation.
The three three columsn represent by variables, and the fourth a 'fitness' parameter.
I would like to show this in a 3D scatter plot, where each axis is one of my variables and then color each point depending on how close to one of the extremes in my fourth column it is.
For example, say fitness ranged between 0 and 1. I would want observations with fintess 0 to be blue, those with fitness 1 to be red and those in-between some corresponding shade.
Any advice on how best to do this?
Thanks!
The function scatter3 has a color input argument. But you need to define the size of the markers also.
% Generate example data,
X=rand(10,1)*10;
Y=rand(10,1)*3;
Z=rand(10,1)*5;
fit=rand(10,1)*3+10;
scatter3(X,Y,Z,ones(size(X))*40,fit,'fill')
Use scatter3 with an appropriate colormap:
scatter3(data(:,1), data(:,2), data(:,3), 10, data(:,4), '*')
colormap(hsv)
colorbar
where
data is your matrix
10 is markersize
'*' is the marker shape
hsv is the selected colormap
This question already has answers here:
How to create a custom colormap programmatically?
(2 answers)
Closed 7 years ago.
I have a contour plot with data that goes from -90 to 90 degrees. for now i am using jet so I have a map that looks like this
I have been asked to change the colormap so that instead of having a gradient, I have a fixed color for each 5 degress (so I believe 36 colors). Also i was thinking of maybe having same colors for the interval [5 10] and [-10 -5], and so on if that makes sense.
My code is quite long because i have a lot of data to process, but that's part of it just so you can see what function i am using to plot this
%%
x1=data(:,5); %x location
y1=data(:,16); %y location
z1=phi*90; %angle phi
z2=gamma*90; %angle gamma
n=300; precision of grid
%Create regular grid across data space
[X,Y] = meshgrid(linspace(min(x1),max(x1),n), linspace(min(y1),max(y1),n));
figure(3);
contourf(X,Y,griddata(x1,y1,z1,X,Y),100,'EdgeColor', 'None')
%title('Variation of In-plane angle \phi')
axis equal
axis ([0 8000 0 12000])
axis off
h=colorbar;
caxis([-90 90])
set(h, 'YTick', [-90:15:90])
Does anyone know how to create this colorbar?
Cheers
Every colormap-generating function in Matlab, including jet, takes an argument that specifies how many colormap entries there should be. In your case, you want 180 / 5 = 36 discrete colors:
colormap(jet(36))
To make sure the 36 colors cover exactly the 5 degree steps, set the color axis explicitly:
caxis([-90 90])
The result looks e.g. like this: