Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2014 Ian Horn <[email protected]> and
Danilo J. S. Bellini <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
hipsterplot
===========

[![PyPI](https://img.shields.io/pypi/v/hipsterplot)](https://pypi.org/project/hipsterplot/)
[![PyPI - License](https://img.shields.io/pypi/l/hipsterplot)](https://opensource.org/licenses/MIT)

because matplotlib is too mainstream <sup><sup>(or you broke it like I did)</sup></sup>. Also good while debugging numerical code.

-----------------------------------
Expand All @@ -9,7 +12,7 @@ A python script for command line plotting.

`pip install hipsterplot` is all you need.

Can plot a list of y values [ys] which works for evenly spaced points, or pairs of lists ([xs], [ys]) which works for heterogeneous x-spacing, or disorder in the x's (scatterplots).
Can plot a list of y values \[ys\] which works for evenly spaced points, or pairs of lists (\[xs\], \[ys\]) which works for heterogeneous x-spacing, or disorder in the x's (scatterplots).

In a given 'pixel', the plotted character depends how many data points map to it, so it can look good on densities.

Expand Down
33 changes: 19 additions & 14 deletions hipsterplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# SOFTWARE.

from __future__ import print_function, division
import math, random, sys
import math, random, sys, os
from operator import itemgetter

# Python 2.x and 3.x compatibility
Expand All @@ -37,9 +37,9 @@
CHAR_LOOKUP_SYMBOLS = [(0, ' '), # Should be sorted
(1, '.'),
(2, ':'),
#(3, '!'),
(3, '!'),
(4, '|'),
#(8, '+'),
(8, '+'),
(float("inf"), '#')]

def charlookup(num_chars):
Expand Down Expand Up @@ -73,15 +73,7 @@ def enumerated_reversed(seq):
return zip(range(len(seq) - 1, -1, -1), reversed(seq))


def plot(y_vals, x_vals=None, num_x_chars=70, num_y_chars=15):
"""
Plots the values given by y_vals. The x_vals values are the y indexes, by
default, unless explicitly given. Pairs (x, y) are matched by the x_vals
and y_vals indexes, so these must have the same length.

The num_x_chars and num_y_chars inputs are respectively the width and
height for the output plot to be printed, given in characters.
"""
def gen_plot(y_vals, x_vals=None, num_x_chars=70, num_y_chars=15):
y_vals = list(y_vals)
x_vals = list(x_vals) if x_vals else list(range(len(y_vals)))
if len(x_vals) != len(y_vals):
Expand All @@ -106,7 +98,20 @@ def plot(y_vals, x_vals=None, num_x_chars=70, num_y_chars=15):

for idx, row in enumerated_reversed(rows):
y_bin_mid = y_bin_ends[idx] - y_bin_width * 0.5
print("{:10.4f} {}".format(y_bin_mid, "".join(row)))
lines = "{:10.4f} {}".format(y_bin_mid, "".join(row))
return os.linesep.join([line.rstrip() for line in lines.split(os.linesep)])


def plot(y_vals, x_vals=None, num_x_chars=70, num_y_chars=15):
"""
Plots the values given by y_vals. The x_vals values are the y indexes, by
default, unless explicitly given. Pairs (x, y) are matched by the x_vals
and y_vals indexes, so these must have the same length.

The num_x_chars and num_y_chars inputs are respectively the width and
height for the output plot to be printed, given in characters.
"""
print(gen_plot(y_vals, x_vals, num_x_chars, num_y_chars))


if __name__ == '__main__':
Expand All @@ -123,4 +128,4 @@ def plot(y_vals, x_vals=None, num_x_chars=70, num_y_chars=15):
k = 20
ys = [random.gauss(0, 0.5) + math.cos(x/5.0/k) for x in range(180*k)]
num_x_chars = min(160, len(ys))
plot(ys, num_x_chars=num_x_chars, num_y_chars=25)
plot(ys, num_x_chars=num_x_chars, num_y_chars=25)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

metadata = {
"name": "hipsterplot",
"version": "0.1",
"version": "0.1.1",
"author": "Ian Horn and Danilo J. S. Bellini",
"author_email": "[email protected] ; danilo.bellini.gmail.com",
"url": "http://github.com/imh/hipsterplot",
Expand Down