Skip to content

Commit

Permalink
Refactor PyPlot (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
henry2004y authored Apr 25, 2024
1 parent 58cb774 commit 329f443
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Batsrus"
uuid = "e74ebddf-6ac1-4047-a0e5-c32c99e57753"
authors = ["Hongyang Zhou <[email protected]>"]
version = "0.5.5"
version = "0.5.6"

[deps]
FortranFiles = "c58ffaec-ab22-586d-bfc5-781a99fd0b10"
Expand Down
57 changes: 19 additions & 38 deletions src/plot/pyplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Plot the variable from SWMF output.
`plotdata(data, func, plotmode="trimesh", plotrange=[-1.0, 1.0, -1.0, 1.0], plotinterval=0.2)`
# Arguments
- `bd::BATLData`: BATSRUS data to be visualized.
- `bd::BATLData`: BATSRUS data.
- `func::String`: variables for plotting.
# Keywords
Expand Down Expand Up @@ -328,7 +328,6 @@ end
"""
function cutplot(bd::BATLData, var::AbstractString, ax=nothing;
plotrange=[-Inf,Inf,-Inf,Inf], dir="x", sequence=1, levels::Int=20)

x, w = bd.x, bd.w
varIndex_ = findindex(bd, var)

Expand Down Expand Up @@ -381,7 +380,6 @@ separated with `;`.
"""
function streamslice(bd::BATLData, var::AbstractString, ax=nothing;
plotrange=[-Inf,Inf,-Inf,Inf], dir="x", sequence=1, kwargs...)

x,w = bd.x, bd.w
varstream = split(var, ";")
var1_ = findindex(bd, varstream[1])
Expand Down Expand Up @@ -469,9 +467,7 @@ Wrapper over `contour` in matplotlib.
"""
function PyPlot.contour(bd::BATLData, var::AbstractString, ax=nothing; levels::Int=0,
plotrange=[-Inf,Inf,-Inf,Inf], plotinterval=0.1, innermask=false, kwargs...)

Xi, Yi, Wi = getdata2d(bd, var, plotrange, plotinterval; innermask)

if isnothing(ax) ax = plt.gca() end

if levels != 0
Expand All @@ -489,9 +485,7 @@ Wrapper over `contourf` in matplotlib.
"""
function PyPlot.contourf(bd::BATLData, var::AbstractString, ax=nothing; levels::Int=0,
plotrange=[-Inf,Inf,-Inf,Inf], plotinterval=0.1, innermask=false, kwargs...)

Xi, Yi, Wi = getdata2d(bd, var, plotrange, plotinterval; innermask)

if isnothing(ax) ax = plt.gca() end

if levels != 0
Expand All @@ -508,15 +502,14 @@ Wrapper over `tricontourf` in matplotlib.
"""
function PyPlot.tricontourf(bd::BATLData, var::AbstractString, ax=nothing;
plotrange=[-Inf,Inf,-Inf,Inf], kwargs...)

x, w = bd.x, bd.w
varIndex_ = findindex(bd, var)

X = vec(x[:,:,1])
Y = vec(x[:,:,2])
W = vec(w[:,:,varIndex_])

# This needs to be modified!!!
#TODO This needs improvement.
if !all(isinf.(plotrange))
xyIndex = X .> plotrange[1] .& X .< plotrange[2] .&
Y .> plotrange[3] .& Y .< plotrange[4]
Expand All @@ -537,15 +530,14 @@ Wrapper over `plot_trisurf` in matplotlib.
"""
function PyPlot.plot_trisurf(bd::BATLData, var::AbstractString, ax=nothing;
plotrange=[-Inf,Inf,-Inf,Inf], kwargs...)

x, w = bd.x, bd.w
varIndex_ = findindex(bd, var)

X = vec(x[:,:,1])
Y = vec(x[:,:,2])
W = vec(w[:,:,varIndex_])

# This needs to be modified!!!
#TODO This needs improvement.
if !all(isinf.(plotrange))
xyIndex = X .> plotrange[1] .& X .< plotrange[2] .&
Y .> plotrange[3] .& Y .< plotrange[4]
Expand All @@ -567,7 +559,6 @@ Wrapper over `plot_surface` in matplotlib.
"""
function PyPlot.plot_surface(bd::BATLData, var::AbstractString;
plotrange=[-Inf,Inf,-Inf,Inf], plotinterval=0.1, innermask=false, kwargs...)

xi, yi, Wi = getdata2d(bd, var, plotrange, plotinterval; innermask)
Xi, Yi = meshgrid(xi, yi)

Expand All @@ -583,7 +574,6 @@ Wrapper over `pcolormesh` in matplotlib.
"""
function PyPlot.pcolormesh(bd::BATLData, var::AbstractString, ax=nothing;
plotrange=[-Inf,Inf,-Inf,Inf], plotinterval=0.1, innermask=false, kwargs...)

xi, yi, Wi = getdata2d(bd, var, plotrange, plotinterval; innermask)

if isnothing(ax) ax = plt.gca() end
Expand All @@ -600,7 +590,6 @@ Wrapper over `tripcolor` in matplotlib.
"""
function PyPlot.tripcolor(bd::BATLData, var::AbstractString, ax=nothing;
plotrange=[-Inf,Inf,-Inf,Inf], innermask=false, kwargs...)

x, w, ndim = bd.x, bd.w, bd.head.ndim

varIndex_ = findindex(bd, var)
Expand All @@ -609,13 +598,7 @@ function PyPlot.tripcolor(bd::BATLData, var::AbstractString, ax=nothing;
Y = @view x[:,:,2]
W = vec(w[:,:,varIndex_])

if any(abs.(plotrange) .== Inf)
if plotrange[1] == -Inf plotrange[1] = minimum(X) end
if plotrange[2] == Inf plotrange[2] = maximum(X) end
if plotrange[3] == -Inf plotrange[3] = minimum(Y) end
if plotrange[4] == Inf plotrange[4] = maximum(Y) end
end

adjust_plotrange!(plotrange, extrema(X), extrema(Y))
triang = matplotlib.tri.Triangulation(vec(X), vec(Y))

# Mask off unwanted triangles at the inner boundary.
Expand All @@ -635,8 +618,6 @@ function PyPlot.tripcolor(bd::BATLData, var::AbstractString, ax=nothing;

triang.set_mask(mask)
end


if isnothing(ax) ax = plt.gca() end

c = ax.tripcolor(triang, W; kwargs...)
Expand All @@ -656,21 +637,15 @@ Wrapper over `streamplot` in matplotlib .
"""
function PyPlot.streamplot(bd::BATLData, var::AbstractString, ax=nothing;
plotrange=[-Inf,Inf,-Inf,Inf], plotinterval=0.1, kwargs...)

x, w = bd.x, bd.w
varstream = split(var,";")
varstream = split(var, ";")
wnames = lowercase.(bd.head.wnames)
var1_ = findfirst(x->x==lowercase(varstream[1]), wnames)
var2_ = findfirst(x->x==lowercase(varstream[2]), wnames)

if bd.head.gencoord # generalized coordinates
X, Y = vec(x[:,:,1]), vec(x[:,:,2])
if any(isinf.(plotrange))
if plotrange[1] == -Inf plotrange[1] = minimum(X) end
if plotrange[2] == Inf plotrange[2] = maximum(X) end
if plotrange[3] == -Inf plotrange[3] = minimum(Y) end
if plotrange[4] == Inf plotrange[4] = maximum(Y) end
end
adjust_plotrange!(plotrange, extrema(X), extrema(Y))

# Create grid values first.
xi = range(Float64(plotrange[1]), stop=Float64(plotrange[2]), step=plotinterval)
Expand All @@ -685,7 +660,6 @@ function PyPlot.streamplot(bd::BATLData, var::AbstractString, ax=nothing;

interpolator = matplotlib.tri.LinearTriInterpolator(tr, w[:,1,var2_])
v2 = interpolator(Xi, Yi)

else # Cartesian coordinates
# Convert to Float64 to satisfy the equal space checking in streamplot.py
xrange = range(Float64(x[1,1,1]), Float64(x[end,1,1]), size(x,1))
Expand All @@ -696,18 +670,16 @@ function PyPlot.streamplot(bd::BATLData, var::AbstractString, ax=nothing;
v1 = w[:,:,var1_]'
v2 = w[:,:,var2_]'
else
if plotrange[1] == -Inf plotrange[1] = xrange[1] end
if plotrange[2] == Inf plotrange[2] = xrange[end] end
if plotrange[3] == -Inf plotrange[3] = yrange[1] end
if plotrange[4] == Inf plotrange[4] = yrange[end] end
adjust_plotrange!(plotrange, (xrange[1], xrange[end]), (yrange[1], yrange[end]))

w1, w2 = @views w[:,:,var1_], w[:,:,var2_]
xi = range(plotrange[1], stop=plotrange[2], step=plotinterval)
yi = range(plotrange[3], stop=plotrange[4], step=plotinterval)
interp1 = cubic_spline_interpolation((xrange, yrange), w1)
xyrange = (xrange, yrange)
interp1 = cubic_spline_interpolation(xyrange, w1)
v1 = [interp1(i, j) for j in yi, i in xi]

interp2 = cubic_spline_interpolation((xrange, yrange), w2)
interp2 = cubic_spline_interpolation(xyrange, w2)
v2 = [interp2(i, j) for j in yi, i in xi]
end
end
Expand Down Expand Up @@ -760,4 +732,13 @@ function set_colorbar(colorscale, vmin, vmax, data=[1.0])
end

cnorm, ticks
end

function adjust_plotrange!(plotrange, xlimit, ylimit)
plotrange[1] = ifelse(isinf(plotrange[1]), xlimit[1], plotrange[1])
plotrange[2] = ifelse(isinf(plotrange[2]), xlimit[2], plotrange[2])
plotrange[3] = ifelse(isinf(plotrange[3]), ylimit[1], plotrange[3])
plotrange[4] = ifelse(isinf(plotrange[4]), ylimit[2], plotrange[4])

return
end

2 comments on commit 329f443

@henry2004y
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/105575

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.6 -m "<description of version>" 329f44369d09f4ef048ba22d0dad5d33134cd7e6
git push origin v0.5.6

Please sign in to comment.