Colorbar With Wrong Graphs
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
Rahul
el 24 de Jun. de 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:
- Colorbar showing color scale using 'colorbar': https://www.mathworks.com/help/matlab/ref/colorbar.html
- View and set current colormap using 'colormap': https://www.mathworks.com/help/matlab/ref/colormap.html
Hope this helps!
0 comentarios
Ver también
Categorías
Más información sobre Color and Styling 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!