Skip to content

Commit

Permalink
Default plotter handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
j-fu committed May 2, 2021
1 parent 10d7975 commit c0947ef
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 52 deletions.
5 changes: 3 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name = "GridVisualize"
uuid = "5eed8a63-0fb0-45eb-886d-8d5a387d12b8"
authors = ["Juergen Fuhrmann <[email protected]>"]
version = "0.2.0"
version = "0.2.1"

[deps]
AbstractPlotting = "537997a7-5e4e-5d89-9595-2241ea00577e"
ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4"
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
Expand All @@ -22,7 +23,7 @@ Colors = "0.12,0.13,1"
DocStringExtensions = "^0.8.0"
ElasticArrays = "1"
ExtendableGrids = "0.7,0.8"
GeometryBasics = "^0.3.0"
GeometryBasics = "0.3"
OrderedCollections = "1"
PkgVersion = "0.1"
StaticArrays = "0.12,1"
Expand Down
33 changes: 29 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[![Build status](https://github.com/j-fu/GridVisualize.jl/workflows/linux-macos-windows/badge.svg)](https://github.com/j-fu/GridVisualize.jl/actions)
[![](https://img.shields.io/badge/docs-stable-blue.svg)](https://j-fu.github.io/GridVisualize.jl/stable)
[![](https://img.shields.io/badge/docs-dev-blue.svg)](https://j-fu.github.io/GridVisualize.jl/dev)

GridVisualize
=============
Expand All @@ -18,6 +17,10 @@ gridplot(grid, Plotter=PyPlot)
scalarplot(grid, function,Plotter=PyPlot)
````

This works for 1/2/3D grids and either a function represented by its
values on the nodes of the grid, or a scalar function of 1, 2 or 3
variables, respectively.

### Multiple plots in one plotting window:
````
vis=GridVisualizer(Plotter=GLMakie, layout=(1,2))
Expand All @@ -36,12 +39,33 @@ for i=1:N
end
````

### Setting a default plotter

Instead of specifying a `Plotter` in calls to `GridVisualizer`,
`gridplot` or `scalarplot`, a default plotter can be set:

```
default_plotter!(PyPlot)
gridplot(grid)
scalarplot(grid, function)
```

or
```
default_plotter!(GLMakie)
vis=GridVisualizer(layout=(1,2))
gridplot!(vis[1,1],grid)
scalarplot!(vis[1,2],grid,function)
```

### Switching off plotting
Just pass `Plotter=nothing` in the respective places, and all
plotting functions will do nothing.
Just pass `Plotter=nothing` in the respective places, or set `default_plotter!(nothing)`
and all plotting functions will do nothing. This also is the default.

## Available plotting backends and functionality.

The main supported backends are GLMakie and PyPlot.

- 'i' means some level of interactive control
- '(y)' means avaiability only on rectangular resp. cuboid grids.

Expand All @@ -55,6 +79,8 @@ plotting functions will do nothing.
| grid, 3D | y | y,i | no | y,i |




### [PyPlot](https://github.com/JuliaPy/PyPlot.jl):
<img src="docs/src/assets/multiscene_pyplot.png?raw=true" width=300/>

Expand All @@ -75,4 +101,3 @@ plotting functions will do nothing.
## Notebooks
Plotting within Pluto notebooks for PyPlot, Plots, GLMakie is working.

I have no resources to support Jupyter notebooks.
7 changes: 4 additions & 3 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ENV["MPLBACKEND"]="agg"
using Documenter, ExtendableGrids, Literate, GridVisualize
using GridVisualize.FlippableLayout
import PyPlot

plotting=joinpath(@__DIR__,"..","examples","plotting.jl")
Expand All @@ -16,8 +17,8 @@ example_md_dir = joinpath(@__DIR__,"src","examples")
function mkdocs()

Literate.markdown(plotting, example_md_dir, documenter=false,info=false)
makeplots(example_md_dir)
generated_examples=joinpath.("examples",filter(x->endswith(x, ".md"),readdir(example_md_dir)))
# makeplots(example_md_dir)
# generated_examples=joinpath.("examples",filter(x->endswith(x, ".md"),readdir(example_md_dir)))
makedocs(sitename="GridVisualize.jl",
modules = [GridVisualize],
doctest = false,
Expand All @@ -26,7 +27,7 @@ function mkdocs()
repo="https://github.com/j-fu/GridVisualize.jl",
pages=[
"Home"=>"index.md",
"Examples" => generated_examples,
# "Examples" => generated_examples,
"Public API"=> "api.md",
"Private API"=> "privapi.md",
])
Expand Down
4 changes: 2 additions & 2 deletions docs/src/privapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ Pages = ["pyplot.jl"]

## Makie
```@autodocs
Modules = [GridVisualize]
Modules = [GridVisualize,FlippableLayout]
Private = true
Pages = ["makie.jl"]
Pages = ["makie.jl", "flippablelayout.jl"]
```

## Plots
Expand Down
8 changes: 4 additions & 4 deletions examples/plotting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ end
# explore the inner triangulation.
#
# For Makie and VTKView, the cutplane values can be controlled interactively.
function plotting_grid3d(;Plotter=nothing, kwargs...)
function plotting_grid3d(;Plotter=default_plotter(), kwargs...)
gridplot(grid3d(); Plotter=Plotter, kwargs...)
end
# ![](plotting_grid3d.svg)
Expand Down Expand Up @@ -71,7 +71,7 @@ end
# to control an isolevel.
#
# For Makie and VTKView, the cutplane values and the flevel can be controlled interactively.
function plotting_func3d(;Plotter=nothing, kwargs...)
function plotting_func3d(;Plotter=default_plotter(), kwargs...)
g,f=func3d()
scalarplot(g,f; Plotter=Plotter, zplane=0.49,xplane=0.49,flevel=0.25, kwargs...)
end
Expand All @@ -84,9 +84,9 @@ end
# The ',' key for GLMakie and the '*' key for VTKView allow to
# switch between gallery view (default) and focused view of only
# one subscene.
function plotting_multiscene(;Plotter=nothing)
p=GridVisualizer(;Plotter=Plotter,layout=(2,3),clear=true,resolution=(800,500))
function plotting_multiscene(;Plotter=default_plotter())

p=GridVisualizer(;Plotter=Plotter,layout=(2,3),clear=true,resolution=(800,500))
gridplot!(p[1,1],grid1d(), title="1D grid")
scalarplot!(p[2,1],grid1d(), sin, title="1D grid function", label="sin",markershape=:diamond,color=:red,legend=:rb)
scalarplot!(p[2,1],grid1d(), cos, title="1D grid function", label="cos",linestyle=:dash,markershape=:none,color=:green,clear=false)
Expand Down
3 changes: 2 additions & 1 deletion src/GridVisualize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ include("plots.jl")

export scalarplot,scalarplot!
export gridplot,gridplot!
export save,reveal
export save,reveal,backend!
export isplots,isvtkview,ispyplot,ismakie
export GridVisualizer, SubVisualizer
export plottertype, available_kwargs
export default_plotter!, default_plotter
export PyPlotType,MakieType,PlotsType,VTKViewType

end
61 changes: 44 additions & 17 deletions src/dispatch.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
"""
`nothing` as initial default plotter
"""
default_backend=nothing

"""
$(SIGNATURES)
Return default plotter backend
"""
function default_plotter()
global default_backend
default_backend
end

"""
````
plotter!(Plotter)
````
Set plotter module as the default plotter backend.
"""
function default_plotter!(Plotter)
global default_backend=Plotter
end

"""
$(SIGNATURES)
Expand Down Expand Up @@ -125,12 +150,12 @@ end

"""
````
GridVisualizer(; Plotter=nothing , kwargs...)
GridVisualizer(; Plotter=default_plotter() , kwargs...)
````
Create a grid visualizer
Plotter: defaults to `nothing` and can be `PyPlot`, `Plots`, `VTKView`, `Makie`.
Plotter: defaults to `default_plotter()` and can be `PyPlot`, `Plots`, `VTKView`, `Makie`.
This pattern to pass the backend as a module to a plot function allows to circumvent
to create heavy default package dependencies.
Expand All @@ -154,18 +179,19 @@ is equivalent to
```julia
vis=GridVisualizer(Plotter=PyPlot, layout=(1,1))
gridplot!(vis,grid)
reveal(vis)
```
Please note that the return values of all plot commands are specific to the Plotter.
Depending on the backend, interactive mode switch between "gallery view" showing all plots at
onece and "focused view" showing only one plot is possible.
An interactive mode switch key for GLMakie (`,`) and VTKView (`*`) allows to
toggle between "gallery view" showing all plots at once and "focused view" showing only one plot.
Keyword arguments: see [`available_kwargs`](@ref)
"""
function GridVisualizer(;Plotter::Union{Module,Nothing}=nothing, kwargs...)
function GridVisualizer(;Plotter::Union{Module,Nothing}=default_plotter(), kwargs...)
default_ctx=Dict{Symbol,Any}( k => v[1] for (k,v) in default_plot_kwargs())
_update_context!(default_ctx,kwargs)
layout=default_ctx[:layout]
Expand Down Expand Up @@ -299,14 +325,14 @@ gridplot!(p::GridVisualizer,grid::ExtendableGrid, kwargs...)= gridplot!(p[1,1],g

"""
````
gridplot(grid; Plotter=nothing; kwargs...)
gridplot(grid; Plotter=default_plotter(); kwargs...)
````
Create grid visualizer and plot grid
Keyword arguments: see [`available_kwargs`](@ref)
"""
gridplot(grid::ExtendableGrid; Plotter=nothing, kwargs...)=gridplot!(GridVisualizer(Plotter=Plotter; show=true, kwargs...),grid)
gridplot(grid::ExtendableGrid; Plotter=default_plotter(), kwargs...)=gridplot!(GridVisualizer(Plotter=Plotter; show=true, kwargs...),grid)


"""
Expand Down Expand Up @@ -344,24 +370,24 @@ scalarplot!(ctx::GridVisualizer,X::AbstractVector,Y::AbstractVector,Z::AbstractV

"""
````
scalarplot(grid,vector; Plotter=nothing)
scalarplot(grid,function; Plotter=nothing)
scalarplot(X,vector; Plotter=nothing)
scalarplot(X,function; Plotter=nothing)
scalarplot(X,Y,function; Plotter=nothing)
scalarplot(X,Y,Z,function; Plotter=nothing)
scalarplot(grid,vector; Plotter=default_plotter())
scalarplot(grid,function; Plotter=default_plotter())
scalarplot(X,vector; Plotter=default_plotter())
scalarplot(X,function; Plotter=default_plotter())
scalarplot(X,Y,function; Plotter=default_plotter())
scalarplot(X,Y,Z,function; Plotter=default_plotter())
````
Plot node vector on grid as P1 FEM function on the triangulation.
If instead of the node vector, a function is given, it will be evaluated on the grid.
If instead of the grid, a vectors for coordinates are given, a grid is created automatically.
If instead of the grid, vectors for coordinates are given, a grid is created automatically.
Keyword arguments: see [`available_kwargs`](@ref)
"""
scalarplot(grid::ExtendableGrid,func ;Plotter=nothing,kwargs...) = scalarplot!(GridVisualizer(Plotter=Plotter;kwargs...),grid,func,show=true)
scalarplot(grid::ExtendableGrid,func ;Plotter=default_plotter(),kwargs...) = scalarplot!(GridVisualizer(Plotter=Plotter;kwargs...),grid,func,show=true)
scalarplot(X::AbstractVector,func ;kwargs...)=scalarplot(simplexgrid(X),func;kwargs...)
scalarplot(X::AbstractVector,Y::AbstractVector,func ;kwargs...)=scalarplot(simplexgrid(X,Y),func;kwargs...)
scalarplot(X::AbstractVector,Y::AbstractVector,Z::AbstractVector, func ;kwargs...)=scalarplot(simplexgrid(X,Y,Z),func;kwargs...)
Expand All @@ -379,14 +405,14 @@ $(SIGNATURES)
Save last plotted figure from visualizer to disk.
"""
save(fname,visualizer::GridVisualizer)=save(fname,p, plottertype(p.Plotter))
save(fname::String,visualizer::GridVisualizer)=save(fname,p, plottertype(p.Plotter))

"""
$(SIGNATURES)
Save scene returned from [`reveal`](@ref), [`scalarplot`](@ref) or [`gridplot`](@ref) to disk.
"""
save(fname,scene;Plotter::Union{Module,Nothing}=nothing)=save(fname,scene, Plotter, plottertype(Plotter))
save(fname::String,scene;Plotter::Union{Module,Nothing}=default_plotter())=save(fname,scene, Plotter, plottertype(Plotter))



Expand All @@ -402,6 +428,7 @@ gridplot!(ctx, ::Type{Nothing}, ::Type{Val{2}}, grid)=nothing
gridplot!(ctx, ::Type{Nothing}, ::Type{Val{3}}, grid)=nothing

scalarplot!(ctx::Nothing,grid::ExtendableGrid,func;kwargs...)=nothing
scalarplot!(ctx::Nothing,grid::ExtendableGrid,func::Function;kwargs...)=nothing
scalarplot!(ctx, ::Type{Nothing}, ::Type{Val{1}},grid,func)=nothing
scalarplot!(ctx, ::Type{Nothing}, ::Type{Val{2}},grid,func)=nothing
scalarplot!(ctx, ::Type{Nothing}, ::Type{Val{3}},grid,func)=nothing
Expand Down
Loading

2 comments on commit c0947ef

@j-fu
Copy link
Member Author

@j-fu j-fu commented on c0947ef May 2, 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/35848

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.1 -m "<description of version>" c0947efaabc3907c6226ade18ab4306bb569355d
git push origin v0.2.1

Please sign in to comment.