Skip to content

Commit

Permalink
fixed number of small glitches
Browse files Browse the repository at this point in the history
robust handling of plot sizes in Pluto notebooks, no need
to have different fignums
  • Loading branch information
j-fu committed May 3, 2021
1 parent 877c520 commit 779bd74
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "GridVisualize"
uuid = "5eed8a63-0fb0-45eb-886d-8d5a387d12b8"
authors = ["Juergen Fuhrmann <[email protected]>"]
version = "0.2.1"
version = "0.2.2"

[deps]
ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4"
Expand Down
2 changes: 1 addition & 1 deletion src/dispatch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ default_plot_kwargs()=OrderedDict{Symbol,Pair{Any,String}}(
:elevation => Pair(0.0,"2D plot height factor for elevation"),
:colorlevels => Pair(51,"2D/3D contour plot: number of color levels"),
:colormap => Pair(:viridis,"2D/3D contour plot color map (any from [ColorSchemes.jl](https://juliagraphics.github.io/ColorSchemes.jl/stable/basics/#Pre-defined-schemes))"),
:colorbar => Pair(true,"2D/3D plot colorbar"),
:colorbar => Pair(:vertical,"2D/3D plot colorbar. One of [:none, :vertical, :horizontal]"),
:alpha => Pair(0.1,"3D outline surface alpha value"),
:interior => Pair(true,"3D plot interior of grid"),
:outline => Pair(true,"3D plot outline of domain"),
Expand Down
80 changes: 69 additions & 11 deletions src/makie.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,23 @@ scenekwargs(ctx)=Dict(:xticklabelsize => 0.5*ctx[:fontsize],
############################################################################################################
#1D grid

function makescene1d_grid(ctx)
Makie=ctx[:Plotter]
GL=Makie.GridLayout(parent=ctx[:figure])
GL[1,1]=ctx[:scene]
ncol=length(ctx[:cmap])
if ctx[:colorbar]!=:none
GL[2,1]=Makie.Colorbar(ctx[:figure],
colormap=Makie.cgrad(ctx[:cmap],categorical=true),
limits=(1,ncol),
height=15,
textsize=0.5*ctx[:fontsize],
ticklabelsize=0.5*ctx[:fontsize],
vertical=false)
end
GL
end

function gridplot!(ctx, TP::Type{MakieType}, ::Type{Val{1}}, grid)

Makie=ctx[:Plotter]
Expand Down Expand Up @@ -199,19 +216,37 @@ function gridplot!(ctx, TP::Type{MakieType}, ::Type{Val{1}}, grid)


if !haskey(ctx,:scene)
ctx[:scene]=Makie.Axis(ctx[:figure];title=ctx[:title], scenekwargs(ctx)...)
ctx[:scene]=Makie.Axis(ctx[:figure];yticklabelsvisible=false,yticksvisible=false,title=ctx[:title], scenekwargs(ctx)...)
ctx[:grid]=Makie.Node(grid)
cmap=region_cmap(nregions)
ctx[:cmap]=cmap
Makie.linesegments!(ctx[:scene],Makie.lift(g->basemesh(g), ctx[:grid]),color=:black)

coord=vec(grid[Coordinates])
xmin=minimum(coord)
xmax=maximum(coord)
h=(xmax-xmin)/40.0
Makie.scatter!(ctx[:scene],[Point2f0(xmin,-5*h),Point2f0(xmax,5*h)],color=:white,markersize=0.0,strokewidth=0)


for i=1:nregions
Makie.linesegments!(ctx[:scene],Makie.lift(g->regionmesh(g,i), ctx[:grid]) , color=cmap[i], strokecolor=:black,linewidth=4)
Makie.linesegments!(ctx[:scene],Makie.lift(g->regionmesh(g,i), ctx[:grid]) ,
color=cmap[i], strokecolor=:black,linewidth=4, label="c $(i)")
end

bcmap=bregion_cmap(nbregions)
for i=1:nbregions
Makie.linesegments!(ctx[:scene],Makie.lift(g->bmesh(g,i),ctx[:grid]), color=bcmap[i], linewidth=4)
Makie.linesegments!(ctx[:scene],Makie.lift(g->bmesh(g,i),ctx[:grid]),
color=bcmap[i], linewidth=4, label="b$(i)")
end
add_scene!(ctx,ctx[:scene])

if ctx[:legend]!=:none
pos=ctx[:legend]==:best ? :rt : ctx[:legend]
Makie.axislegend(ctx[:scene],position=pos,labelsize=0.5*ctx[:fontsize],
nbanks=5)
end

add_scene!(ctx, ctx[:scene])
Makie.display(ctx[:figure])
else
ctx[:grid][]=grid
Expand All @@ -222,6 +257,7 @@ end

########################################################################
# 1D function

function scalarplot!(ctx, TP::Type{MakieType}, ::Type{Val{1}}, grid,func)
Makie=ctx[:Plotter]

Expand Down Expand Up @@ -332,15 +368,19 @@ function scalarplot!(ctx, TP::Type{MakieType}, ::Type{Val{1}}, grid,func)
Makie.lines!(ctx[:scene],Makie.lift(a->a, ctx[:lines][end]),
linestyle=ctx[:linestyle],
linewidth=ctx[:linewidth],
color=RGB(ctx[:color]),
label=ctx[:label])
else
Makie.lines!(ctx[:scene],Makie.lift(a->a, ctx[:lines][end]),
linestyle=ctx[:linestyle],
color=RGB(ctx[:color]),
linewidth=ctx[:linewidth])

Makie.scatter!(ctx[:scene],
Makie.lift(a->a[1:ctx[:markevery]:end],ctx[:lines][end]),
color=RGB(ctx[:color]),
marker=ctx[:markershape],
markercolor=RGB(ctx[:color]),
markersize=ctx[:markersize])

if ctx[:label]!=""
Expand All @@ -351,7 +391,8 @@ function scalarplot!(ctx, TP::Type{MakieType}, ::Type{Val{1}}, grid,func)
marker=ctx[:markershape],
markersize=ctx[:markersize],
markercolor=RGB(ctx[:color]),
color=RGB(ctx[:color]),label=ctx[:label])
color=RGB(ctx[:color]),
label=ctx[:label])
end
end
if ctx[:label]!=""
Expand Down Expand Up @@ -381,7 +422,11 @@ function makescene2d(ctx)
Makie=ctx[:Plotter]
GL=Makie.GridLayout(parent=ctx[:figure])
GL[1,1]=ctx[:scene]
GL[1,2]=Makie.Colorbar(ctx[:figure],ctx[:poly],width=15, textsize=0.5*ctx[:fontsize],ticklabelsize=0.5*ctx[:fontsize])
if ctx[:colorbar]==:vertical
GL[1,2]=Makie.Colorbar(ctx[:figure],ctx[:poly],width=15, textsize=0.5*ctx[:fontsize],ticklabelsize=0.5*ctx[:fontsize])
elseif ctx[:colorbar]==:horizontal
GL[2,1]=Makie.Colorbar(ctx[:figure],ctx[:poly],height=15, textsize=0.5*ctx[:fontsize],ticklabelsize=0.5*ctx[:fontsize],vertical=false)
end
GL
end

