I got a 2d array ,that each row represents a signal (for wiener filtering)
Arr(10,45).
I want to plot,all signals (all column) in same figure, with X axis the K coefficient of wiener fieltering that is
K=(-11:0.5:11);
Which is of size=45.I also want it to be with logarithmic in both axis , x and y.
But when i plot with
loglog(Arr.');
set(gca,'xtick',(-11:0.5:11);
The result is not what i need.
What's going on?thanks in advance.
When plotting it, you should specify the k values as the first input to plot and Arr.' as the second. If you do not do this, MATLAB will simply use 1:size(Arr, 2) as the x coordinates of your plot.
hplot = plot(k, Arr.');
Related
In Matlab:
How do I modify plot(x,y,'o'), where x=1:10 and y=ones(1,10), such that each point in the plot will have a random shape?
And how can I give it colors chosen from a scheme where the value at x=1 is the darkest blue, and x=10 is red (namely some sort of heat map)?
Can this be done without using loops? Perhaps I should replace "plot" with a different function for this purpose (like "scatter"? I don't know...)? The reason is that I am plotting this inside another loop, which is already very long, so I am interested in keeping the running-time short.
Thanks!
First, the plain code:
x = 1:20;
nx = numel(x);
y = ones(1, nx);
% Color map
cm = [linspace(0, 1, nx).' zeros(nx, 1) linspace(1, 0, nx).'];
% Possible markers
m = 'o+*.xsd^vph<>';
nm = numel(m);
figure(1);
hold on;
for k = 1:nx
plot(x(k), y(k), ...
'MarkerSize', 12, ...
'Marker', m(ceil(nm * (rand()))), ...
'MarkerFaceColor', cm(k, :), ...
'MarkerEdgeColor', cm(k, :) ...
);
end
hold off;
And, the output:
Most of this can be found in the MATLAB help for the plot command, at the Specify Line Width, Marker Size, and Marker Color section. Colormaps are simply n x 3 matrices with RGB values ranging from 0 to 1. So, I interpreted the darkest blue as [0 0 1], whereas plain red is [1 0 0]. Now, you just need a linear "interpolation" between those two for n values. Shuffling the marker type is done by simple rand. (One could generate some rand vector with size n beforehand, of course.) I'm not totally sure, if one can put all of these in one single plot command, but I'm highly sceptical. Thus, using a loop was the easiest way right now.
I have an Nx3 array which stores the values in N coordinates. The first and second column correspond to x and y coordinate respectively, and the third column represents the value at that coordinates. I want to plot a 2D intensity plot, what's the best way to do it?
If the coordinates are evenly spaced, then I can use meshgrid and then use imshow, but in my data the coordinates are not evenly spaced. Besides, the array is very large N~100000, and the values (third column) span several orders of magnitude (so I should be using a logplot?). What's the best way to plot such a graph?
You can use griddata to interpolate your data at all 100000 points to a uniform grid (say 100 x 100) and then plot everything with a Log scaling of the colours,
x = data[:,0]
y = data[:,1]
z = data[:,2]
# define grid.
xi = np.linspace(np.min(x),np.max(x),100)
yi = np.linspace(np.min(y),np.max(y),100)
# grid the data.
zi = griddata(x,y,z,xi,yi,interp='linear')
#pcolormesh of interpolated uniform grid with log colormap
plt.pcolormesh(xi,yi,zi,norm=matplotlib.colors.LogNorm())
plt.colormap()
plt.show()
I've not tested this but basic idea should be correct. This has the advantage that you don't need to know your original (large) dataset and can work simply with the grid data xi, yi and zi.
The alternative is to colour a scatterplot,
plt.scatter(x, y, c=z,edgecolors='none', norm=matplotlib.colors.LogNorm())
and turn off the outer edges of the points so they make up a continuous picture.
I have 3 Nx1 arrays, say X,Y,Z. I'd like to create a 3D plot such that each of the arrays I can assign different colours to improve the visibility of points. My main objective now is to change only the colour of array Z, such that elements of array X and Y have different colours as that of elements of Z .
I tried scatter3 function and gscatter MATLAB functions, but not able to achieve what I desire.
If we see the Vertical axis in image (Z axis), the colour of points is varying from Blue to Orange/Yellow. I want to set the colour of all these Z points as Red. Rest, all the colours of X and Y points remain same
scatter(x, y, a, c) takes arguments x and y, and then a for size, and c for colour. a can either be a single scalar, or a vector with a size for each (x,y) point. c can be an RGB triplet, or a vector, the same size as x and y. For example:
x = 1:4;
scatter(x, x, 10*x, x);
results in
scatter3(x,y,z,s,c) is similar, so in your case, perhaps
scatter3(X,Y,Z,[],Z)
will result in your data having a different colour determined by its z value.
One little example which I think is what you're looking for:
X = rand(100,1);
Y = rand(100,1);
Z = rand(100,1);
scatter3(X,Y,Z,[],Z)
Produces:
I got the answer by trying different MATLAB functions.
I used the gscatter function. As I had to colour all the Z points(consider 'Z' array as the 3rd data set) as Red and rest X and Y data set (the other 2 data set) as green or other colour, I used the following code:
group = Z(:,1);
gscatter(X(:,1),Y(:,1),group, 'gr', 'xo');
It creates a 2D plot but serves my purpose.
The Image is made of 3 data sets, but the first 2 data sets are intentionally combined (in Green Cross) and 3rd data set is kept Red, to visualize the relation of 3rd data set with 1st and 2nd data sets combined.
Click here for Image.
So, I have two arrays:
X'
ans =
2.5770 2.5974 2.1031 2.7813 2.6083 2.9498 3.0053 3.3860
>> Y'
ans =
0.7132 0.5908 1.9988 1.0332 1.3301 1.1064 1.3522 1.3024
I would like to combine n-th members of two arrays together, and than plot those coordinates on graph.
So it should be:
{(2.5770,0.7132), (2.5974,0.5908)...}
Is this possible to do? If so, how?
Schorsch showed that it is simple to plot, but just to answer the question as asked in the title, you can combine the arrays into coordinates by just arranging the vectors like rectangles.
Your x and y are vertical, so you can put them side-by-side in a 2-column matrix:
combined = [x y]
or transform and have 2 rows: combined = [x' ; y']
(Because they're vertical, what you don't want is these, which would concatenate them out into one long column or row: [x ; y] or [x' y'])
Just to be clear, though, this is not needed for plotting.
Edit: A suggested edit asked what happens if you plot(combined). That depends if it's the horizontal or vertical version. In any case, plotting a 2x? matrix won't plot x vs. y. It plots all of the columns versus the simple indices 1,2,3,... So the first way I defined combined will make two lines, plotting x and y on the y-axis against their indices on the x-axis, and the second version of combined will make a strange plot with the all of the values of x plotted in a vertical column where x=1 and all of the points of y beside those at x=2.
I am trying to use imported data from a Scanning Transmission X-Ray Microscope raster scan in Matlab to create an intensity image in the form of an array. That is, the y-poisition is held constant and all x values are scanned and an intensity value is given for the relative (x,y) position. Then the next y value is taken, held constant and all x values are scanned, etc. The data is read as the column vectors into Matlab is as follows:
x = x-position ranging in values from 0-326 [104640x1 double]
y = y-position ranging in values from 0-319 [104640x1 double]
I = intensity at position (x,y) [104640x1 double]
I wish to create a a 326x319 array with entries corresponding to intensity vaules at the point (m0,n0)=(x0,y0)
I believe there is an easy way to do this using either matrix indexing or a for statement but I am relatively new to Matlab.
If I understand correctly:
result = reshape(I,327,320);
reshape() function on co-ordinates with respect to intensity should work.