Skip to content

Support vertical histograms #250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Here is a list of the main high-level functions for common scenarios:
* [`lineplot`](https://github.com/JuliaPlots/UnicodePlots.jl#lineplot) (Line Plot)
* [`stairs`](https://github.com/JuliaPlots/UnicodePlots.jl#staircase-plot) (Staircase Plot)
* [`barplot`](https://github.com/JuliaPlots/UnicodePlots.jl#barplot) (Bar Plot - horizontal)
* [`histogram`](https://github.com/JuliaPlots/UnicodePlots.jl#histogram) (Histogram - horizontal)
* [`histogram`](https://github.com/JuliaPlots/UnicodePlots.jl#histogram) (Histogram - horizontal / vertical)
* [`boxplot`](https://github.com/JuliaPlots/UnicodePlots.jl#boxplot) (Box Plot - horizontal)
* [`spy`](https://github.com/JuliaPlots/UnicodePlots.jl#sparsity-pattern) (Sparsity Pattern)
* [`densityplot`](https://github.com/JuliaPlots/UnicodePlots.jl#density-plot) (Density Plot)
Expand Down Expand Up @@ -156,17 +156,23 @@ barplot(["Paris", "New York", "Moskau", "Madrid"],
<details open> <summary><a name=histogram></a><b>Histogram</b></summary>

```julia
histogram(randn(1000) .* .1, nbins=15, closed=:left)
histogram(randn(1_000) .* .1, nbins=15, closed=:left)
```
![Histogram](https://github.com/JuliaPlots/UnicodePlots.jl/raw/unicodeplots-docs/2.10/histogram1.png)


The `histogram` function also supports axis scaling using the parameter `xscale`:

```julia
histogram(randn(1000) .* .1, nbins=15, closed=:right, xscale=:log10)
histogram(randn(1_000) .* .1, nbins=15, closed=:right, xscale=:log10)
```
![Histogram](https://github.com/JuliaPlots/UnicodePlots.jl/raw/unicodeplots-docs/2.10/histogram2.png)


Vertical histograms are supported: ```julia
histogram(randn(1_000_000) .* .1, nbins=100, vertical=true)
```
![Histogram](https://github.com/JuliaPlots/UnicodePlots.jl/raw/unicodeplots-docs/2.10/histogram3.png)
</details>

<details open> <summary><a name=boxplot></a><b>Boxplot</b></summary>
Expand Down Expand Up @@ -206,8 +212,8 @@ spy(sprandn(50, 120, .9), show_zeros=true)
<details open> <summary><a name=density-plot></a><b>Density Plot</b></summary>

```julia
plt = densityplot(randn(1000), randn(1000))
densityplot!(plt, randn(1000) .+ 2, randn(1000) .+ 2)
plt = densityplot(randn(1_000), randn(1_000))
densityplot!(plt, randn(1_000) .+ 2, randn(1_000) .+ 2)
```
![Densityplot](https://github.com/JuliaPlots/UnicodePlots.jl/raw/unicodeplots-docs/2.10/densityplot1.png)
</details>
Expand Down
14 changes: 9 additions & 5 deletions docs/generate_docs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ main() = begin
[2.244, 8.406, 11.92, 3.165],
title="Population")
"""),
histogram1 = ("Histogram", "histogram(randn(1000) .* .1, nbins=15, closed=:left)"),
histogram2 = ("Histogram", "histogram(randn(1000) .* .1, nbins=15, closed=:right, xscale=:log10)"),
histogram1 = ("Histogram", "histogram(randn(1_000) .* .1, nbins=15, closed=:left)"),
histogram2 = ("Histogram", "histogram(randn(1_000) .* .1, nbins=15, closed=:right, xscale=:log10)"),
histogram3 = ("Histogram", "histogram(randn(1_000_000) .* .1, nbins=100, vertical=true)"),
boxplot1 = ("Boxplot", "boxplot([1, 3, 3, 4, 6, 10])"),
boxplot2 = ("Boxplot", """
boxplot(["one", "two"],
Expand All @@ -55,8 +56,8 @@ main() = begin
spy1 = ("Spy", "using SparseArrays\nspy(sprandn(50, 120, .05))"),
spy2 = ("Spy", "using SparseArrays\nspy(sprandn(50, 120, .9), show_zeros=true)"),
densityplot1 = ("Densityplot", """
plt = densityplot(randn(1000), randn(1000))
densityplot!(plt, randn(1000) .+ 2, randn(1000) .+ 2)
plt = densityplot(randn(1_000), randn(1_000))
densityplot!(plt, randn(1_000) .+ 2, randn(1_000) .+ 2)
"""),
contourplot1 = ("Contourplot", "contourplot(-3:.01:3, -7:.01:3, (x, y) -> exp(-(x / 2)^2 - ((y + 2) / 4)^2))"),
polarplot1 = ("Polarplot", "polarplot(range(0, 2π, length = 20), range(0, 2, length = 20))"),
Expand Down Expand Up @@ -268,7 +269,7 @@ Here is a list of the main high-level functions for common scenarios:
- [`lineplot`](https://github.com/JuliaPlots/UnicodePlots.jl#lineplot) (Line Plot)
- [`stairs`](https://github.com/JuliaPlots/UnicodePlots.jl#staircase-plot) (Staircase Plot)
- [`barplot`](https://github.com/JuliaPlots/UnicodePlots.jl#barplot) (Bar Plot - horizontal)
- [`histogram`](https://github.com/JuliaPlots/UnicodePlots.jl#histogram) (Histogram - horizontal)
- [`histogram`](https://github.com/JuliaPlots/UnicodePlots.jl#histogram) (Histogram - horizontal / vertical)
- [`boxplot`](https://github.com/JuliaPlots/UnicodePlots.jl#boxplot) (Box Plot - horizontal)
- [`spy`](https://github.com/JuliaPlots/UnicodePlots.jl#sparsity-pattern) (Sparsity Pattern)
- [`densityplot`](https://github.com/JuliaPlots/UnicodePlots.jl#density-plot) (Density Plot)
Expand Down Expand Up @@ -357,6 +358,9 @@ Here is a list of the main high-level functions for common scenarios:
The `histogram` function also supports axis scaling using the parameter `xscale`:

$(examples.histogram2)

Vertical histograms are supported:
$(examples.histogram3)
</details>

<details open>
Expand Down
2 changes: 2 additions & 0 deletions src/UnicodePlots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export GraphicsArea,
stairs,
stairs!,
histogram,
vertical_histogram,
horizontal_histogram,
densityplot,
densityplot!,
heatmap,
Expand Down
8 changes: 4 additions & 4 deletions src/canvas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ function annotate!(
halign = :center,
valign = :center,
)
xs = c.xscale(x)
ys = c.yscale(y)
origin_x(c) ≤ (xs = c.xscale(x)) ≤ origin_x(c) + width(c) || return c
origin_y(c) ≤ (ys = c.yscale(y)) ≤ origin_y(c) + height(c) || return c
pixel_x = (xs - origin_x(c)) / width(c) * pixel_width(c)
pixel_y = pixel_height(c) - (ys - origin_y(c)) / height(c) * pixel_height(c)

Expand All @@ -263,8 +263,8 @@ function annotate!(
end

function annotate!(c::Canvas, x::Number, y::Number, text::Char, color::UserColorType)
xs = c.xscale(x)
ys = c.yscale(y)
origin_x(c) ≤ (xs = c.xscale(x)) ≤ origin_x(c) + width(c) || return c
origin_y(c) ≤ (ys = c.yscale(y)) ≤ origin_y(c) + height(c) || return c
pixel_x = (xs - origin_x(c)) / width(c) * pixel_width(c)
pixel_y = pixel_height(c) - (ys - origin_y(c)) / height(c) * pixel_height(c)

Expand Down
8 changes: 4 additions & 4 deletions src/canvas/braillecanvas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ function BrailleCanvas(
end

function pixel_to_char_point(c::BrailleCanvas, pixel_x::Number, pixel_y::Number)
pixel_x = pixel_x < c.pixel_width ? pixel_x : pixel_x - 1
pixel_y = pixel_y < c.pixel_height ? pixel_y : pixel_y - 1
pixel_x < c.pixel_width || (pixel_x -= 1)
pixel_y < c.pixel_height || (pixel_y -= 1)
cw, ch = size(c.grid)
tmp = pixel_x / c.pixel_width * cw
char_x = floor(Int, tmp) + 1
Expand All @@ -90,8 +90,8 @@ function pixel_to_char_point(c::BrailleCanvas, pixel_x::Number, pixel_y::Number)
end

function pixel!(c::BrailleCanvas, pixel_x::Int, pixel_y::Int, color::UserColorType)
0 <= pixel_x <= c.pixel_width || return c
0 <= pixel_y <= c.pixel_height || return c
0 pixel_x c.pixel_width || return c
0 pixel_y c.pixel_height || return c
char_x, char_y, char_x_off, char_y_off = pixel_to_char_point(c, pixel_x, pixel_y)
if BLANK_BRAILLE <= (val = UInt64(c.grid[char_x, char_y])) <= FULL_BRAILLE
c.grid[char_x, char_y] = Char(val | UInt64(braille_signs[char_x_off, char_y_off]))
Expand Down
4 changes: 2 additions & 2 deletions src/graphics/bargraphics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function addrow!(
end

function printrow(io::IO, print_nc, print_col, c::BarplotGraphics, row::Int)
0 < row <= nrows(c) || throw(ArgumentError("Argument \"row\" out of bounds: $row"))
0 < row nrows(c) || throw(ArgumentError("Argument \"row\" out of bounds: $row"))
bar = c.bars[row]
max_val = c.maximum === nothing ? c.max_val : max(c.max_val, c.maximum)
max_bar_width = max(c.char_width - 2 - c.max_len, 1)
Expand All @@ -106,7 +106,7 @@ function printrow(io::IO, print_nc, print_col, c::BarplotGraphics, row::Int)
bar_head += 1 # padding, we printed one more char
end
bar_lbl = string(bar)
if bar >= 0
if bar 0
print_col(io, :normal, " ", bar_lbl)
len = length(bar_lbl)
else
Expand Down
Loading