How do I set a surf to one color (no gradient) in my matlab-plot? - graphics

My dataset consists of three vectors (x,y and z). I plot these values as dots in a 3d-plot with plot3(x,y,z), which is fine. I also want to show a plane in the same plot. To get the data of this plot I use linear regression on x and y to get a new z.
This is how it looks:
(source: bildr.no)
I want the surf to be filled with only one color (say light blue or gray) and set the opacity, to make it see-through. How can I do this?

The easiest way to create a surface that has just 1 color and a given transparency value is to set the 'FaceColor' and 'FaceAlpha' properties of the surface object:
hSurface = surf(...your arguments to create the surface object...);
set(hSurface,'FaceColor',[1 0 0],'FaceAlpha',0.5);
This example sets the surface color to be red and the transparency to 0.5. You can also set the edge properties too (with 'EdgeColor' and 'EdgeAlpha').

It is not clear to me what you want to do. When you say one color for the surf, do you mean exactly one color, or do you mean you want shades of gray?
Here is some code that will do a variety of things, you can choose which lines to use:
x = rand(1,20);
y = rand(1,20);
z = rand(1,20);
[X,Y] = meshgrid(linspace(0,1,10),linspace(0,1,10));
Z = rand(10)*0.1;
clf
plot3(x,y,z,'.');
hold on
h = surf(X,Y,Z)
hold off
%% This will change the color
colormap(copper)
%% This will remove colordata
set(h, 'cdata',zeros(10))
%% This will make transparent
alpha(0.5)

Completing the answer from gnovice, an extra ingredient in set(hsurface...) may be required (Matlab R2010b 64):
hSurface = surf(...your arguments to create the surface object...);
set(hSurface, 'FaceColor',[1 0 0], 'FaceAlpha',0.5, 'EdgeAlpha', 0);
to make invisible the point-to-point edges of the plotted surface

#matlabDoug has what you need, I think. The property cdata holds color data that gets a color map applied to it. Setting it to an array the same size as your surface data, with each element in that array having the same value, will make your surface one color. With the default color map, setting everything in cdata to zero will make your surface blue, and setting everything to 1 will make the surface red. Then you can play with the alpha to make it transparent.

Related

How to set relative position (oCoords) in FabricJs?

I have a Text in fabricJs. I set top and left.
This sets the aCoords properly to those values.
However the oCoords dont match. And the Text is not displayed at the right position.
I suspect that I need to set to oCoords somehow. So that the Text is displayed at the right pixel coordinates (top & left) on the canvas.
aCoords and oCoords are two different things and should not be in sync.
In your comment you speak about scaled canvas.
Top and Left are 2 absolute values that represent the position of the object on the canvas. This position match with the canvas pixels when the canvas has a identity transform matrix.
If you apply a zoom, this coordinates diverge.
To get the position of pixel 300,100 of the scaled canvas on the unscaled canvas, you need to apply some basic math.
1) get the transform applied to the canvas
canvas.viewportTransform
2) invert it
var iM = fabric.util.invertTransform(canvas.viewportTransform)
3) multiply the wanted point by this matrix
var point = new fabric.Point(myX, myY);
var transformedPoint = fabric.util.transformPoint(point, iM)
4) set the object at that point.

networkx: set different alpha for each node?

I want to set a different alpha for each node. It's easy to set a different color for each node but alpha only excepts a single value. Anyone know how to do this?
(My goal is to have a color gradient from blue to red. If the value is below the midpoint it's blue and if above it's red. Alpha would set the gradient.)
nx.draw_networkx_nodes(g,pos=pos,nodelist=nodelist,
node_color=node_color, alpha=1.0)
The alpha value will control transparency, not color.
If you are already supplying the node_color parameter, your nodes will be colored.
If you want them to go from blue to red based on those intensities, instead of alpha, use a colormap. E.g. seismic colormap goes from red to blue.
nx.draw_networkx_nodes(g, pos=pos, nodelist=nodelist, node_color=node_color, cmp='seismic')
EDIT:
I stumbled across some odd behavior trying to dig deeper into this.
I would have thought that networkx's plotting would behave like matplotlib's scatter plot, and accept rgba colors, allowing you to specify particular colors and transparency. But when I call draw_networkx_nodes with an array of 4-tuples to denote rgba colors, it sets all alpha values to 1.
rgba_colors = np.zeros((5,4))
rgba_colors[:,0] = np.linspace(1,0,5) #red
rgba_colors[:,2] = np.linspace(0,1,5) #blue
rgba_colors[:,3] = np.linspace(0.1,1,5) #alpha values
plt.scatter([1,2,3,4,5],[1,2,3,4,5], color=rgba_colors)
This plots some points with varying color and transparency. But, if you try plotting with networkx, not only does the resulting plot have no transparency, the variable rgba_colors has actually been modified.
G = nx.complete_graph(5)
pos = {0: [0,0], 1: [1,1], 2: [2,2], 3: [2,1], 4: [2,3]}
nx.draw_networkx_nodes(G, pos=pos, node_color=rgba_colors)
print(rgba_colors)
I'm not sure why this is happening.

