why is the c of scatter function scaled automatically?

4 visualizaciones (últimos 30 días)
Mi
Mi el 28 de Dic. de 2021
Comentada: Image Analyst el 29 de Dic. de 2021
I was trying to test the scatter plot with the two y-axes, but i found that using left axis and right axis gives different results.
For example, if i use yyaxis left, the colorbar of c is correct.
figure()
yyaxis left
x = linspace(0,3*pi,200);
y = cos(x) + rand(1,200);
c = linspace(1,10,length(x));
scatter(x,y,[],c)
colorbar
However, if I use yyaxis right, the colorbar will be automatically scale from 0 to 1.
figure()
yyaxis right
x = linspace(0,3*pi,200);
y = cos(x) + rand(1,200);
c = linspace(1,10,length(x));
scatter(x,y,[],c)
colorbar
Does anyone know how to deal with this? I don't want the c to scale from 0 to 1 when I use yyaxis right.
Hope i express the questions clearly!
  1 comentario
VBBV
VBBV el 28 de Dic. de 2021
Editada: VBBV el 28 de Dic. de 2021
It happens when you use yyaxis right call on a new figure handle directly without triggering the left axis. The colorbar by default uses left axis values and displays it.

Iniciar sesión para comentar.

Respuestas (2)

VBBV
VBBV el 28 de Dic. de 2021
figure()
yyaxis left
x = linspace(0,3*pi,200);
y = cos(x) + rand(1,200);
c = linspace(1,10,length(x));
scatter(x,y,[],c)
colorbar
yyaxis right
x = linspace(0,3*pi,200);
y = cos(x) + rand(1,200);
c = linspace(1,10,length(x));
scatter(x,y,[],c)
colorbar
  2 comentarios
Mi
Mi el 28 de Dic. de 2021
but i only want the right axis has the scatter plot, the left axis I plan to use just simple plot, is there a way to change it such that it uses right axis values?
Image Analyst
Image Analyst el 28 de Dic. de 2021
@Mi Lei Mi Lei, did you see my explanation below? The colors are independent. Also I think there is no reason to make the colors change along the x axis.
So you want a scatter plot on the right axis, and a simple curve on the left axis? Like what?
And exactly what do you want to have colors and what do those colors depend on?

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 28 de Dic. de 2021
I think you're getting confused about colorbar and colormap and marker colors.
The colorbar shows the colormap, and the colorbar/map depends on the y values.
The 4th input into scatter is the marker colors. You can set whatever colors you want. It's totally independent and has nothing at all to do with the colormap, colorbar, and y values.
figure()
x = linspace(0,3*pi,200);
y = cos(x) + rand(1,200);
% Set up the marker colors.
% Find out what row from the colormap we should get our color from.
cmap1 = parula(length(x));
colorMapRows = round(rescale(x, 1, size(cmap1, 1)));
markerColors = cmap1(colorMapRows, :);
% Set up the colormap for the graph. It depends only on the y values.
cmap2 = jet(length(x)); % Depends on y values only.
% Plot on the left axis.
yyaxis left
scatter(x, y, [], markerColors)
colormap(cmap2);
colorbar
title('Jet colormap, Parula marker colors')
figure()
% Plot on the right axis.
yyaxis right
x = linspace(0,3*pi,200);
y = cos(x) + rand(1,200);
scatter(x, y, [], markerColors)
colormap(cmap2);
colorbar
title('Jet colormap, Parula marker colors')
Note that the colors used for the colorbar depends on the y value while the marker colors are chosen from a different colormap (at least that's what I did in the demo, though they can be the same if you want, though it would be confusing and deceptive).
One important thing to note is that if you use yyaxis, and just use one of the axes (either the right one or the left one), the other axis that you didn't use will be scaled 0-1 until you actually plot something to that axis. So that's why when you plot on the left, the left axis goes from 0-2 (which is the range of your y data) but the right axis defaults to 0-1 because you didn't plot anything to it ever. Then you do figure(), which starts a new graph so the old right axis on the first graph stays at 0-1 (because you never plotted to it). And you plot on the right side of the new graph so that's why the right side goes from 0-2, but since you didn't plot anything on the left axis for that figure, it defaults to 0-1.
  2 comentarios
Mi
Mi el 29 de Dic. de 2021
why do you say "colorbar/map depends on the y values"? I thought colorbar/map is a note to show the value of c. I actually had three dimensional data, like x=1:10, y=1:10, c= 10:19. Since x has x axis to notate, y has y axis to notate, so I want to use color bar to indicate the value of c. That is why I don't want it to rescale by itself.
For yyaxis left, I just want to plot something normal, without c axis (like using plot)
While for yyaxis right, I want to do what i described above.
Image Analyst
Image Analyst el 29 de Dic. de 2021
I say "colorbar/map depends on the y values" because it does.
"I thought colorbar/map is a note to show the value of c." Not normally. Normally everyone has the marker colors related to something having to do with the value of something, though not usually the x axis like you set it up. It's only because your x values are monotonically increasing that your colors proceed in that way. If you had scrambled the order of the points, you'd still get the same scatterplot but the colors would be way different if you did what you (using c for a colormap) than using the x values for the colormap.
Let's say you didn't use marker colors and had all the markers just be the same color. Is that bad? Would some information be missing? Exactly what does colorizing the markers by their x value do? Just make it look pretty?
"I want to do what i described above." Fine, you can do it, though most everyone else would not do it that way and would be confused by what you did.
Not sure why you're using yyaxis when you call figure in between. The whole point of yyaxis is to put two plots on the same graph. You're not doing that. You call figure in between so you're putting them on two separate graphs and using yyaxis when you're plotting only one curve just adds an unnecessary and confusing extra axis. I suggest you don't call figure in between.

Iniciar sesión para comentar.

Categorías

Más información sobre Orange en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by