我正在尝试在 Julia 中绘制曲面图。我想要的是表面的颜色与垂直轴值相匹配,并且范围从深红色(负)到深蓝色(正),垂直时为白色(pi - c)= 0.
我可以毫无问题地绘制图形,甚至可以在 Plot 函数中调整 clim。然而,图表上的实际颜色与图例中的颜色条不匹配:clim 只影响图例但不会改变表面的颜色。
这是一张固定想法的图片。请注意,垂直轴上的所有值都是负数,但即使垂直值 = -0.9,表面也是白色的,而右侧的图例正在执行我想要的操作。
这是代码:
using Plots
# Paramters
A = 1
αω = 0.5
αz = 0.5
β1 = 1
β2 = 1
β0 = 0
γ = 1
# Functions
π(ω,z) = A*ω^(αω)*z^(-αz)
c(x) = β1*x + β2*x^2 + β0
p(x) = 1 - exp(-γ*x);
F(x,Ω=Ω, Z=Z) = [π(ω_val, z_val) for ω_val in Ω, z_val in Z] .- [c(x) for ω_val in Ω, z_val in Z]
# Grids
Ω = range(0, 1, length=101)
Z = range(0.4, 1, length=101)
function plot_f(x, Ω=Ω, Z=Z)
# Compute f(ω,z) = π(ω,z) - c(x) for each pair of ω and z values
f_values = F(x,Ω,Z)
# Compute the maximum absolute value of f_values
max_abs = float(maximum(abs.(f_values)))
# Create a 3D surface plot of f(ω,z) for the given value of x, with the colormap fixed at 0 for the white portion
plot(Z,Ω,f_values,xlabel="z", ylabel="ω", zlabel="π(ω,z) - c($x)",
st=:surface,camera=(30,30),
cmap=:RdBu,clim=(-max_abs, max_abs),
title="Current-period profit when x=$x")
end
plot_f(0.9)
我确定我在做一些愚蠢的事情。如何使表面与图例匹配?
找不到完整的答案,但设法用另一个问题替换了一个问题。尝试:
function plot_f(x, Ω=Ω, Z=Z)
# Compute f(ω,z) = π(ω,z) - c(x) for each pair of ω and z values
f_values = F(x,Ω,Z)
# Compute the maximum absolute value of f_values
f_min, f_max = extrema(f_values)
f_max_abs = max(f_max, -f_min)
f_cmap = cgrad(ColorSchemes.RdBu[[f_min, f_max]./(2*f_max_abs) .+ 0.5])
# Create a 3D surface plot of f(ω,z) for the given value of x, with the colormap fixed at 0 for the white portion
plot(Z,Ω,f_values,xlabel="z", ylabel="ω", zlabel="π(ω,z) - c($x)",
st=:surface,camera=(30,30),
cmap=f_cmap,
title="Current-period profit when x=$x")
end
表面颜色看起来更好,但颜色图不对。