Skip to content

Commit

Permalink
Legend ntuples (#33)
Browse files Browse the repository at this point in the history
* replace legend dict with list of namedtuples

* updated test to work with lst of LegendItem tuples
  • Loading branch information
CDonnerer committed Feb 14, 2021
1 parent d0885bb commit 82b19b1
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/shellplot/_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import numpy as np

from shellplot.drawing import LegendItem
from shellplot.utils import numpy_1d, numpy_2d


Expand Down Expand Up @@ -73,7 +74,7 @@ def _plot(fig, x, y, marker=True, line=None, label=None, **kwargs):

if label is not None:
key = marker or line
fig.legend.update({key: label})
fig.legend.append(LegendItem(symbol=key, name=label))


def _within_display(x, y):
Expand Down
7 changes: 5 additions & 2 deletions src/shellplot/drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
converts them to strings. Please note that drawing is entirely agnostic to the
type of plot.
"""
from collections import namedtuple
from typing import List

PALETTE = {
Expand All @@ -26,6 +27,8 @@
23: "┐",
}

LegendItem = namedtuple("LegendItem", ["symbol", "name"])


def draw(canvas, x_axis, y_axis, legend=None) -> str:
"""Draw figure from plot elements (i.e. canvas, x-axis, y-axis, legend)
Expand Down Expand Up @@ -132,8 +135,8 @@ def _draw_x_axis(x_axis, left_pad) -> List[str]:
def _draw_legend(legend) -> List[str]:
legend_lines = list()

for marker, name in legend.items():
legend_str = f"{PALETTE[marker]} {name}"
for item in legend:
legend_str = f"{PALETTE[item.symbol]} {item.name}"
legend_lines.append(legend_str)
return legend_lines

Expand Down
2 changes: 1 addition & 1 deletion src/shellplot/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def clear(self):

def _init_figure_elements(self):
self.canvas = np.zeros(shape=(self.figsize[0], self.figsize[1]), dtype=int)
self.legend = dict()
self.legend = list()
self.markers = cycle([1, 2, 3, 4, 5, 6])
self.lines = cycle([10, 11])

Expand Down
3 changes: 2 additions & 1 deletion tests/test_drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from shellplot.axis import Axis
from shellplot.drawing import (
LegendItem,
_draw_canvas,
_draw_legend,
_draw_x_axis,
Expand All @@ -15,7 +16,7 @@


def test_draw_legend():
legend = {1: "one", 2: "two"}
legend = [LegendItem(1, "one"), LegendItem(2, "two")]
legend_lines = ["+ one", "* two"]
assert legend_lines == _draw_legend(legend)

Expand Down

0 comments on commit 82b19b1

Please sign in to comment.