Skip to content
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

Inconsistent x range behaviour with dates #64

Open
archiewood opened this issue Aug 12, 2024 · 1 comment
Open

Inconsistent x range behaviour with dates #64

archiewood opened this issue Aug 12, 2024 · 1 comment

Comments

@archiewood
Copy link

archiewood commented Aug 12, 2024

Issue

When I plot three different charts with identical y data, but transpose the x range, the x labels are incorrect, and the displayed ranges differ.

To reproduce

require 'unicode_plot'

def plot_sine_for_year(year)
  start_date = Date.new(year, 1, 1)
  x = (0..364).map { |i| start_date + i }
  y_sin = x.map { |xi| Math.sin((xi - start_date).to_f / 30 * 2 * Math::PI) }
  plot = UnicodePlot.lineplot(x, y_sin, name: "sin(x)", width: 40, height: 5, title: year.to_s)
  plot.render
end

[2020, 2021, 2022].each { |year| plot_sine_for_year(year) }

Expected behaviour

The x ranges start and finish at the same point relative to the first datapoint

OR

The x labels are the actual labels for the starting point of the axis, rather than the beginning and end of the range

Actual behaviour

The scales are all different

  • There is more padding to the left of the 2021 example than the 2020 example.
  • The 2021 example is much more "compressed" than 2020
  • However, the start and end of the xrange (labels) imply that they are the same scale, spanning the full set
                        2020
      ┌────────────────────────────────────────┐       
    1 │⠀⠀⢀⢿⠀⢀⢿⠀⢀⢿⠀⢀⢿⠀⢀⢿⠀⢀⢿⠀⢀⢿⠀⢀⢿⠀⢀⢿⠀⢀⢿⠀⢀⢿⠀⢀⢿⠀⢀⠀│ sin(x)
      │⠀⠀⢸⠸⡀⢸⠸⡀⢸⠸⡀⢸⠸⡀⢸⠸⡀⢸⠸⡀⢸⠸⡀⢸⠸⡀⢸⠸⡀⢸⠸⡀⢸⠸⡀⢸⠸⡀⢸⠀│       
      │⠤⠤⠼⠤⡧⢼⠤⡧⢼⠤⡧⢼⠤⡧⢼⠤⡧⢼⠤⡧⢼⠤⡧⢼⠤⡧⢼⠤⡧⢼⠤⡧⢼⠤⡧⢼⠤⡧⢼⠤│       
      │⠀⠀⠀⠀⡇⡎⠀⡇⡎⠀⡇⡎⠀⡇⡎⠀⡇⡎⠀⡇⡎⠀⡇⡎⠀⡇⡎⠀⡇⡎⠀⡇⡎⠀⡇⡎⠀⡇⡎⠀│       
   -1 │⠀⠀⠀⠀⢱⡇⠀⢱⡇⠀⢱⡇⠀⢱⡇⠀⢱⡇⠀⢱⡇⠀⢱⡇⠀⢱⡇⠀⢱⡇⠀⢱⡇⠀⢱⡇⠀⢱⡇⠀│       
      └────────────────────────────────────────┘       
      2020-01-01                      2020-12-30
                        2021
      ┌────────────────────────────────────────┐       
    1 │⠀⠀⠀⠀⠀⠀⠀⢸⡇⠀⣷⠀⡜⡄⢰⡇⠀⣿⠀⢸⡇⠀⣷⠀⡜⡄⢰⡇⠀⣿⠀⢸⡇⠀⣷⠀⡀⠀⠀⠀│ sin(x)
      │⠀⠀⠀⠀⠀⠀⠀⡜⡇⢸⢹⠀⡇⡇⢸⢸⠀⡟⡆⡜⡇⢸⢹⠀⡇⡇⢸⢸⠀⡟⡆⡜⡇⢸⢹⠀⡇⠀⠀⠀│       
      │⠤⠤⠤⠤⠤⠤⠤⠧⢼⢼⠼⡤⡧⡧⢼⢼⢴⠥⡧⡧⢼⢼⢼⠤⡧⡧⢼⢼⢴⠥⡧⡧⢼⢼⠼⡤⡧⠤⠤⠤│       
      │⠀⠀⠀⠀⠀⠀⠀⠀⢸⢸⠀⣧⠇⢣⡇⢸⣸⠀⡇⡇⢸⢸⠀⣧⠇⢣⡇⢸⣸⠀⡇⡇⢸⢸⠀⣧⠇⠀⠀⠀│       
   -1 │⠀⠀⠀⠀⠀⠀⠀⠀⠸⡇⠀⣿⠀⢸⡇⠀⡿⠀⢣⠃⠸⡇⠀⣿⠀⢸⡇⠀⡿⠀⢣⠃⠸⡇⠀⣿⠀⠀⠀⠀│       
      └────────────────────────────────────────┘       
      2021-01-01                      2021-12-31
                        2022
      ┌────────────────────────────────────────┐       
    1 │⠀⠀⠀⠀⢰⡇⠀⣿⠀⢸⡇⠀⣷⠀⡜⡄⢰⡇⠀⣿⠀⢸⡇⠀⣷⠀⡜⡄⢰⡇⠀⣿⠀⢀⠀⠀⠀⠀⠀⠀│ sin(x)
      │⠀⠀⠀⠀⢸⢸⠀⡟⡆⡜⡇⢸⢹⠀⡇⡇⢸⢸⠀⡟⡆⡜⡇⢸⢹⠀⡇⡇⢸⢸⠀⡟⡆⡜⠀⠀⠀⠀⠀⠀│       
      │⠤⠤⠤⠤⠼⢼⢴⠥⡧⡧⢼⢼⠼⡤⡧⡧⢼⢼⢴⠥⡧⡧⢼⢼⢼⠤⡧⡧⢼⢼⢴⠥⡧⡧⠤⠤⠤⠤⠤⠤│       
      │⠀⠀⠀⠀⠀⢸⣸⠀⡇⡇⢸⢸⠀⣧⠇⢣⡇⢸⣸⠀⡇⡇⢸⢸⠀⣧⠇⢣⡇⢸⣸⠀⡇⡇⠀⠀⠀⠀⠀⠀│       
   -1 │⠀⠀⠀⠀⠀⠀⡿⠀⢣⠃⠸⡇⠀⣿⠀⢸⡇⠀⡿⠀⢣⠃⠸⡇⠀⣿⠀⢸⡇⠀⡿⠀⢣⠃⠀⠀⠀⠀⠀⠀│       
      └────────────────────────────────────────┘       
      2022-01-01                      2022-12-31

CleanShot 2024-08-12 at 19 38 57@2x

@archiewood
Copy link
Author

archiewood commented Aug 13, 2024

I believe the line causing the behaviour is here

plot.annotate!(:bl, xmin.to_s, color: :light_black)

Despite the plot function increasing range so the xmin and xmax are a nice "rounded" number that divides by 10 (not sensible behaviour for dates), the x labels being used are the ends of the date range the user supplies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant