Skip to content

Inconsistent x range behaviour with dates #64

@archiewood

Description

@archiewood

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions