julia语言绘制1<|z|<2区域

1.源码实现

using Plots
using LaTeXStrings

default(size = (600, 600), legend = false)
L = 4.0

p = plot(xlims = (-L, L), ylims = (-L, L),
    aspect_ratio = 1, framestyle = :none,
    grid = false, xticks = false, yticks = false,
    tickfontsize = 0, guidefontsize = 0)

plot!(p, [-L, L], [0, 0], color = :black, lw = 2, arrow = :closed, label = "")
plot!(p, [0, 0], [-L, L], color = :black, lw = 2, arrow = :closed, label = "")

# 斜体数学符号 + 衬线体,字号加大
xlab = text(L"x",   :black, 16, "Computer Modern")
ylab = text(L"y",   :black, 16, "Computer Modern")
Olab = text(L"O",   :black, 16, "Computer Modern")

annotate!(p,  L - 0.15, -0.25, xlab)   # x 放在右端轴下方
annotate!(p, -0.25, L - 0.15, ylab)   # y 放在上端轴左侧
annotate!(p, -0.25, -0.25, Olab)      # O 放在原点左下

# ===== 阴影:圆环 1 < |z| < 2 =====
Rout, Rin = 2.0, 1.0            # 外、内半径
spacing = 0.2
EPS = 1e-9
m = 1.0; A = 1 + m^2

# 直线 y=mx+b 与圆 x²+y²=r² 的交点 x 区间;无交返回 nothing
function circle_xs(r, m, b, A)
    C = b*b - r*r
    disc = (2m*b)^2 - 4A*C       # 等价于 4A r² - 4b²
    disc <= 0 && return nothing
    rq = sqrt(disc) / (2A)
    xc = -m*b / A
    return (xc - rq, xc + rq)
end

bmax = Rout * sqrt(1 + m^2)      # 只需覆盖外圆
nsteps = Int(ceil(bmax / spacing))
xs_sh = Float64[]; ys_sh = Float64[]
for k in -nsteps:nsteps
    b = k * spacing

    out = circle_xs(Rout, m, b, A)     # 外圆内区间
    out === nothing && continue
    lo, hi = out

    inner = circle_xs(Rin, m, b, A)    # 内圆(要挖掉)
    if inner === nothing
        append!(xs_sh, [lo, hi, NaN]); append!(ys_sh, [m*lo+b, m*hi+b, NaN])
    else
        i1, i2 = inner
        for (xl, xr) in ((lo, min(hi, i1)), (max(lo, i2), hi))
            xl < xr - EPS || continue
            append!(xs_sh, [xl, xr, NaN]); append!(ys_sh, [m*xl+b, m*xr+b, NaN])
        end
    end
end
plot!(p, xs_sh, ys_sh, color = :black, lw = 1.0, label = "")

# 内外圆边界
θ = range(0, 2π, length = 300)
plot!(p, Rout .* cos.(θ), Rout .* sin.(θ), linecolor = :black, lw = 1.5, label = "")
plot!(p, Rin  .* cos.(θ), Rin  .* sin.(θ), linecolor = :black, lw = 1.5, label = "")

# ---- 刻度线:-3 到 3(跳过 0,原点不画刻度)----
ts, h = -3:3, 0.12        # h 为刻度线半长
for t in ts
    t == 0 && continue
    plot!(p, [t, t], [0, h], color = :black, lw = 1.2, label = "")   # x 轴刻度
    plot!(p, [0, h], [t, t], color = :black, lw = 1.2, label = "")   # y 轴刻度
end

# ---- 数字标签 ----
for t in ts
    t == 0 && continue
    annotate!(p, t, -0.25, text(string(t), :black, 15, "Computer Modern"))      # x 轴数字在轴下方
    annotate!(p, -0.25, t, text(string(t), :black, 15, "Computer Modern"))      # y 轴数字在轴左侧
end

savefig(p, "annulus_1_2.png.png")

2.结果展示

annulus_1_2.png.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容