Skip to content

Commit

Permalink
Merge pull request #58 from KristofferC/tp/table-rewrite
Browse files Browse the repository at this point in the history
Rewrite of tables.
  • Loading branch information
tpapp authored Feb 9, 2018
2 parents c4d07af + b685411 commit 4211f29
Show file tree
Hide file tree
Showing 10 changed files with 474 additions and 95 deletions.
1 change: 1 addition & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ makedocs(
],
"Examples" => [
"examples/coordinates.md",
"examples/tables.md",
"examples/gallery.md",
"examples/juliatypes.md"
]
Expand Down
4 changes: 2 additions & 2 deletions docs/src/examples/gallery.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ pgf.@pgf pgf.Axis(
{
meta = "label"
},
x = [0.1, 0.45, 0.02, 0.06, 0.9 , 0.5 , 0.85, 0.12, 0.73, 0.53, 0.76, 0.55],
y = [0.15, 0.27, 0.17, 0.1, 0.5, 0.3, 0.52, 0.05, 0.45, 0.25, 0.5, 0.32],
x = [0.1, 0.45, 0.02, 0.06, 0.9 , 0.5 , 0.85, 0.12, 0.73, 0.53, 0.76, 0.55],
y = [0.15, 0.27, 0.17, 0.1, 0.5, 0.3, 0.52, 0.05, 0.45, 0.25, 0.5, 0.32],
label = ["a", "c", "a", "a", "b", "c", "b", "a", "b", "c", "b", "c"],
)
)
Expand Down
67 changes: 58 additions & 9 deletions docs/src/examples/juliatypes.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ pgf.@pgf pgf.Axis(
}
},
pgf.Table(
df, # <--- Creating a Table from a DataFrame
{
x = "SepalLength",
y = "SepalWidth",
meta = "Species" }
meta = "Species" },
df, # <--- Creating a Table from a DataFrame
)
),
pgf.Legend(["Setosa", "Versicolor", "Virginica"])
Expand All @@ -141,16 +141,65 @@ using Contour
x = 0.0:0.1:2π
y = 0.0:0.1:2π
f = (x,y) -> sin(x)*sin(y)
pgf.@pgf pgf.Plot(pgf.Table(contours(x, y, f.(x, y'), 6)),
{
contour_prepared,
very_thick
};
incremental = false
)
pgf.@pgf pgf.Plot({
contour_prepared,
very_thick
},
pgf.Table(contours(x, y, f.(x, y'), 6));
incremental = false)
savefigs("contour", ans) # hide
```

[\[.pdf\]](contour.pdf), [\[generated .tex\]](contour.tex)

![](contour.svg)

## StatsBase.jl

`StatsBase.Histogram` can be plotted using `Table`, both for 1D and 2D histograms.

```@example pgf
using StatsBase: Histogram, fit
pgf.Axis(pgf.Plot(pgf.Table(fit(Histogram, randn(100), closed = :left));
incremental = false),
pgf.@pgf {
"ybar interval",
"xticklabel interval boundaries",
xticklabel = raw"$[\pgfmathprintnumber\tick,\pgfmathprintnumber\nexttick)$",
"xticklabel style" =
{
font = raw"\tiny"
}
})
savefigs("histogram-1d", ans) # hide
```

[\[.pdf\]](histogram-1d.pdf), [\[generated .tex\]](histogram-1d.tex)

![](histogram-1d.svg)

```@example pgf
using StatsBase: Histogram, fit
h = fit(Histogram, (randn(1000), randn(1000)), closed = :left)
@pgf.pgf pgf.Axis(
{
view = (0, 90),
colorbar,
"colormap/jet"
},
pgf.Plot3(
{
surf,
shader = "flat",
},
pgf.Table(h);
incremental = false
)
)
savefigs("histogram-2d", ans) # hide
```

[\[.pdf\]](histogram-2d.pdf), [\[generated .tex\]](histogram-2d.tex)

![](histogram-2d.svg)
98 changes: 98 additions & 0 deletions docs/src/examples/tables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Tables

`Table`s are coordinates in a tabular format (essentially a matrix), optionally with named columns. They have various constructors, for direct construction and also for conversion from other types.

```jl
import PGFPlotsX
const pgf = PGFPlotsX
using LaTeXStrings
```

```@setup pgf
import PGFPlotsX
const pgf = PGFPlotsX
using LaTeXStrings
savefigs = (figname, obj) -> begin
pgf.save(figname * ".pdf", obj)
run(`pdf2svg $(figname * ".pdf") $(figname * ".svg")`)
pgf.save(figname * ".tex", obj);
return nothing
end
```

Let
```jl
x = linspace(0, 2*pi, 100)
y = sin.(x)
```

```@setup pgf
x = linspace(0, 2*pi, 100)
y = sin.(x)
```

You can pass these coordinates in unnamed columns:

```@example pgf
pgf.Plot(pgf.Table([x, y]); incremental = false)
savefigs("table-unnamed-columns", ans) # hide
```

[\[.pdf\]](table-unnamed-columns.pdf), [\[generated .tex\]](table-unnamed-columns.tex)

![](table-unnamed-columns.svg)

or named columns:

```@example pgf
pgf.Plot(pgf.Table([:x => x, :y => y]); incremental = false)
savefigs("table-named-columns", ans) # hide
```

[\[.pdf\]](table-named-columns.pdf), [\[generated .tex\]](table-named-columns.tex)

![](table-named-columns.svg)

or rename using options:

```@example pgf
pgf.@pgf pgf.Plot(
{
x = "a",
y = "b",
},
pgf.Table([:a => x, :b => y]);
incremental = false)
savefigs("table-dict-rename", ans) # hide
```

[\[.pdf\]](table-dict-rename.pdf), [\[generated .tex\]](table-dict-rename.tex)

![](table-dict-rename.svg)

In the example below, we use a matrix of values with edge vectors, and omit the points outside the unit circle:
```@example pgf
x = linspace(-1, 1, 20)
z = @. 1 - √(abs2(x) + abs2(x'))
z[z .≤ 0] .= -Inf
@pgf.pgf pgf.Axis(
{
colorbar,
"colormap/jet",
"unbounded coords" = "jump"
},
pgf.Plot3(
{
surf,
shader = "flat",
},
pgf.Table(x, x, z);
incremental = false
)
)
savefigs("table-jump-3d", ans) # hide
```

[\[.pdf\]](table-jump-3d.pdf), [\[generated .tex\]](table-jump-3d.tex)

![](table-jump-3d.svg)
14 changes: 13 additions & 1 deletion src/PGFPlotsX.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ end

include("utilities.jl")

function print_tex(io_main::IO, str::String)
function print_tex(io_main::IO, str::AbstractString)
print_indent(io_main) do io
print(io, str)
end
Expand All @@ -55,6 +55,18 @@ function print_tex(io_main::IO, vs::Vector)
end
end

function print_tex(io::IO, x::Real)
if isfinite(x)
print(io, x)
elseif isnan(x)
print(io, "nan")
elseif isinf(x)
s = x > 0 ? "+" : "-"
print(io, "$(s)inf")
else
throw(ArgumentError("Don't know how to print $x for LaTeX."))
end
end

print_tex(io::IO, v) = throw(ArgumentError(string("No tex function available for data of type $(typeof(v)). ",
"Define one by overloading print_tex(io::IO, data::T) ",
Expand Down
Loading

0 comments on commit 4211f29

Please sign in to comment.