Expand All @@ -390,10 +435,18 @@ function makescene2d_grid(ctx)
GL=Makie.GridLayout(parent=ctx[:figure])
GL[1,1]=ctx[:scene]
ncol=length(ctx[:cmap])
GL[1,2]=Makie.Colorbar(ctx[:figure],
colormap=Makie.cgrad(ctx[:cmap],categorical=true),
limits=(1,ncol),
width=15, textsize=0.5*ctx[:fontsize],ticklabelsize=0.5*ctx[:fontsize])
if ctx[:colorbar]==:vertical
GL[1,2]=Makie.Colorbar(ctx[:figure],
colormap=Makie.cgrad(ctx[:cmap],categorical=true),
limits=(1,ncol),
width=15, textsize=0.5*ctx[:fontsize],ticklabelsize=0.5*ctx[:fontsize])
elseif ctx[:colorbar]==:horizontal
GL[2,1]=Makie.Colorbar(ctx[:figure],
colormap=Makie.cgrad(ctx[:cmap],categorical=true),
limits=(1,ncol),
heigth=15, textsize=0.5*ctx[:fontsize],ticklabelsize=0.5*ctx[:fontsize],
vertical=false)
end
GL
end

Expand Down Expand Up @@ -546,7 +599,12 @@ function makescene3d(ctx)
end
GL[1,1 ]=ctx[:scene]
if haskey(ctx,:mesh)
GL[1,2]=Makie.Colorbar(ctx[:figure],ctx[:mesh],width=15,textsize=0.5*ctx[:fontsize],ticklabelsize=0.5*ctx[:fontsize])
if ctx[:colorbar]==:vertical
GL[1,2]=Makie.Colorbar(ctx[:figure],ctx[:mesh],width=15,textsize=0.5*ctx[:fontsize],ticklabelsize=0.5*ctx[:fontsize])
elseif ctx[:colorbar]==:horizontal
GL[2,1]=Makie.Colorbar(ctx[:figure],ctx[:mesh],height=15,textsize=0.5*ctx[:fontsize],ticklabelsize=0.5*ctx[:fontsize],
vertical=false)
end
end
# Put the status label into protrusion space on the bottom of the scene
GL[1,1,Makie.Bottom()]=Makie.Label(ctx[:figure],ctx[:status],tellwidth=false,height=30,textsize=0.5*ctx[:fontsize])
Expand Down
41 changes: 30 additions & 11 deletions src/pyplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ function initialize!(p, ::Type{PyPlotType})
PyPlot.PyObject(PyPlot.axes3D)# see https://github.com/JuliaPy/PyPlot.jl/issues/351
if !haskey(p.context,:figure)
res=p.context[:resolution]
p.context[:figure]=PyPlot.figure(p.context[:fignumber])
if isdefined(Main, :PlutoRunner) # allow to reset figure size for Pluto
PyPlot.close(p.context[:figure])
end
p.context[:figure]=PyPlot.figure(p.context[:fignumber],figsize=(res[1]/100,res[2]/100),dpi=100)
for ctx in p.subplots
ctx[:figure]=p.context[:figure]
Expand Down Expand Up @@ -123,36 +127,36 @@ function gridplot!(ctx, TP::Type{PyPlotType}, ::Type{Val{1}}, grid)

