From fa6959553cd5d67ef222f9c725e49279e3575e7d Mon Sep 17 00:00:00 2001 From: rocky Date: Thu, 12 Jan 2023 23:54:09 -0500 Subject: [PATCH] Add rudimentary NumberLinePlot --- CHANGES.rst | 1 + SYMBOLS_MANIFEST.txt | 1 + mathics/builtin/drawing/plot.py | 61 ++++++++++++++++++++++++++++++--- 3 files changed, 58 insertions(+), 5 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 60a468883..8973ccc8b 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -34,6 +34,7 @@ New Builtins #. ``Kurtosis`` #. ``ListLogPlot`` #. ``LogPlot`` +#. ``NumberLinePlot`` #. ``PauliMatrix`` #. ``Remove`` #. ``SetOptions`` diff --git a/SYMBOLS_MANIFEST.txt b/SYMBOLS_MANIFEST.txt index ef4f558e4..eb74417e7 100644 --- a/SYMBOLS_MANIFEST.txt +++ b/SYMBOLS_MANIFEST.txt @@ -690,6 +690,7 @@ System`NumberForm System`NumberQ System`NumberString System`Numerator +System`NumberLinePlot System`NumericFunction System`NumericQ System`O diff --git a/mathics/builtin/drawing/plot.py b/mathics/builtin/drawing/plot.py index 6a2ef9aa3..b245e4edd 100644 --- a/mathics/builtin/drawing/plot.py +++ b/mathics/builtin/drawing/plot.py @@ -62,6 +62,8 @@ SymbolRectangle = Symbol("Rectangle") SymbolText = Symbol("Text") +TwoTenths = Real(0.2) +MTwoTenths = -TwoTenths # PlotRange Option def check_plot_range(range, range_type) -> bool: @@ -1184,7 +1186,7 @@ def axes(): yield Expression(SymbolFaceForm, Symbol("Black")) def points(x): - return ListExpression(vector2(x, 0), vector2(x, Real(-0.2))) + return ListExpression(vector2(x, 0), vector2(x, MTwoTenths)) for (k, n), x0, x1, y in boxes(): if k == 1: @@ -1199,7 +1201,7 @@ def labels(names): if k <= len(names): name = names[k - 1] yield Expression( - SymbolText, name, vector2((x0 + x1) / 2, Real(-0.2)) + SymbolText, name, vector2((x0 + x1) / 2, MTwoTenths) ) x_coords = list(itertools.chain(*[[x0, x1] for (k, n), x0, x1, y in boxes()])) @@ -1987,7 +1989,9 @@ class ListPlot(_ListPlot): class ListLinePlot(_ListPlot): """ - :WMA link: https://reference.wolfram.com/language/ref/ListLinePlot.html + + :WMA link: + https://reference.wolfram.com/language/ref/ListLinePlot.html
'ListLinePlot[{$y_1$, $y_2$, ...}]'
plots a line through a list of $y$-values, assuming integer $x$-values 1, 2, 3, ... @@ -2092,11 +2096,58 @@ class LogPlot(_Plot): """ - summary_text = "plots on a log scale curves of one or more functions" + summary_text = "plot on a log scale curves of one or more functions" use_log_scale = True +class NumberLinePlot(_ListPlot): + """ + :WMA link: + https://reference.wolfram.com/language/ref/NumberLinePlot.html +
+
'NumberPlot[{$v_1$, $v_2$, ...}]' +
plots a list of values along a line. +
+ + >> NumberLinePlot[Table[Prime[x], {x, 10}]] + = -Graphics- + """ + + options = Graphics.options.copy() + + # This is ListPlot with some tweaks: + # * remove the Y axis in display, + # * set the Y value to a constant, and + # * set the aspect ratio to reduce the distance above the + # x-axis + options.update( + { + "Axes": "{True, False}", + "AspectRatio": "1 / 10", + "Mesh": "None", + "PlotRange": "Automatic", + "PlotPoints": "None", + "Filling": "None", + "Joined": "False", + } + ) + summary_text = "plot along a number line" + + use_log_scale = False + + def eval(self, values, evaluation: Evaluation, options: dict): + "%(name)s[values_, OptionsPattern[%(name)s]]" + + # Fill in a Y value, and use the generic _ListPlot.eval(). + # Some graphics options have been adjusted above. + points_list = [ + ListExpression(eval_N(value, evaluation), Integer1) + for value in values.elements + ] + return _ListPlot.eval(self, ListExpression(*points_list), evaluation, options) + + class PieChart(_Chart): """ :Pie Chart: https://en.wikipedia.org/wiki/Pie_chart \ @@ -2194,7 +2245,7 @@ def _draw(self, data, color, evaluation, options: dict): sector_spacing = self.get_option(options, "SectorSpacing", evaluation) if isinstance(sector_spacing, Symbol): if sector_spacing.get_name() == "System`Automatic": - sector_spacing = ListExpression(Integer0, Real(0.2)) + sector_spacing = ListExpression(Integer0, TwoTenths) elif sector_spacing.get_name() == "System`None": sector_spacing = ListExpression(Integer0, Integer0) else: