I currently trying to use plot3d function from rgl library.
open3d()
x = usage[,5]
y = usage[,6]
z = usage$transfer
plot3d(x,y,z, type='h')
I was wondering if anybody can help me change the colour of the lines such that the positive z will be red and the negative z will be blue.
Thanks!
This can be achieved by
plot3d(x, y, z, type = 'h', col = ifelse(z < 0, 'blue', 'red'))
Related
I am using python plotly and am trying to overlay grid lines on a contour plot. The following code does not generate an error but it also does not show grid lines. How can I make the grid lines show?
I didn't find a good way. The grid layer is there (you can see it if you add opacity<1 in your contour) but you cannot put the grid layer above the traces (or at least I didn't find a way reading the doc and the code).
I ended up rebuilding the grid on top:
def add_box(lfig, x, y):
lfig.add_trace(go.Scatter(
x=x,
y=y,
opacity=0.5,
marker_color='white',
line_width=1,
))
def compute_horizontal_lines(x_min, x_max, y_data):
x = np.tile([x_min, x_max, None], len(y_data))
y = np.ndarray.flatten(np.array([[a, a, None] for a in y_data]))
return x, y
def compute_vertical_lines(y_min, y_max, x_data):
y = np.tile([y_min, y_max, None], len(x_data))
x = np.ndarray.flatten(np.array([[a, a, None] for a in x_data]))
return x, y
hx, hy = compute_horizontal_lines(200, 20000, range(-150, 180, 30))
vrange = [100*i for i in range(3, 9)] + [1000*i for i in range(1, 10)] + [10000+1000*i for i in range(1, 5)]
vx, vy = compute_vertical_lines(-180, 180, vrange)
add_box(fig, hx, hy)
add_box(fig, vx, vy)
fig.show()
which give me something like:
contour plot with a grid on top
the following procedure is shutting dow my Rstudio: I understand is any of the akima or rgl packages or both. How to solve this? data here
s=read.csv("GRVMAX tadpoles.csv")
require(nlme)
t=s[s$SPP== levels(s$SPP)[1],]
head(t)
t=na.omit(t)
t$TEM=as.numeric(as.character(t$TEM))
library(akima)
x=t$TEM
y=t$value
z=t$time
spline <- with(t,interp(x,y,z,duplicate="median",linear=T))
# rotatable 3D plot of points and spline surface
library(rgl)
open3d(scale=c(1/diff(range(x)),1/diff(range(y)),1/diff(range(z))))
with(spline,surface3d(as.character(x),y,z, col))
points3d(x,y,z, add=T)
title3d(xlab="temperature",ylab="performance",zlab="time")
axes3d()
interp() causes the problem. I think the reason is that scale of y is much different from x (the algorithm of interp() is basically for contour of spatial map). So interp() run when you give y changed scale. (Note; I did y*10 and output/10 but maybe it is a rough scale change. It whoud be better to concider methods of changing scale)
library(nlme); library(akima); library(rgl)
s = read.csv("GRVMAX tadpoles.csv")
t = s[s$SPP == levels(s$SPP)[1],]
t = na.omit(t)
head(t)
t$TEM = as.numeric(as.character(t$TEM))
x = t$TEM
y = t$value * 10 # scale change
z = t$time
spline <- interp(x, y, z, duplicate = "median", linear = T) # with() is unnecessary
spline$y <- spline$y / 10 # rescale
y <- y / 10 # rescale
open3d() # Is scale needed ??
# persp3d() can directly take interp.obj as an argument
persp3d(spline, col = "blue", alpha = 0.5, axes = F, xlab="", ylab="", zlab="")
points3d(x, y, z, add=T)
title3d(xlab="temperature", ylab="performance", zlab="time")
axes3d()
I'm trying to implement a paper. In it I need to calculate the centre of gravity and second order moment of an image.
The equations of centre of gravity and second order moment are respectively given as:
Im having trouble trying to code this in Matlab ss from what I understand p(x,y) is the pixel of the image, but I'm having trouble what y represents and how would I implement in in the sum function. This is my implementation of the first equation but since I did not incorporate the y in there I'm sure the result given is wrong.
img = imread(path);
m = numel(img);
cog = sum(img(:))/m;
i think, m should be the maximum of y, because f2 is a function of x which means in Matlab it should be a vector.
try this code to implement f2:
img = magic(10)
m = 10;
temp = 0;
for y = 1:m
temp = temp+y*img(:,y);
%temp = temp+y*img(y,:); % depends on your image coordinates system
end
f2 = temp/m
Try the following code that uses vectorized anonymous functions.
% Read the image into an array (3 dimensions).
% Note: you may need to convert to doubles
img = im2double(imread(path));
% Get the size (may need to switch m and n).
[m, n, o] = size(img);
% Create y vector
y = 1:m;
% Create functions (not sure how you want to handle the RGB values).
f2 = #(x, p) sum(y.*p(x,:,1)/m);
f3 = #(x, p) sum(y.^2.*p(x,:,1)/(m^2));
% Call the functions
x = 10; % Some pixel x position
f2_result = f2(x, img);
f3_result = f3(x, img);
Note: I may have the x and y switched depending on the orientation of your image. If that's the case then switch things around like this:
[n, m, o] = size(img);
...
f2 = #(x, p) sum(y.*p(:,x)/m);
etc...
I'm not at work so I can't run the im2double function (don't have the library) but I think it will work.
I am plotting a 7x7 pixel 'image' in MATLAB, using the imagesc command:
imagesc(conf_matrix, [0 1]);
This represents a confusion matrix, between seven different objects. I have a thumbnail picture of each of the seven objects that I would like to use as the axes tick labels. Is there an easy way to do this?
I don't know an easy way. The axes properties XtickLabel which determines the labels, can only be strings.
If you want a not-so-easy way, you could do something in the spirit of the following non-complete (in the sense of a non-complete solution) code, creating one label:
h = imagesc(rand(7,7));
axh = gca;
figh = gcf;
xticks = get(gca,'xtick');
yticks = get(gca,'ytick');
set(gca,'XTickLabel','');
set(gca,'YTickLabel','');
pos = get(axh,'position'); % position of current axes in parent figure
pic = imread('coins.png');
x = pos(1);
y = pos(2);
dlta = (pos(3)-pos(1)) / length(xticks); % square size in units of parant figure
% create image label
lblAx = axes('parent',figh,'position',[x+dlta/4,y-dlta/2,dlta/2,dlta/2]);
imagesc(pic,'parent',lblAx)
axis(lblAx,'off')
One problem is that the label will have the same colormap of the original image.
#Itmar Katz gives a solution very close to what I want to do, which I've marked as 'accepted'. In the meantime, I made this dirty solution using subplots, which I've given here for completeness. It only works up to a certain size input matrix though, and only displays well when the figure is square.
conf_mat = randn(5);
A = imread('peppers.png');
tick_images = {A, A, A, A, A};
n = length(conf_mat) + 1;
% plotting axis labels at left and top
for i = 1:(n-1)
subplot(n, n, i + 1);
imshow(tick_images{i});
subplot(n, n, i * n + 1);
imshow(tick_images{i});
end
% generating logical array for where the confusion matrix should be
idx = 1:(n*n);
idx(1:n) = 0;
idx(mod(idx, n)==1) = 0;
% plotting the confusion matrix
subplot(n, n, find(idx~=0));
imshow(conf_mat);
axis image
colormap(gray)
What is the best way to draw a line over a black and white (binary) image in MATLAB, provided the start and end coordinates are known?
Please note, I am not trying to add an annotation line. I would like the line to become part of the image.
You may want to look at my answer to an SO question about adding a line to an image matrix. Here's a similar example to the one I have in that answer, which will make a white line running from row and column index (10, 10) to (240, 120):
img = imread('cameraman.tif'); % Load a sample black and white image
x = [10 240]; % x coordinates
y = [10 120]; % y coordinates
nPoints = max(abs(diff(x)), abs(diff(y)))+1; % Number of points in line
rIndex = round(linspace(y(1), y(2), nPoints)); % Row indices
cIndex = round(linspace(x(1), x(2), nPoints)); % Column indices
index = sub2ind(size(img), rIndex, cIndex); % Linear indices
img(index) = 255; % Set the line points to white
imshow(img); % Display the image
And here's the resulting image:
If you are bothered by exceptional cases of other methods here's a bullet-proof method that results in a line:
whose pixels always touch each other during the whole length of the line (pixels are 8-neighbors to each other),
density of the line is not dependent on the additional parameter, but is determined flexibly to accommodate guarantee from the first point.
Inputs (convenient for making function out of this code):
img - matrix that contains image,
x1, y1, x2, y2 - coordinates of the end points of the line to be drawn.
Code:
% distances according to both axes
xn = abs(x2-x1);
yn = abs(y2-y1);
% interpolate against axis with greater distance between points;
% this guarantees statement in the under the first point!
if (xn > yn)
xc = x1 : sign(x2-x1) : x2;
yc = round( interp1([x1 x2], [y1 y2], xc, 'linear') );
else
yc = y1 : sign(y2-y1) : y2;
xc = round( interp1([y1 y2], [x1 x2], yc, 'linear') );
end
% 2-D indexes of line are saved in (xc, yc), and
% 1-D indexes are calculated here:
ind = sub2ind( size(img), yc, xc );
% draw line on the image (change value of '255' to one that you need)
img(ind) = 255;
Here's the example image with three lines drawn on it:
This algorithm offers one approach.
It actually is just a modification on plesiv's answer. I'm drawing thousands of lines over an image and I need to increase the performance. The most improvement made by omitting interp1 calls and using integer variables made it slightly faster. It performs about 18% faster on my PC comparing to plesiv's code.
function img = drawLine(img, x1, y1, x2, y2)
x1=int16(x1); x2=int16(x2); y1=int16(y1); y2=int16(y2);
% distances according to both axes
xn = double(x2-x1);
yn = double(y2-y1);
% interpolate against axis with greater distance between points;
% this guarantees statement in the under the first point!
if (abs(xn) > abs(yn))
xc = x1 : sign(xn) : x2;
if yn==0
yc = y1+zeros(1, abs(xn)+1, 'int16');
else
yc = int16(double(y1):abs(yn/xn)*sign(yn):double(y2));
end
else
yc = y1 : sign(yn) : y2;
if xn==0
xc = x1+zeros(1, abs(yn)+1, 'int16');
else
xc = int16(double(x1):abs(xn/yn)*sign(xn):double(x2));
end
end
% 2-D indexes of line are saved in (xc, yc), and
% 1-D indexes are calculated here:
ind = sub2ind(size(img), yc, xc);
% draw line on the image (change value of '255' to one that you need)
img(ind) = 255;
end
If you have the Computer Vision System Toolbox, you can use insertShape.