xmin=minimum(coord)
xmax=maximum(coord)
h=(xmax-xmin)/40.0
ax.set_aspect(ctx[:aspect])
h=(xmax-xmin)/20.0
# ax.set_aspect(ctx[:aspect])
ax.get_yaxis().set_ticks([])
ax.set_ylim(-5*h,xmax-xmin)
cmap=region_cmap(ncellregions)

for icell=1:num_cells(grid)
ireg=cellregions[icell]
label = crflag[ireg] ? "cellregion $(ireg)" : ""
label = crflag[ireg] ? "c$(ireg)" : ""
crflag[ireg]=false

x1=coord[1,cellnodes[1,icell]]
x2=coord[1,cellnodes[2,icell]]
ax.plot([x1,x1],[-h,h],linewidth=0.5,color="k",label="")
ax.plot([x2,x2],[-h,h],linewidth=0.5,color="k",label="")
ax.plot([x1,x2],[0,0],linewidth=3.0,color=rgbtuple(cmap[cellregions[icell]]),label=label)
ax.plot([x1,x1],[-h,h],linewidth=ctx[:linewidth],color="k",label="")
ax.plot([x2,x2],[-h,h],linewidth=ctx[:linewidth],color="k",label="")
end

cmap=bregion_cmap(ncellregions)
for ibface=1:num_bfaces(grid)
ireg=bfaceregions[ibface]
if ireg >0
label = brflag[ireg] ? "boundary $(ireg)" : ""
label = brflag[ireg] ? "b$(ireg)" : ""
brflag[ireg]=false
x1=coord[1,bfacenodes[1,ibface]]
ax.plot([x1,x1],[-2*h,2*h],linewidth=3.0,color=rgbtuple(cmap[ireg]),label=label)
end
end
if ctx[:legend]!=:none
ax.legend(loc=leglocs[ctx[:legend]])
ax.legend(loc=leglocs[ctx[:legend]],ncol=5)
end
reveal(ctx,TP)
end
Expand Down Expand Up @@ -187,7 +191,15 @@ function gridplot!(ctx, TP::Type{PyPlotType}, ::Type{Val{2}},grid)
tridat=tridata(grid)
cmap=region_cmap(ncellregions)
cdata=ax.tripcolor(tridat...,facecolors=grid[CellRegions],cmap=PyPlot.ColorMap(cmap,length(cmap)))
cbar=fig.colorbar(cdata,ax=ax,ticks=collect(1:ncellregions))

if ctx[:colorbar]==:horizontal
cbar=fig.colorbar(cdata,ax=ax,ticks=collect(1:ncellregions),orientation="horizontal")
end

if ctx[:colorbar]==:vertical
cbar=fig.colorbar(cdata,ax=ax,ticks=collect(1:ncellregions),orientation="vertical")
end

ax.triplot(tridat...,color="k",linewidth=ctx[:linewidth])


Expand Down Expand Up @@ -370,7 +382,7 @@ function scalarplot!(ctx, TP::Type{PyPlotType}, ::Type{Val{1}},grid, func)
# ax.scatter(ampoints[1,:], ampoints[2,:],color=ctx[:color],label="")

end

ax.grid()
if ctx[:legend]!=:none
ax.legend(loc=leglocs[ctx[:legend]])
end
Expand Down Expand Up @@ -414,10 +426,17 @@ function scalarplot!(ctx, TP::Type{PyPlotType}, ::Type{Val{2}},grid, func)
c.set_edgecolor("face")
end
ax.tricontour(ctx[:tridata]...,func,colors="k",levels=isolines)



if ctx[:colorbar]
ctx[:cbar]=fig.colorbar(cnt,ax=ax,ticks=isolines,boundaries=colorlevels)
if ctx[:colorbar]==:horizontal
ctx[:cbar]=fig.colorbar(cnt,ax=ax,ticks=isolines,boundaries=colorlevels, orientation="horizontal")
end

if ctx[:colorbar]==:vertical
ctx[:cbar]=fig.colorbar(cnt,ax=ax,ticks=isolines,boundaries=colorlevels, orientation="vertical")
end

reveal(ctx,TP)
end

Expand Down

2 comments on commit 779bd74

@j-fu
Copy link
Member Author

@j-fu j-fu commented on 779bd74 May 3, 2021

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/35998

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.2.2 -m "<description of version>" 779bd7444988db901b4dfe9895a6ba986dce0fed
git push origin v0.2.2

Please sign in to comment.