From 33e973499182e7f10fcde004fce23c65a6a0b3ba Mon Sep 17 00:00:00 2001 From: simon987 Date: Wed, 23 Jun 2021 19:37:22 -0400 Subject: [PATCH] Add plot_freq_bar --- .gitignore | 3 ++- hexlib/plot.py | 30 ++++++++++++++++++++++++++++++ setup.py | 5 +++-- 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 hexlib/plot.py diff --git a/.gitignore b/.gitignore index 67d5de1..431af02 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.iml .idea/ -*.db \ No newline at end of file +*.db +*.png \ No newline at end of file diff --git a/hexlib/plot.py b/hexlib/plot.py new file mode 100644 index 0000000..06d22f7 --- /dev/null +++ b/hexlib/plot.py @@ -0,0 +1,30 @@ +import numpy as np +import matplotlib.pyplot as plt + +DATA = [ + *["apple"] * 5, + *["banana"] * 12, + *["strawberry"] * 8, + *["pineapple"] * 2, +] + + +def plot_freq_bar(items, ylabel="frequency", title=""): + item_set, item_counts = np.unique(items, return_counts=True) + + plt.bar(item_set, item_counts) + plt.xticks(rotation=35) + plt.ylabel(ylabel) + plt.title(title) + + for i, cnt in enumerate(item_counts): + plt.text(x=i, y=cnt / 2, s=cnt, ha="center", color="white") + + plt.tight_layout() + + +if __name__ == '__main__': + plot_freq_bar(DATA, title="My title") + plt.show() + + diff --git a/setup.py b/setup.py index 620a988..c8d37fc 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup setup( name="hexlib", - version="1.42", + version="1.43", description="Misc utility methods", author="simon987", author_email="me@simon987.net", @@ -13,6 +13,7 @@ setup( ]}, install_requires=[ "ImageHash", "influxdb", "siphash", "python-dateutil", "redis", "orjson", "zstandard", - "u-msgpack-python", "psycopg2-binary", "fake-useragent", "bs4", "lxml", "nltk" + "u-msgpack-python", "psycopg2-binary", "fake-useragent", "bs4", "lxml", "nltk", "numpy", + "matplotlib" ] )