Julia矩阵热力图可视化求助:区域曲线显示异常
热力图区域划分问题
我想用热力图可视化矩阵,将其按(x,y)值约束划分为多个子区域,但两次尝试均出现问题:
- 第一次生成的热力图不符合预期,但矩阵数据完整
- 第二次矩阵3/4区域无数据,无法正常绘图
目标是实现与参考折线图一致的区域划分效果,考虑用自定义函数替代Julia自动计算的条件,以下是两次尝试的代码:
第一次尝试(生成错误热力图)
using Plots function W(x, y, g, d, ϕ, c, α, s) if x <= c/ϕ && y <= c/ϕ p_1 = x + g * x * (1 - x) - d * (x - y) - α*x p_2 = y + g * y * (1 - y) - d * (y - x) - α*y return 1 - p_1 + 1 - p_2 elseif x >= c/ϕ && y <= c/ϕ p_1 = x + g * x * (1 - x) - d * (x - y) - ϕ*x - α*x p_2 = y + g * y * (1 - y) - d * (y - x) - α*y return 1 - p_1 - c + 1 - p_2 elseif x <= c/ϕ && y >= c/ϕ p_1 = x + g * x * (1 - x) - d * (x - y) - α*x p_2 = y + g * y * (1 - y) - d * (y - x) - ϕ*y - α*y return 1 - p_1 + 1 - p_2 - c elseif x >= c/ϕ && y >= c/ϕ && x<= 0.79 && y<= 0.79 ### 我替换了不等式的满足值 p_1 = x + g * x * (1 - x) - d * (x - y) - ϕ*x - α*x p_2 = y + g * y * (1 - y) - d * (y - x) - ϕ*y - α*y return 1 - p_1 - c + 1 - p_2 - c elseif x >= 0.79 && y >= c/ϕ p_1 = x + g * x * (1 - x) - d * (x - y) - ϕ*x - α*x p_2 = y + g * y * (1 - y) - d * (y - x) - ϕ*y - α*y return s + 1 - p_2 - c elseif x >= c/ϕ && y >= 0.79 p_1 = x + g * x * (1 - x) - d * (x - y) - ϕ*x - α*x p_2 = y + g * y * (1 - y) - d * (y - x) - ϕ*y - α*y return 1 - p_1 - c + s elseif x >= 0.79 && y >=0.79 p_1 = x + g * x * (1 - x) - d * (x - y) - ϕ*x - α*x p_2 = y + g * y * (1 - y) - d * (y - x) - ϕ*y - α*y return ((1 - p_1 - c + s) + (s + 1 - p_2 - c))/2 end end g = 0.5 d = 0.25 c = 0.2 ϕ = 0.4 α = 0.25 s = 0.3 x_vals = range(0, stop = 1, length = 100) y_vals = range(0, stop = 1, length = 100) z = [W(x, y, g, d, ϕ, c, α, s) for x in x_vals, y in y_vals] pyplot() fig = heatmap(x_vals, y_vals, z, xlabel = "P1", ylabel = "P2", zlabel = "", title = "Total Payoff", color = :viridis, aspect_ratio=:equal)
第二次尝试(矩阵多数区域无数据)
using Plots function W(x, y, g, d, ϕ, c, α, s) condition_3 = (1/d)*(1 - s - c) - (1/d)*(1 + g - d - ϕ)*x - (g/d)x^2 condition_4 = (1/d)*(1 - s - c) - (1/d)(1 + g - d - ϕ)*y - (g/d)y^2 condition_31 = (1/d)*(1 - s - c) - (1/d)(1 + g - d - ϕ - (α/2))*x - (g/d)x^2 condition_41 = (1/d)*(1 - s - c) - (1/d)(1 + g - d - ϕ - (α/2))*y - (g/d)*y^2 if x <= c/ϕ && y <= c/ϕ p_1 = x + g * x * (1 - x) - d * (x - y) p_2 = y + g * y * (1 - y) - d * (y - x) return 1 - p_1 + 1 - p_2 elseif x >= c/ϕ && y >= c/ϕ && y <= condition_3 && x <= condition_4 #y <= f(x) && x <= f(y) p_1 = x + g * x * (1 - x) - d * (x - y) - ϕ*x p_2 = y + g * y * (1 - y) - d * (y - x) - ϕ*y return 1 - p_1 - c + 1 - p_2 - c elseif x <= c/ϕ && y >= c/ϕ && x <= condition_4 p_1 = x + g * x * (1 - x) - d * (x - y) p_2 = y + g * y * (1 - y) - d * (y - x) - ϕ*y return 1 - p_1 + 1 - p_2 - c elseif x >= c/ϕ && y <= c/ϕ && y <= condition_3 p_1 = x + g * x * (1 - x) - d * (x - y) - ϕ*x p_2 = y + g * y * (1 - y) - d * (y - x) return 1 - p_1 - c + 1 - p_2 elseif x >= c/ϕ && y >= c/ϕ && y >= condition_3 && x <= condition_41 p_1 = x + g * x * (1 - x) - d * (x - y) p_2 = y + g * y * (1 - y) - d * (y - x) - ϕ*y return s + (1 - P_2 - c + ϕ*y + (α/2)*y) elseif x >= c/ϕ && y >= c/ϕ && y <= condition_31 && x >= condition_4 p_1 = x + g * x * (1 - x) - d * (x - y) p_2 = y + g * y * (1 - y) - d * (y - x) - ϕ*y return (1 - P_1 - c + ϕ*x + (α/2)*x) + s end end g = 0.5 d = 0.25 c = 0.2 ϕ = 0.4 α = 0.25 s = 0.3 x_vals = range(0, stop = 1, length = 100) y_vals = range(0, stop = 1, length = 100) z = [W(x, y, g, d, ϕ, c, α, s) for x in x_vals, y in y_vals] fig = heatmap(x_vals, y_vals, z, xlabel = "Pi", ylabel = "Pj", zlabel = "", title = "Total Payoff", color = :viridis)
内容的提问来源于stack exchange,提问作者Linh
相关产品推荐
相关产品推荐

