red-data-tools / unicode_plot.rb

Plot your data by Unicode characters
MIT License
246 stars 12 forks source link

Behavior when passing an array of strings to xlim #55

Open kojix2 opened 3 years ago

kojix2 commented 3 years ago

Hello.

This is not a bug report, but rather just a neutral report. We should not give xlim an array of strings.

But, when xlim is an array of strings, Unicode Plot shows a somewhat unexpected behavior.

Step to reproduce

require 'unicode_plot'

x = [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 3.0, 1.0, 3.0, 1.0, 0.0, 0.0, 0.0, 2.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 4.0, 7.0, 12.0, 6.0, 7.0, 7.0, 10.0, 9.0, 11.0, 27.0, 12.0, 12.0, 8.0, 22.0, 24.0, 20.0, 9.0, 15.0, 14.0, 16.0, 33.0, 31.0, 59.0, 47.0, 33.0, 26.0, 54.0, 52.0, 55.0, 40.0, 62.0, 33.0, 15.0, 44.0, 39.0, 36.0, 53.0, 34.0, 42.0, 38.0, 65.0, 93.0, 96.0, 104.0, 194.0, 185.0, 74.0, 218.0, 224.0, 253.0, 337.0, 370.0, 386.0, 270.0, 377.0, 550.0, 572.0, 708.0, 676.0, 571.0, 333.0, 511.0, 511.0, 596.0, 575.0, 590.0, 372.0, 365.0, 374.0, 450.0, 445.0, 451.0, 374.0, 221.0, 189.0, 284.0, 227.0, 199.0, 266.0, 302.0, 204.0, 177.0, 120.0, 106.0, 96.0, 89.0, 108.0, 68.0, 50.0, 80.0, 54.0, 100.0, 52.0, 57.0, 30.0, 30.0, 31.0, 38.0, 43.0, 31.0, 29.0, 40.0, 20.0, 27.0, 37.0, 61.0, 64.0, 46.0, 32.0, 36.0, 51.0, 26.0, 45.0, 41.0, 45.0, 32.0, 22.0, 30.0, 36.0, 40.0, 57.0, 43.0, 62.0, 60.0, 42.0, 43.0, 68.0, 54.0, 65.0, 49.0, 40.0, 53.0, 89.0, 79.0, 99.0, 88.0, 111.0, 110.0, 132.0, 125.0, 194.0, 249.0, 268.0, 195.0, 172.0, 208.0, 203.0, 352.0, 420.0, 373.0, 391.0, 248.0, 327.0, 440.0, 619.0, 588.0, 655.0, 501.0, 407.0, 618.0, 792.0, 966.0, 766.0, 798.0, 830.0, 581.0, 968.0, 1242.0, 1297.0, 1574.0, 1535.0, 1324.0, 937.0, 1234.0, 1350.0, 1479.0, 1595.0, 1523.0, 1486.0, 836.0, 693.0, 969.0, 1176.0, 1356.0, 1234.0, 1017.0, 630.0, 904.0, 1080.0, 1182.0, 1036.0, 985.0, 739.0, 491.0, 712.0, 893.0, 867.0, 870.0, 844.0, 598.0, 425.0, 624.0, 589.0, 656.0, 583.0, 598.0, 447.0, 288.0, 514.0, 507.0, 709.0, 639.0, 641.0, 439.0, 265.0, 531.0, 543.0, 480.0, 569.0, 597.0, 469.0, 307.0, 322.0, 216.0, 477.0, 570.0, 638.0, 478.0, 294.0, 531.0, 570.0, 623.0, 537.0, 564.0, 395.0, 271.0, 497.0, 505.0, 622.0, 594.0, 669.0, 432.0, 273.0, 494.0, 549.0, 703.0, 635.0, 579.0, 457.0, 315.0, 477.0, 616.0, 610.0, 745.0, 718.0, 488.0, 401.0, 649.0, 724.0, 804.0, 769.0, 868.0, 606.0, 482.0, 868.0, 607.0, 1049.0, 1137.0, 1302.0, 938.0, 772.0, 1278.0, 1535.0, 1623.0, 1704.0, 1723.0, 1423.0, 948.0, 1686.0, 2179.0, 2383.0, 2418.0, 2508.0, 2150.0, 1513.0, 1217.0, 1930.0, 2499.0, 2510.0, 2674.0, 2041.0, 1429.0, 2019.0, 2419.0, 2507.0, 2425.0, 2497.0, 1999.0, 1502.0, 2148.0, 2802.0, 2948.0, 2781.0, 3012.0, 2373.0, 1662.0, 2402.0, 2987.0, 3208.0, 2833.0, 2972.0, 2391.0, 1776.0, 2665.0, 3258.0, 3714.0, 3813.0, 3694.0, 2939.0, 2383.0, 3595.0, 3845.0, 4322.0, 3106.0, 3045.0, 3127.0, 3302.0, 4885.0, 5946.0, 7537.0, 7844.0, 7278.0, 6090.0, 4851.0, 4521.0, 5841.0, 6598.0, 6741.0, 6987.0, 5751.0, 4917.0, 5300.0, 5532.0, 5662.0, 4803.0, 4696.0, 3987.0, 2761.0, 3849.0, 3965.0, 4123.0, 3536.0, 3332.0, 2672.0, 1783.0, 2329.0, 2639.0, 2573.0, 2371.0, 2274.0, 1628.0, 1215.0, 1569.0, 1885.0, 1691.0, 1297.0, 1356.0, 1362.0, 962.0, 1304.0, 1444.0, 1541.0, 1281.0, 1229.0, 1032.0, 739.0, 1083.0, 926.0, 1075.0, 1059.0, 1208.0, 994.0, 685.0, 887.0, 1241.0, 1168.0, 1146.0, 1048.0, 1062.0, 599.0, 1126.0, 1312.0, 1316.0, 1268.0, 1316.0, 984.0, 688.0, 1130.0, 1522.0, 1492.0, 1453.0, 1516.0, 1110.0, 812.0, 1487.0, 1924.0, 1910.0, 2013.0, 2025.0, 1753.0, 1300.0, 2077.0, 2831.0, 2595.0, 2741.0, 2753.0, 2451.0, 1554.0, 2635.0, 3456.0, 3405.0, 3467.0, 3630.0, 2758.0, 2094.0, 3444.0, 4302.0, 4570.0, 4519.0, 4722.0, 4032.0, 2893.0, 4328.0, 5280.0, 5452.0, 5094.0, 5433.0, 4434.0, 3303.0, 4958.0, 5788.0, 5727.0, 4658.0, 5813.0, 5589.0, 4458.0, 4072.0, 3884.0, 4315.0, 6032.0, 6985.0, 6243.0, 4925.0, 6238.0, 7057.0, 6867.0, 6269.0, 6420.0, 5247.0, 3677.0, 5229.0, 5811.0, 5711.0, 5251.0, 5034.0, 4035.0, 2711.0, 3898.0, 4526.0, 4131.0, 3706.0, 3591.0, 2876.0, 1786.0, 2640.0, 3030.0, 2825.0, 2586.0, 2648.0, 2017.0, 1276.0, 1884.0]

UnicodePlot.lineplot(x, xlim: ["50", "400"]).render

image

Expected behavior

image

Why is this occurring?

This occurs in the extend_limits method in utils.rb. https://github.com/red-data-tools/unicode_plot.rb/blob/master/lib/unicode_plot/utils.rb

limits = ["50", 400"]
# p limits.minmax # "400", "50"

mi, ma = limits.minmax.map(&:to_f) 

If you change limits.minmax.map(&:to_f) to limits.map(&:to_f).minmax, you will get expected result. limits is usually a very small array. and will not affect performance. However, I may be missing some other disadvantages. Feel free to close this.

Thanks.

mrkn commented 2 years ago

Why you want to pass string values to xlim parameter?