3D line multiple colors

I need help with Matlab.
I have an Excel sheet with three columns: X, Y and Z. I have used plot3 function to make one 3D curve.
But I need to vary it in colors.
What function/functions do I need to make X, Y and Z in different 3 colors(each column one color)?
Could you please send me link, where I can find out the way, or just write the function/functions needed for it?
Here is the code:
VCG=xlsread('VCGsheet.xls');
figure(1)
plot3(VCG(:,1),VCG(:,2),VCG(:,3));
grid on
I know that plot3 isn't suitable for it.
I'm not aware of a native command that draws line with varying color.
I will also assume that VCG vector is a vector of RGB values, so one color per row.
RGB = VCG(:,1:3);
If you want, you can replace it (VCG(:,1:3)) with any other vector of color derived from your data. Here is an example if you have a single value that can be calculated for each of your points, in the vector T for example, and that you want to show as a color.
map = jet(256);
RGB = map(round((T(:)-min(T(:)))/(max(T(:)) - min(T(:)))*255)+1,:);
For the plot, I propose two different ways:
you can make use of the scatter3 function
It will print points, but no lines connecting the points. The color vector is set with VCG(:,1:3)
colormap('jet')
scatter3(VCG(:,1), VCG(:,2), VCG(:,3), 70, RGB, 'filled');
You can make a direct use of line in a for loop.
It is a bit slower, but it is generally ok for graphs.
for i=2:size(VCG,1)
line(VCG(i-1:i,1), VCG(i-1:i,2), VCG(i-1:i,3), 'color', RGB(i-1,:));
end
If you want both, just use the hold function
I hope I understood what you needed !

How to find custom shape speicific area?

please , see following image, here you can see blue rectangle is custom shape bounds and custom shape is shoe , i want to find area of a portion written in image and i want that area in form of rectangle
do is there any path iterator concept ?
Note
custom shape i derived from image of the same size.
I would do it like this:
1.create table for all bounding box-rect perimeter lines
each value in it will represent the empty space length form border line to shape
something like this:
the values are found by simple image scanning until first non space color found
2.now bruteforce find the biggest rectangle area
x,y = top left corner
for xs = 1 to bounding box width
now scan the max valid height of rectangle from x to x + xs (x grows to the right)
// it should be the min y0[x..x+xs]
remember the biggest valid area/size combination
do this for all 4 combinations (star from the other corners)
I now Brute-force is slow but
you can divide perimeter lines not by pixels but with some step instead
also I am sure this can be optimized somehow
for example by derivation of perimeter find the extremes and check from them backwards
when the size will start shrinking then stop ...
of course take in mind that on complicated shapes this optimization will not work ...

Built in colormaps in Matlab

I want a lighter version of the "cyan" color, using the function colormap('cyan'). How do you do this?
Check out the function BRIGHTEN:
X = spiral(8);
image(X)
colormap(winter), colorbar
brighten(0.6)
Another trick is to right click on the colorbar and select Interactive Colormap Shift, this allows to shift the color-to-data mapping using mouse dragging.
Pure cyan is represented by the RGB triple [0 1 1]. To make it lighter, just increase the red component (ex: [0.5 1 1]), thus moving it closer to pure white ([1 1 1]). If you want to make a colormap that spans from pure cyan through lighter shades of cyan all the way to pure white, you can do the following:
nValues = 128; %# The number of unique values in the colormap
map = [linspace(0,1,nValues)' ones(nValues,2)]; %'# 128-by-3 colormap
Now you can set the colormap to the one made above using the COLORMAP function:
colormap(map);
For more discussion of colors in MATLAB, check out this link.
For me colormap('cyan') fails because cyan is undefined.
However, you can create your own colors easily. If cyan is equivalent to [0,1,1] a lighter color would be [0,1,1] + [.1,0,0] = [.1,1,1] or rather just increase the R in RGB to increase the luminosity.

Resources