Multiple Colormaps with (or without) Transparency

27 views (last 30 days)
Hello everyone,
I have 2d histogram data which I can plot as in the example below:
But I have several more histograms that I would like to plot on top of this with different colormaps. The idea for each different histogram is to have a different colormap with shades of a different color to distinguish different datasets, but zero should tend to the white for all data. I found a way to define a custom colormap but as I want all histograms within the same plot and each axes only accepts a single colormap, I thought of introducing transparent colors in the new axes objects for the additional histograms I want to plot. How can I do this? I found something about transparent hex codes but I couldn't find how (and if at all) I can have a gradient with transparent colors. I am inserting the dataset that I used to generate the above plot if you would like to play with. The commands for this plot are:
load("2d_hist_data.mat")
imagesc(edges(1),edges(2), counts);
colormap(flipud(bone));

Accepted Answer

Garmit Pant
Garmit Pant on 8 Aug 2024
Hello Hakan Caldag,
From what I gather, you have multiple 2D histogram datasets that you want to plot together by overlaying them on top of one another, with each dataset represented using a different color map.
You can plot the histograms overlaid by plotting them on separate axes and then aligning the axes together. The transparency can be adjusted by changing the ‘AlphaData’ of the plots using the “alpha” function. A lower ‘AlphaData’ value will result in a more transparent plot.
The following code snippet plots two histograms in a single plot, with each histogram represented by a custom color map and one of the histograms being transparent:
% Load data
data = load('2d_hist_data.mat');
edges = data.edges;
counts = data.counts;
vec = [100; 83; 68; 30; 15; 0];
N = 128;
% Define color maps
hex_colors_blue = [
'#ffffff'; % White
'#e6f0ff'; % Light blue
'#cce0ff'; % Lighter blue
'#99c2ff'; % Light blue
'#3399ff'; % Medium blue
'#0066cc' % Dark blue
];
raw_blue = sscanf(hex_colors_blue', '#%2x%2x%2x', [3, size(hex_colors_blue, 1)]).' / 255;
map_blue = interp1(vec, raw_blue, linspace(100, 0, N), 'pchip');
hex_colors_red = [
'#ffffff'; % White
'#ffe6e6'; % Light red
'#ffcccc'; % Lighter red
'#ff9999'; % Light red
'#ff3333'; % Medium red
'#cc0000' % Dark red
];
raw_red = sscanf(hex_colors_red', '#%2x%2x%2x', [3, size(hex_colors_red, 1)]).' / 255;
map_red = interp1(vec, raw_red, linspace(100, 0, N), 'pchip');
% Plot the two histograms
figure;
% Create two axes
ax1 = axes;
s1 = imagesc(edges(1), edges(2), counts);
view(2);
ax2 = axes;
s2 = imagesc(edges(1), edges(2), counts');
% Link them together
linkaxes([ax1, ax2]);
% Hide the top axes
ax2.Visible = 'off';
ax2.XTick = [];
ax2.YTick = [];
% Give each one its own colormap
colormap(ax1, map_blue);
colormap(ax2, map_red);
alpha(s2, 0.4);
% Add colorbars and get everything lined up
set([ax1, ax2], 'Position', [.17 .11 .685 .815]);
cb1 = colorbar(ax1, 'Position', [.05 .11 .0675 .815]);
cb2 = colorbar(ax2, 'Position', [.88 .11 .0675 .815]);
For further understanding, kindly refer to the following MathWorks Documentation:
  • Refer to the ‘Description’ section to understand the relevant input methods and their effects: MathWorks Documentation.
I hope you find the above explanation and suggestions useful!
  1 Comment
Hakan Caldag
Hakan Caldag on 8 Aug 2024
Edited: Hakan Caldag on 8 Aug 2024
Dear Garmit,
That was exactly what I was trying to do, thank you so much!
Update: So if I do it this way, I understand that all of the second histogram is affected from the transparency. I was wondering whether the color gradient in the second histogram can be from solid red-to-transparent rather than transparent red to transparent white.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!