Visualizing 1D data in MATLAB - matlab

I have a vector which gives the speed of a rat over time. Could someone please help me on how I can show this data with a "color map" or "color bar". Basically I want to show each data point with a color.

As what Suever suggested, using imagesc is perfectly fine for your purposes. You can also add in a colour bar to give meaning of the mapped colours to the values in your vector. The y-axis won't have any meaning as you'll want to concentrate on the colours themselves. Therefore, you'll want to blank out the y-axis by grabbing a handle to the current axes in the plot and just setting the y-axis labels to blank.
As such, do something like this assuming that your data is stored in the vector data:
data = rand(1,100); %// random dummy data - 100 element vector
imagesc(data);
colorbar;
set(gca, 'YTick', []);
We get this image now:
Note that the colour bar on the right is scaled using the lowest and highest values in your data. The colours will be scaled so that it conforms to this lowest and highest value.

Related

plotting a text file with 4 columns in matlab

I want to plot a text file with 4 columns that first column in longitude,second in latitude, third is depth and forth is amount of displacement in each point.(it's related to a fualt)
-114.903874 41.207504 1.446784 2.323745
I want a plot to show the amount of displacement in each point (like images that we plot with imagesc),unfortunately "imagesc" command doesn't work for it.
how can I plot it?
Thanks for your attention
A simple way would be to use scatter3 and assign your displacements to be the colours. Note that you have to supply a size for this to work - I'm using [] (empty matrix) which will set it to default. If your four sets of values are four vectors of the same size, then it's just something like:
scatter3(lat,lon,depth,[],displacement, 'filled')
Values in displacement will be linearly mapped to the current colormap. 'filled' gives you filled markers rather than open ones (default marker is a circle but can be changed).
You can plot each point using plot3(longitude,latitude,depth). You can color each point according to the displacement in a for loop. The easiest way to do this is create a colormap, e.g. using jet and chosing the color according to the displacement.
figure;
hold on;
cmap = jet(256);
dispRange = [min(displacement),max(displacement)];
for k=1:size(longitude,2)
c = cmap(1+round(size(cmap,1)*(displacement(k)-dispRange(1))/dispRange(2)),:);
plot3(longitude(k),latitude(k),depth(k),'o', ...
'MarkerEdgeColor',c,'MarkerFaceColor',c);
end

Set legend from looped data points matlab

I would like to scatter plot different sets of data points. Each point should have a different markerfacecolor, but within a given set, they would all have the same markeredgecolor.
I got this to work by looping over single scatter points individually, because markerfacecolor seems to only be able to take scalar points. The way I do it is that I loop through my data, and look for the appropriate color.
This works fine because I can define each point separately, but it becomes a problem when trying to set up the legend. It tries to list all the different points, but what I'd like is just an empty circle (markerfacecolor white or transparent), with each set having their specific markeredgecolor.
I hope this is clear enough. Thank you.
Mike
I've had luck using patches, setting 'FaceColor', 'EdgeColor' to 'none', 'MarkerEdgeColor' to 'flat' and setting the 'FaceVertexCData' to a Nx3 matrix where each row is a color corresponding the the points specified by you Nx1 'XData', 'YData' and 'ZData'.
h = patch('parent',gca,...
'XData',x,...
'YData',y,...
'ZData',z,...
'FaceColor','none',...
'EdgeColor','none',...
'MarkerFaceColor',faceColor,...
'MarkerEdgeColor','flat',... This is what sets the Edge of each marker
'FaceVertexCData',C) % C is a Nx3
I don't have access to Matlab at the moment, and Octave does not seem to have exactly the same functionality. Check out the Matlab patch properties documentation for more information.

Multiple Marker Types for one Function

I am using the plot3c function to map a matrix of data in the x,y,z, and color axes. For clarification, my z-data and color data are one and the same, but represented on the two axes. Due to instrumental limitations, my data has a set of false color values where an unreadable point is represented with a 0. I would like to have every z/color value of 0 represented with a different marker type than the rest of the data. I know how to change marker type for a plot but I do not know how to set a marker type for specific values within a plot. How can I do this?
You could just overlay a second plot that only plots where z is 0:
% Your original plot, I'm assuming plot3c(x,y,z,z)
hold on
mask = z==0;
plot3(x(mask), y(mask), z(mask), '^')
Or more efficiently as suggested in radarhead's comment:
mask = z==0;
plot3c(x(~mask), y(~mask), z(~mask), z(~mask))
hold on
plot3(x(mask), y(mask), z(mask), '^')

How do I create a scatter plot with graduated marker colours in MATLAB?

I would like to plot a simple scatter graph in MATLAB, with marker colours varying from one end of the spectrum to the other (e.g. red, orange, yellow....blue, purple).
My data compares the amount of water in a river with the quality of the water, over time (3 simple columns: time, amount, quality). I would like to plot the x,y scatter plot of amount vs quality, but with the colour progressing over time, so that it is possible to see the progression of the quality over time.
I will need to produce many graphs of this type, so if I can find a piece of code that will work for any length of dataset, that would be really useful.
Many thanks in advance for helping a Matlab novice!
You can use the color argument of scatter
If your data are already sorted in time than simply use:
% let n be the number of points you have
cmp = jet(n); % create the color maps changed as in jet color map
scatter(x, y, 10, cmp, 'filled');
Otherwise you need to sort your data first:
[time, idx] = sort(time);
x = x(idx);
y = y(idx);
cmp = jet(n); % create the color maps changed as in jet color map
scatter(x, y, 10, cmp, 'filled');
The simplest way to color a scatter plot by an additional variable is to simply pass it as the "color"-argument. Say you have x, y, and time (where time is a numeric vector. If time contains date strings instead, call datenum on it, first). Then you can write
scatter(x,y,[],time,'filled')
The colorbar axes will then show you which point in time a specific color corresponds to. Importantly, this will properly advance colors even in case the time between measurements isn't uniform.
/aside: The default colormap is jet, which is pretty bad for visualizing smooth transitions, I suggest you download a perceptually improved colormap from the File Exchange. To use it to set the colormap, you can then call
cmap = pmkmp(length(time));
colormap(cmap);

"2D" plot with square colour in grid indicative of value - the ArcGIS look

I've looked up surface plots and contour plots but cannot find the appropriate format. I am not concerned with height and want a bird's eye view of a flat grid - much like the format of ARCGis.
I have random values between 0 to 10 in a 7x7 matrix.
I want to draw a grid 7 by 7 that is 2D where the individual grid cells are block-coloured according to their value.
The top left value of the matrix is the top left cell of the plot and so on. They should align.
I don't want to have to export matlab's output and put it back into ARCG each time I get a result. Thanks for your advice guys!
If you have a 2D array, you should be able to just "show" it like an image. Try using imagesc() with colormap on. You can change the colormap, size, etc.