Created
February 10, 2019 15:15
-
-
Save devforfu/96a7897d8ec0cb52df8d10a8a784df52 to your computer and use it in GitHub Desktop.
CLI with Docopt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Scatter plots generator. | |
Usage: | |
scatter <filename> <X> <Y> (<X> <Y>)... | |
""" | |
from itertools import chain | |
import docopt | |
import matplotlib.pyplot as plt | |
from matplotlib import rcParams | |
default_style = { | |
'font.family': 'monospace', | |
'font.size': 18, | |
'figure.figsize': (8, 6) | |
} | |
class Plotter: | |
def __init__(self, style: dict=None): | |
self.style = style or default_style | |
plt.rcParams.update(self.style) | |
def scatter(self, name: str, *points): | |
n = len(points) | |
if n % 2 != 0: | |
n -= 1 | |
points = points[:n] | |
xs, ys = points[::2], points[1::2] | |
f, ax = plt.subplots(1, 1) | |
ax.scatter(xs, ys) | |
self.save(f, name) | |
def save(self, figure: plt.Figure, output: str): | |
figure.savefig(f'{output}.png', format='png') | |
if __name__ == '__main__': | |
args = docopt.docopt(__doc__) | |
pairs = [(int(x), int(y)) for x, y in zip(args['<X>'], args['<Y>'])] | |
points = list(chain.from_iterable(pairs)) | |
filename = args['<filename>'] | |
plotter = Plotter() | |
plotter.scatter(filename, *points) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment