Skip to content

Commit 55e84da

Browse files
authored
fixed issue #4880 (#5060)
* fixed issue #4880 * documentation of issue#4880
1 parent 1b5cc85 commit 55e84da

File tree

4 files changed

+13
-0
lines changed

4 files changed

+13
-0
lines changed

PlotsBase/src/recipes.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,14 @@ end
410410

411411
# create a bar plot as a filled step function
412412
@recipe function f(::Type{Val{:bar}}, x, y, z) # COV_EXCL_LINE
413+
if typeof(y) <: NamedTuple
414+
names = collect(keys(y))
415+
values_y = [values(y)...]
416+
plotnames = string.(names)
417+
418+
x = plotnames
419+
y = values_y
420+
end
413421
ywiden --> false
414422
procx, procy, xscale, yscale, _ = _preprocess_barlike(plotattributes, x, y)
415423
nx, ny = length(procx), length(procy)

PlotsBase/src/shorthands.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ julia> scatter([(1,4),(2,5),(3,6)])
2525
bar!(x,y)
2626
2727
Make a bar plot of `y` vs `x`.
28+
If `y` is a named tuple the keys are used as ticklabels.
2829
2930
# Keyword arguments
3031
- $(_document_argument(:bar_position))
@@ -37,6 +38,7 @@ Make a bar plot of `y` vs `x`.
3738
```julia-repl
3839
julia> bar([1,2,3],[4,5,6],fillcolor=[:red,:green,:blue],fillalpha=[0.2,0.4,0.6])
3940
julia> bar([(1,4),(2,5),(3,6)])
41+
julia> bar((A=5, B=8, C=3, D=5, E=8, F=10, G=3))
4042
```
4143
"""
4244
@shorthands bar

RecipesPipeline/src/series.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ _prepare_series_data(::Nothing) = nothing
1010
_prepare_series_data(t::Tuple{T,T}) where {T<:Number} = t
1111
_prepare_series_data(f::Function) = f
1212
_prepare_series_data(ar::AbstractRange{<:Number}) = ar
13+
_prepare_series_data(nt::NamedTuple) = nt
1314
function _prepare_series_data(a::AbstractArray{T}) where {T<:MaybeNumber}
1415
# Get a non-missing AbstractFloat type for the array
1516
# There may be a better way to do this?
@@ -67,6 +68,7 @@ _compute_x(x::Nothing, y::Nothing, z) = axes(z, 1)
6768
_compute_x(x::Nothing, y, z) = axes(y, 1)
6869
_compute_x(x::Function, y, z) = map(x, y)
6970
_compute_x(x, y, z) = x
71+
_compute_x(x::Nothing, y::NamedTuple, z) = length(y)
7072

7173
_compute_y(x::Nothing, y::Nothing, z) = axes(z, 2)
7274
_compute_y(x, y::Function, z) = map(y, x)

RecipesPipeline/src/user_recipe.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ end
301301
@recipe f(v::AVec{<:Tuple}) = unzip(v)
302302
@recipe f(tup::Tuple) = [tup]
303303

304+
304305
# list of NamedTuples
305306
@recipe function f(ntv::AVec{<:NamedTuple{K,Tuple{S,T}}}) where {K,S,T} # COV_EXCL_LINE
306307
xguide --> string(K[1])

0 commit comments

Comments
 (0)