I want to overlay a scatter plot onto a heatmap, where the points in the scatter plot are colored based on values at those points. If the values of the scatter points are in the same range as the heatmap’s values, this is pretty easy. However, if the scatter points have a different range/scale than the heatmap, then the plot with the smaller range of variables gets washed out.
For example, in the following code, the heatmap values in Grid_mat
fall in [0,1] and Y~N(0,5), which results in a significantly larger range than the Grid_mat
values. So, the heatmap becomes less informative, because the color limits are linked between the two plots. I even tried specifying different clim
s for the two plots, but that didn’t fix the problem.
using Plots, Distributions
using Plots.PlotMeasures
# Example data
x = 1:10
y = 1:10
Grid_mat = rand(1, 100)
ngridpoints = 10
n=50
locs = [rand(Uniform(1, 10), n) rand(Uniform(1, 10), n)]
Y = rand(Normal(0, 5), n)
# Create the heatmap with the first color scale
heatmap(x, y, reshape(Grid_mat[1, :], ngridpoints, ngridpoints),
color = :viridis, title = "", xlabel = "", ylabel = "",
clim = (minimum(Grid_mat[1, :]), maximum(Grid_mat[1, :])), alpha = 0.5, legend = false)
scatter!(locs[:, 1], locs[:, 2], marker_z = Y,
clim = (minimum(Y), maximum(Y)), color = :viridis, msw = 0.25, c = :viridis, legend = false, colorbar = true)
How can I plot the heatmap such that the color limits are for the heatmap values and overlay a scatter plot with color limits that are only for the scatter point values? Note that I do not want to rescale Grid_mat
nor Y
.