Skip to content

Commit

Permalink
Add option to (not) print the origin. (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
tammoippen authored Jul 17, 2019
1 parent aada773 commit 83f4319
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 7 deletions.
41 changes: 39 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,25 @@ There are some utility functions for fast graphing of single plots.
#### Plot:
```python
In [4]: plotille.plot?
Signature: plotille.plot(X, Y, width=80, height=40, X_label='X', Y_label='Y', linesep='\n', interp='linear', x_min=None, x_max=None, y_min=None, y_max=None, lc=None, bg=None, color_mode='names')
Signature:
plt.plot(
X,
Y,
width=80,
height=40,
X_label='X',
Y_label='Y',
linesep='\n',
interp='linear',
x_min=None,
x_max=None,
y_min=None,
y_max=None,
lc=None,
bg=None,
color_mode='names',
origin=True,
)
Docstring:
Create plot with X , Y values and linear interpolation between points

Expand All @@ -119,6 +137,7 @@ Parameters:
bg: multiple Give the background color.
color_mode: str Specify color input mode; 'names' (default), 'byte' or 'rgb'
see plotille.color.__docs__
origin: bool Whether to print the origin. default: True

Returns:
str: plot over `X`, `Y`.
Expand All @@ -130,7 +149,24 @@ In [5]: print(plotille.plot(X, np.sin(X), height=30, width=60))
#### Scatter:
```python
In [6]: plotille.scatter?
Signature: plotille.scatter(X, Y, width=80, height=40, X_label='X', Y_label='Y', linesep='\n', x_min=None, x_max=None, y_min=None, y_max=None, lc=None, bg=None, color_mode='names')
Signature:
plt.scatter(
X,
Y,
width=80,
height=40,
X_label='X',
Y_label='Y',
linesep='\n',
x_min=None,
x_max=None,
y_min=None,
y_max=None,
lc=None,
bg=None,
color_mode='names',
origin=True,
)
Docstring:
Create scatter plot with X , Y values

Expand All @@ -151,6 +187,7 @@ Parameters:
bg: multiple Give the background color.
color_mode: str Specify color input mode; 'names' (default), 'byte' or 'rgb'
see plotille.color.__docs__
origin: bool Whether to print the origin. default: True

Returns:
str: scatter plot over `X`, `Y`.
Expand Down
13 changes: 12 additions & 1 deletion plotille/_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def __init__(self):
self._y_max = None
self._color_mode = None
self._with_colors = True
self._origin = True
self.linesep = os.linesep
self.background = None
self.x_label = 'X'
Expand Down Expand Up @@ -132,6 +133,16 @@ def with_colors(self, value):
raise ValueError('Only bool allowed: "{}"'.format(value))
self._with_colors = value

@property
def origin(self):
return self._origin

@origin.setter
def origin(self, value):
if not isinstance(value, bool):
raise ValueError('Invalid origin: {}'.format(value))
self._origin = value

def register_label_formatter(self, type_, formatter):
self._in_fmt.register_formatter(type_, formatter)

Expand Down Expand Up @@ -262,7 +273,7 @@ def show(self, legend=False):
if isinstance(p, Plot):
plot_origin = True

if plot_origin:
if self.origin and plot_origin:
# print X / Y origin axis
canvas.line(self._in_fmt.convert(xmin), 0, self._in_fmt.convert(xmax), 0)
canvas.line(0, self._in_fmt.convert(ymin), 0, self._in_fmt.convert(ymax))
Expand Down
9 changes: 6 additions & 3 deletions plotille/_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def histogram(X, bins=160, width=80, height=40, X_label='X', Y_label='Counts', l

def scatter(X, Y, width=80, height=40, X_label='X', Y_label='Y', linesep=os.linesep, # noqa: N803
x_min=None, x_max=None, y_min=None, y_max=None,
lc=None, bg=None, color_mode='names'):
lc=None, bg=None, color_mode='names', origin=True):
"""Create scatter plot with X , Y values
Basically plotting without interpolation:
Expand All @@ -150,17 +150,18 @@ def scatter(X, Y, width=80, height=40, X_label='X', Y_label='Y', linesep=os.line
bg: multiple Give the background color.
color_mode: str Specify color input mode; 'names' (default), 'byte' or 'rgb'
see plotille.color.__docs__
origin: bool Whether to print the origin. default: True
Returns:
str: scatter plot over `X`, `Y`.
"""
return plot(X, Y, width, height, X_label, Y_label, linesep, None,
x_min, x_max, y_min, y_max, lc, bg, color_mode)
x_min, x_max, y_min, y_max, lc, bg, color_mode, origin)


def plot(X, Y, width=80, height=40, X_label='X', Y_label='Y', linesep=os.linesep, interp='linear', # noqa: N803
x_min=None, x_max=None, y_min=None, y_max=None,
lc=None, bg=None, color_mode='names'):
lc=None, bg=None, color_mode='names', origin=True):
"""Create plot with X , Y values and linear interpolation between points
Parameters:
Expand All @@ -178,6 +179,7 @@ def plot(X, Y, width=80, height=40, X_label='X', Y_label='Y', linesep=os.linesep
bg: multiple Give the background color.
color_mode: str Specify color input mode; 'names' (default), 'byte' or 'rgb'
see plotille.color.__docs__
origin: bool Whether to print the origin. default: True
Returns:
str: plot over `X`, `Y`.
Expand All @@ -188,6 +190,7 @@ def plot(X, Y, width=80, height=40, X_label='X', Y_label='Y', linesep=os.linesep
fig.x_label = X_label
fig.y_label = Y_label
fig.linesep = linesep
fig.origin = origin
if x_min is not None:
fig.set_x_limits(min_=x_min)
if x_max is not None:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]

name = "plotille"
version = "3.6"
version = "3.7"
description = "Plot in the terminal using braille dots."
authors = ["Tammo Ippen <tammo.ippen@posteo.de>"]
license = "MIT"
Expand Down
13 changes: 13 additions & 0 deletions tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ def test_with_colors():
fig.with_colors = 1


def test_origin():
fig = Figure()

assert fig.origin

fig.origin = False

assert not fig.origin

with pytest.raises(ValueError):
fig.origin = 1


def limits(get_limits, set_limits):
fig = Figure()
assert get_limits(fig) == (0, 1)
Expand Down

0 comments on commit 83f4319

Please sign in to comment.