46 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Sim am 22 Jun. 2024 um 20:31
Kommentiert: Star Strider vor etwa 20 Stunden
Akzeptierte Antwort: Star Strider
In MATLAB Online öffnen
How can I "connect" the colorbar to my values (between 0 and 1) of a scatter plot?
a = rand(1000,1);
C = 1-[a a a];
scatter(1:length(a),a,60,C,'fill')
colormap(C);
colorbar;
Here you can see that the colorbar is not gradually changing from 0 to 1.. How to fix it?
0 Kommentare -2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden
-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden
Melden Sie sich an, um zu kommentieren.
Melden Sie sich an, um diese Frage zu beantworten.
Akzeptierte Antwort
Star Strider am 22 Jun. 2024 um 20:58
In MATLAB Online öffnen
I am not certain what you want to do, or what you intend by ‘connect’.
One option —
a = rand(1000,1);
% C = 1-[a a a];
C = ([1;1;1]*linspace(1, 0, 1000)).';
scatter(1:length(a),a,60,C,'fill')
colormap(C);
colorbar;
.
6 Kommentare 4 ältere Kommentare anzeigen4 ältere Kommentare ausblenden
4 ältere Kommentare anzeigen4 ältere Kommentare ausblenden
Sim am 22 Jun. 2024 um 21:04
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2131106-how-can-i-connect-the-colorbar-to-my-values-of-a-scatter-plot#comment_3193501
Bearbeitet: Sim am 22 Jun. 2024 um 21:16
In MATLAB Online öffnen
Thanks a lot for youer answer @Star Strider!
With "connect", I mean that, given a certain colormap with a certain number of colors (which change gradually from each other), the values of my scatter plot (i.e. "a = rand(1000,1)", which are between 0 and 1), should get the proper colors from that defined colormap...
About your option, more or less yes, something like that... however, in your colorbar the 1 corresponds to black and 0 corresponds to white, but the scatterplot shows that 1000 corresponds to black... I mean, the variation of color from white to black should go from zero to one, in the y-axis direction, and not in the x-axis direction, right?.....
I was thinking about a solution similar to yours... but I am not sure if it is correct......
a = rand(1000,1);
C = 1-[a a a];
scatter(1:length(a),a,60,C,'fill')
l = 1-linspace(0,1,1000)';
colormap([l l l])
colorbar
Star Strider am 22 Jun. 2024 um 21:36
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2131106-how-can-i-connect-the-colorbar-to-my-values-of-a-scatter-plot#comment_3193521
In MATLAB Online öffnen
As always, my pleasure!
It would appear to be, if that is the result you want.
You can avoid the interruptions in the axes near the lower end of your plot by by bringing them to the top layer —
a = rand(1000,1);
C = ([1;1;1]*linspace(1, 0, 1000)).';
scatter(a,1:length(a),60,C,'fill')
colormap(C);
colorbar;
Ax = gca;
Ax.Layer = 'top';
That is the only other change I would make to your code if it now does what you want it to do.
.
Sim vor etwa 24 Stunden
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2131106-how-can-i-connect-the-colorbar-to-my-values-of-a-scatter-plot#comment_3194191
Bearbeitet: Sim vor etwa 23 Stunden
In MATLAB Online öffnen
Hi @Star Strider, I am very sorry, I think I did a mistake to explain my needs, my fault.... :-(
In your figure, the change of color, from white (corresponding to 0) to black (corresponding to 1), goes from value 0 to value 1000. Instead, what I would need is that the change of color, from white (corresponding to 0) to black (corresponding to 1), goes from value 0 to value 1, since my values in
a = rand(1000,1);
range from 0 to 1, and not from 0 to 1000.
In other words, what I would need is something like this (which I think it is not very correct since I use directly the values of "a" as inputs for the color of the scatter plot! Obviouslly, your opinion and the opinion of other users of this Matlab community is very welcome!):
a = rand(1000,1);
C = 1-[a a a];
scatter(1:length(a),a,60,C,'fill')
l = 1-linspace(0,1,1000)';
colormap([l l l])
colorbar
Here, we assign a color of the colorbar - which goes from white, at 0, to black, at 1 -, to each value of
a = rand(1000,1);
which values range from 0 to 1.
For example, if we consider the minimum value of "a":
a(285)
ans =
0.0013207800733116
the corresponding point of the scatter plot should have, basically, a white color, since white corresponds to the value 0 in the colorbar.
As another example,
a(3)
ans =
0.88899234515285
and the corresponding point of the scatter plot, should have a very dark grey, almost black, since black corresponds to the value 1 in the colorbar.
Star Strider vor etwa 21 Stunden
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2131106-how-can-i-connect-the-colorbar-to-my-values-of-a-scatter-plot#comment_3194361
In MATLAB Online öffnen
I cannot understand what you want to change.
The colour assignmentts appear to be correct.
I added a few lines to plot specific points with red and green circles, along with ttheir values.
ay = rand(1000,1);
ax = 1:length(ay);
C = 1-[ay ay ay];
scatter(ax,ay,60,C,'fill')
l = linspace(1,0,1000)';
colormap([l l l])
colorbar
format shortG
Point_1 = [ax(1) ay(1)]
Point_1 = 1x2
1 0.32606
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Point_1000 = [ax(1000) ay(1000)]
Point_1000 = 1x2
1.0e+00 * 1000 0.94193
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
hold on
plot(Point_1(1), Point_1(2), 'or', 'MarkerSize',11)
plot(Point_1000(1), Point_1000(2), 'og', 'MarkerSize',11)
hold off
I will do my best to help you. I need to understand what you want to do.
.
Sim vor etwa 20 Stunden
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2131106-how-can-i-connect-the-colorbar-to-my-values-of-a-scatter-plot#comment_3194381
Bearbeitet: Sim vor etwa 20 Stunden
In MATLAB Online öffnen
That's great, thanks a lot! Problem solved!
I was not sure about this code:
a = rand(1000,1);
C = 1-[a a a];
scatter(1:length(a),a,60,C,'fill')
l = linspace(1,0,1000)';
colormap([l l l])
colorbar
but your further check - with the two red and green points -, confirmed what I was thinking/expecting :-)
Thanks a lot for your great support!
And, I am really sorry for my confusion... My fault :-)
Star Strider vor etwa 20 Stunden
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2131106-how-can-i-connect-the-colorbar-to-my-values-of-a-scatter-plot#comment_3194421
As always, my pleasure!
No worries! Often, these sorts of mathematical concepts are difficult to describe.
Melden Sie sich an, um zu kommentieren.
Weitere Antworten (0)
Melden Sie sich an, um diese Frage zu beantworten.
Siehe auch
Kategorien
MATLABGraphicsFormatting and AnnotationColormapsBlue
Mehr zu Blue finden Sie in Help Center und File Exchange
Tags
- colorbar
- colormap
- scatter
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
Es ist ein Fehler aufgetreten
Da Änderungen an der Seite vorgenommen wurden, kann diese Aktion nicht abgeschlossen werden. Laden Sie die Seite neu, um sie im aktualisierten Zustand anzuzeigen.
Website auswählen
Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .
Sie können auch eine Website aus der folgenden Liste auswählen:
Amerika
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asien-Pazifik
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Kontakt zu Ihrer lokalen Niederlassung