# How to show the color legend of gscatter plot in a single bar or range?

11 visualizaciones (últimos 30 días)
Niraj Bal Tamang el 26 de Mayo de 2021
Editada: Yash el 5 de Mzo. de 2024
I have a gscatter plot with slope and area as the axes and time as the colored variable. Is there a simple way in Matlab to show these individual colors in a single color gradient to save space in the figure? I added the Colorbar from the figure window but it doesn't match with those individual legend colors. The code i used for this figure is as follows:
h=gscatter(usarea,slope,Final_max);
set(gca,'xscale','log','yscale','log');
xlabel('Upstream Drainage Area (sq km)');
ylabel('Slope (m/m)');
hold on
xline(7*10^5,'Color','r','LineStyle','--');%vertical line
hold on
plot([1.1*10^6 10^10],[0.15 0.15],'--r');%horizontal line
hold on
plot([7*10^5 5*10^9],[0.2 10^-4],'--r');%diagonal line
hold off
grid on
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Respuestas (1)

Yash el 5 de Mzo. de 2024
Editada: Yash el 5 de Mzo. de 2024
Hi Niraj,
To create a gradient color map based on a continuous variable like time in your scatter plot, you can manually map the colors of your points to a colormap instead of using "gscatter", which is more suited for categorical variables and doesn't automatically provide a gradient colormap. Here's how you can modify your code to achieve a gradient color scheme based on the "Final_max" variable:
1. Map your time variable to a colormap: First, you need to map the values of your time variable (Final_max) to a colormap. MATLAB provides various colormaps (e.g., 'jet', 'hsv', 'hot', etc.), and you can choose one that suits your data visualization needs. Here is a list of available colormaps for MATLAB: https://www.mathworks.com/help/matlab/ref/colormap.html#buc3wsn-6:~:text=%7C%20%27default%27-,Colormap,-for%20the%20new
2. Create a scatter plot and manually set the colors: Instead of using "gscatter", use the "scatter" function and manually set the colors of each point based on the mapped colormap values.
Here is how you can do it:
% Drop the indices where Final_max is NaN
Final_max = Final_max(~isnan(Final_max));
slope = slope(~isnan(Final_max));
usarea = usarea(~isnan(Final_max));
% Assuming usarea, slope, and Final_max are your variables
% Normalize your Final_max variable to span 0 to 1
Final_max_normalized = (Final_max - min(Final_max)) / (max(Final_max) - min(Final_max));
% Choose a colormap
colormapName = 'parula'; % You can change this to any colormap you like
colors = colormap(colormapName);
% Map your normalized time to the colormap
numColors = size(colors, 1);
colorIndices = ceil(Final_max_normalized * (numColors - 1)) + 1;
pointColors = colors(colorIndices, :);
% Create the scatter plot
scatter(usarea, slope, [], pointColors, 'filled');
set(gca, 'xscale', 'log', 'yscale', 'log');
xlabel('Upstream Drainage Area (sq km)');
ylabel('Slope (m/m)');
hold on;
xline(7*10^5, 'Color', 'r', 'LineStyle', '--'); % Vertical line
plot([1.1*10^6 10^10], [0.15 0.15], '--r'); % Horizontal line
plot([7*10^5 5*10^9], [0.2 10^-4], '--r'); % Diagonal line
hold off;
grid on;
colorbar;
caxis([min(Final_max) max(Final_max)]); % Set the limits of the colorbar to match your variable
title('Color represents Final max');
Given below is the obtained plot:
Hope this helps!
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

Más información sobre Data Distribution Plots 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