diff --git a/.gitignore b/.gitignore index f7e7e2f..08e1c27 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ NUtil.egg-info/SOURCES.txt NUtil.egg-info/top_level.txt nutil/__pycache__/__init__.cpython-36.pyc nutil/__pycache__/image.cpython-36.pyc +nutil/__pycache__/plot.cpython-36.pyc diff --git a/README.md b/README.md index 3ac4e34..2688cb6 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,46 @@ Usage: ![Browse through random stack](browse_example.gif) +## Plot features + +How to grab a figure to an RGB image numpy array: + + from nutil.plot import grabFigure + from imageio import mimwrite + + ims = [] + + for _ in range(10): + fig = plt.figure() + plt.plot(np.random.randn(100)) + ims.append(grabFigure(fig)) + + mimwrite("random_plot.gif", ims, fps=3) + +![Random plots saved as movie](random_plot.gif) + +Use this function to make your paper figures nice immediately (and editable in Illustrator & Co!). +Checkout the [example file](nice_figure.svg) in SVG file format. + + import seaborn as sns + from nutil.plot import paperStyle + + # Font-size 8 pt by default, + # text editable + # and seaborn white style with ticks + paperStyle() + + plt.figure(figsize=(3,2)) + plt.plot(np.random.randn(100)) + plt.xlabel("Time [au]") + plt.ylabel("Data [au]") + sns.despine(trim=True, offset=5) + plt.savefig("nice_figure.png") + plt.savefig("nice_figure.svg") + + +![Random plot as nice figure](nice_figure.png) + ## Fake data Moving square diff --git a/nice_figure.png b/nice_figure.png new file mode 100644 index 0000000..0749f1d Binary files /dev/null and b/nice_figure.png differ diff --git a/nice_figure.svg b/nice_figure.svg new file mode 100644 index 0000000..2ac5e75 --- /dev/null +++ b/nice_figure.svg @@ -0,0 +1,279 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + 0 + + + + + + + + + + 20 + + + + + + + + + + 40 + + + + + + + + + + 60 + + + + + + + + + + 80 + + + + + + + + + + 100 + + + + Time [au] + + + + + + + + + + + + + + −2 + + + + + + + + + + −1 + + + + + + + + + + 0 + + + + + + + + + + 1 + + + + + + + + + + 2 + + + + Data [au] + + + + + + + + + + + + + + + + + + + diff --git a/nutil/plot.py b/nutil/plot.py index fd79bbe..e6569e4 100644 --- a/nutil/plot.py +++ b/nutil/plot.py @@ -2,20 +2,22 @@ import matplotlib.pyplot as plt import seaborn as sns -def paperStyle(use_seaborn=True): +def paperStyle(font_size=8, use_seaborn=True): """Defines plot styles for paper Args: + font_size (int, optional): Figure font size. Defaults to 8 (pt). use_seaborn (bool, optional): If seaborn is used to style the plot. Defaults to True. """ if use_seaborn: sns.set_style('white') sns.set_style('ticks') - plt.rcParams['axes.labelsize'] = 8 - plt.rcParams['xtick.labelsize'] = 8 - plt.rcParams['ytick.labelsize'] = 8 - plt.rcParams['legend.fontsize'] = 8 + plt.rcParams['axes.labelsize'] = font_size + plt.rcParams['xtick.labelsize'] = font_size + plt.rcParams['ytick.labelsize'] = font_size + plt.rcParams['legend.fontsize'] = font_size + plt.rcParams['title.fontsize'] = font_size plt.rcParams['font.family'] = ['sans-serif'] plt.rcParams['font.sans-serif'] = ['Arial'] plt.rcParams['svg.fonttype'] = 'none' # Text is not rendered @@ -40,4 +42,4 @@ def grabFigure(fig, autoclose=True): plt.close(fig) # Returns numpy array - return np.frombuffer(rgb, dtype=np.uint8).reshape(shape) \ No newline at end of file + return np.frombuffer(rgb, dtype=np.uint8).reshape(shape) diff --git a/random_plot.gif b/random_plot.gif new file mode 100644 index 0000000..e2b73bf Binary files /dev/null and b/random_plot.gif differ