Colorbar With Wrong Graphs

8 visualizzazioni (ultimi 30 giorni)
Peter Sakkos
Peter Sakkos il 1 Mag 2021
Modificato: Voss il 30 Giu 2025
I have a function that, when executed, creates two plots, both of which have different colormaps and colorplots. When I run my code, the first figure gets printed first, correctly, with the right color scheme and color bar shown. Then, when the 2nd figure shows, the first figure shows what is supposed to be the second color bar. The second figure also shows some random color bar. How can I pin each color to a specific figure so that this switching/ flip-flopping doesn't happen?
  2 Commenti
DGM
DGM il 1 Mag 2021
Modificato: DGM il 1 Mag 2021
Consider including a sample of your code so that others can troubleshoot it.
Peter Sakkos
Peter Sakkos il 1 Mag 2021
Modificato: Peter Sakkos il 1 Mag 2021
My actual code is fairly long due to all of the plotting features I have.
It basically goes as follows. When it runs, colorbar "s" goes to figure 1 and the default colorbar goes to figure 2:
figure(1)
plot(...)
cm_P = colormap(summer(length(psi)));
p = colorbar;
...
figure(2)
plot(...)
cm_T = colormap(winter(length(psi)));
s = colorbar;

Accedi per commentare.

Risposte (2)

Rahul
Rahul il 24 Giu 2025
Hi Peter,
I believe that you are working with two separate figures in MATLAB, each using different colormaps and colorbars, where you are observing an issue with the colorbars, where they appear to "flip-flop" between figures i.e., the second figure seems to overwrite the colorbar of the first, and the colorbars don’t remain consistent with their respective colormaps.
This could happen because the 'colormap' and 'colorbar' functions in MATLAB, by default, apply to the current figure (the one most recently created or clicked on). When switching between figures using 'figure(n)', it would be a better practice to explicitly associate each colormap and colorbar with its corresponding figure or axes to avoid these conflicts.
To resolve this, you can capture each figure or axes handle and assign the colormap and colorbar explicitly. Here is how you can re-structure your code in MATLAB:
% First figure
f1 = figure(1);
ax1 = axes('Parent', f1); % or use gca if axes already exist
plot(ax1, colormap(ax1, summer(length(psi)));
colorbar(ax1); % explicitly attach colorbar to the correct axes
% Second figure
f2 = figure(2);
ax2 = axes('Parent', f2);
plot(ax2, colormap(ax2, winter(length(psi)));
colorbar(ax2);
This approach can ensure that each figure maintains its own colormap and colorbar independently, avoiding any unnecessary overwrites caused by implicit figure switching.
To know more about the usage of various functions mentioned in the above code snippet, you can refer to the following documentation links:
Hope this helps!

Voss
Voss il 30 Giu 2025
Modificato: Voss il 30 Giu 2025

Try specifying the target figure in each colormap() call:

figure(1)
plot(...)
cm_P = colormap(1,summer(length(psi)));
p = colorbar;
...
figure(2)
plot(...)
cm_T = colormap(2,winter(length(psi)));
s = colorbar;

Categorie

Scopri di più su Color and Styling in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by