Skip to content

Commit

Permalink
Merge pull request #109 from MaximeBouton/errorbars
Browse files Browse the repository at this point in the history
Error bars for BarChart
  • Loading branch information
MaximeBouton committed Jan 22, 2019
2 parents fd80ae5 + e3b5939 commit a7ceaeb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
24 changes: 22 additions & 2 deletions doc/PGFPlots.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7103,6 +7103,22 @@
" xlabel=\"vegetables\", ylabel=\"counts\", style=\"bar width=25pt\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Error bars on the value can be added using the `errorBars` keyword."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Plots.BarChart([\"a\", \"b\", \"c\"], [2, 3, 5], errorBars=ErrorBars(y=[0.5, 0.7, 1.5]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -32214,17 +32230,21 @@
}
],
"metadata": {
"@webio": {
"lastCommId": "6c941d19ec7b45458ad56b15a368addc",
"lastKernelId": "15b18b64-2b16-419c-8dba-6d7bbf1cb42b"
},
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Julia 1.0.0",
"display_name": "Julia 1.0.2",
"language": "julia",
"name": "julia-1.0"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.0.0"
"version": "1.0.2"
}
},
"nbformat": 4,
Expand Down
32 changes: 26 additions & 6 deletions src/PGFPlots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,16 @@ end

function plotHelper(o::IO, p::BarChart)
print(o, "\\addplot+ ")

optionHelper(o, barMap, p, brackets=true)
println(o, "coordinates {")
for (k,v) in zip(p.keys, p.values)
println(o, "($k, $v)")
if p.errorBars == nothing
optionHelper(o, barMap, p, brackets=true)
println(o, "coordinates {")
for (k,v) in zip(p.keys, p.values)
println(o, "($k, $v)")
end
println(o, "};")
else
plotHelperErrorBars(o, p)
end
println(o, "};")
plotLegend(o, p.legendentry)
end
function PGFPlots.Axis(p::BarChart; kwargs...)
Expand Down Expand Up @@ -441,6 +444,23 @@ function plotHelperErrorBars(o::IO, p::Linear)
end
println(o, "};")
end
# todo there is a lot of code redundancy
function plotHelperErrorBars(o::IO, p::BarChart)
println(o, "[")
optionHelper(o, barMap, p, brackets=false)
println(o, ", error bars/.cd, ")
optionHelper(o, errorbarsMap, p.errorBars, otherText=["x dir=both", "x explicit", "y dir=both", "y explicit"])
println(o, "]")
println(o, "table [")
println(o, "x error plus=ex+, x error minus=ex-, y error plus=ey+, y error minus=ey-")
println(o, "] {")
println(o, "x y ex+ ex- ey+ ey-")
x = p.errorBars.data
for i = 1:length(p.values)
println(o, "$(p.keys[i]) $(p.values[i]) $(x[1,i]) $(x[2,i]) $(x[3,i]) $(x[4,i])")
end
println(o, "};")
end

function plotHelper(o::IO, p::Linear)
print(o, "\\addplot+ ")
Expand Down
3 changes: 2 additions & 1 deletion src/plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ mutable struct BarChart <: Plot
values
style
legendentry
errorBars

BarChart(keys::AbstractVector, values::AbstractVector{R}; style=nothing, legendentry=nothing) where {R<:Real} = new(keys, values, style, legendentry)
BarChart(keys::AbstractVector, values::AbstractVector{R}; style=nothing, legendentry=nothing, errorBars=nothing) where {R<:Real} = new(keys, values, style, legendentry, errorBars)
end
function BarChart(
values::AbstractVector{R};
Expand Down

0 comments on commit a7ceaeb

Please sign in to comment.