1
votes

I am trying to make a simple plot (for this example doing a plot of y=x^2 will suffice) where I want to set the colors of the points based on their magnitude given some colormap.

Following along my simple example say I had:

x = 1:10;
y = x.^2;

Use gscatter(x,y,jet(10)); legend hide; colorbar which produces a plot with the points colored but the colorbar does not agree with the colored values. (Can't post picture as this is my first post). Using a caxis([1,100]) command gives the right range but the colors are still off.

So I have two questions: (1) How can I fix the colors to fit to a colorbar given a range? In my real data, I am looking at values that range from -50 to 50 in some instances and have many more data points.

(2) I want to create a different plot with the same points (but on different axes) and I want the colors of each point on this new plot to have the same colors as their counterparts in the previous plot. How can I, programmatically, extract the color from each point so I can plot it on two different sets of axes?

I would just move the points into a matrix and do an imagesc() command but they aren't spaced as integers or equally so simple scaling wouldn't work either.

Thanks for any help!

1
I found that if I use something along the lines of: mesh(zeros(3,10),[x;x;x],[y;y;y]); view(90,0) that I get the proper plot and the colors agree with the bar. Why does this work since I am passing a 3x3 matrix as mesh won't take vector arguments. How do I get the colors of each point? - user3806105

1 Answers

0
votes

Regarding you first question, you need to interpolate the y values into a linear index to the colormap. Something like:

x = 1:10;
y = x.^4;
csize = 128;
cmap = jet(csize);
ind = interp1(linspace(min(y),max(y),csize),1:csize,y,'nearest');
scatter(x,y,14,cmap(ind,:),'filled')
colorbar
caxis([min(y) max(y)])

Using interp1 in this case is an overkill; you could calculate it directly. However, I think in this way it is clearer.

I think it also answers your 2nd question, since you have the index of the color of each data point, so you can use it again in the same way.