From 639446e3564469936bc8ec7325f0d9f69c30ad5c Mon Sep 17 00:00:00 2001 From: brecht Date: Thu, 28 Apr 2016 11:01:54 +0200 Subject: [PATCH 01/11] readme to rst --- README.md | 2 -- README.rst | 4 ++++ 2 files changed, 4 insertions(+), 2 deletions(-) delete mode 100644 README.md create mode 100644 README.rst diff --git a/README.md b/README.md deleted file mode 100644 index a52be6a..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# plotcolor -Some nice default color cycles for plotting diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..59979cd --- /dev/null +++ b/README.rst @@ -0,0 +1,4 @@ +plottools +========= + +A package with usefull tools for plotting with matplotlib From f15fb944682955a54cb344c3050fd084c08df055 Mon Sep 17 00:00:00 2001 From: brecht Date: Mon, 6 Jun 2016 13:53:43 +0200 Subject: [PATCH 02/11] updated publication rc parameters --- examples/zoom_axes.py | 6 +-- plottools/plottools.py | 112 +++++++++++++++++++++++------------------ 2 files changed, 66 insertions(+), 52 deletions(-) diff --git a/examples/zoom_axes.py b/examples/zoom_axes.py index 54fc0bb..1e32287 100644 --- a/examples/zoom_axes.py +++ b/examples/zoom_axes.py @@ -5,8 +5,8 @@ pt.set_publication_rc() -# large figure, box on the left -plt.figure(figsize=(12/2.54,8/2.54)) +# small figure, box on the left +plt.figure(figsize=(8/2.54,6/2.54)) plt.plot([0,2],[1,2]) plt.plot([0,2],[2,1]) plt.xlabel('xlabel') @@ -29,7 +29,7 @@ fig = plt.gcf() ax = plt.gca() -ax1 = pt.zoom_axes(fig,ax,[1.5,1.6],[1.7,1.8],[0.15,0.9],[1.4,2.0]) +ax1 = pt.zoom_axes(fig,ax,[1.5,1.6],[1.7,1.8],[0.15,0.9],[1.4,1.9]) plt.plot([0,2],[1,2]) plt.plot([0,2],[2,1]) diff --git a/plottools/plottools.py b/plottools/plottools.py index d64be15..29c1fa9 100644 --- a/plottools/plottools.py +++ b/plottools/plottools.py @@ -21,67 +21,81 @@ import numpy as np def set_publication_rc(): - plt.rc('text', usetex=True) - plt.rc('font', family='serif') plt.rc('figure', autolayout=True) - plt.rc('legend', fontsize=10) - + plt.rc('savefig',bbox='tight',pad_inches=0.01) + plt.rc('axes', linewidth=0.6) + plt.rc('axes.formatter', useoffset=False) + plt.rc('legend', fontsize=8, frameon=True) + plt.rc('lines', linewidth=0.8,markersize=4) + plt.rc('text', usetex=True) + plt.rc('font', family='serif', size=8) def zoom_axes(fig,ax,zoom_x,zoom_y,axes_x,axes_y,box=True,box_color='k',box_alpha=0.8,connect=True,connect_color='k',connect_alpha=0.3,spacing_zoom=2,spacing_axes=20): - """ - Creates a new axes which zooms in on a part of a given axes - - Arguments: - fig: matplotlib figure + """ + Creates a new axes which zooms in on a part of a given axes + + Arguments: + fig: matplotlib figure ax: matplotlib axes zoom_x: list, specifying the zooming horizontal area zoom_y: list, specifying the zooming vertical area axes_x: list, specifying the new axes horizontal location in data coordinates axes_y: list, specifying the new axes vertical location in data coordinates - + Returns: ax1: a new axes - + Example: - - """ - - plt.tight_layout() - ax1_p0 = (ax.transData + fig.transFigure.inverted()).transform_point((axes_x[0],axes_y[0])) - ax1_p1 = (ax.transData + fig.transFigure.inverted()).transform_point((axes_x[1],axes_y[1])) - ax1 = plt.axes([ax1_p0[0],ax1_p0[1],ax1_p1[0]-ax1_p0[0],ax1_p1[1]-ax1_p0[1]]) - - ax1.set_xlim(zoom_x) - ax1.set_ylim(zoom_y) - - plt.xticks(fontsize=6) - plt.yticks(fontsize=6) - - - if box: - ax.plot([zoom_x[0],zoom_x[1],zoom_x[1],zoom_x[0],zoom_x[0]],[zoom_y[0],zoom_y[0],zoom_y[1],zoom_y[1],zoom_y[0]],color=box_color,alpha=box_alpha) - - if connect: - # estimate the width of the ticks - spacing_zoom = (ax.transData.inverted()).transform_point((spacing_zoom,0))[0]-(ax.transData.inverted()).transform_point((0,0))[0] - spacing_axes = (ax.transData.inverted()).transform_point((spacing_axes,0))[0]-(ax.transData.inverted()).transform_point((0,0))[0] + """ + + plt.tight_layout() + ax1_p0 = (ax.transData + fig.transFigure.inverted()).transform_point((axes_x[0],axes_y[0])) + ax1_p1 = (ax.transData + fig.transFigure.inverted()).transform_point((axes_x[1],axes_y[1])) + + ax1 = plt.axes([ax1_p0[0],ax1_p0[1],ax1_p1[0]-ax1_p0[0],ax1_p1[1]-ax1_p0[1]]) + + ax1.set_xlim(zoom_x) + ax1.set_ylim(zoom_y) + + plt.xticks(fontsize=4) + plt.yticks(fontsize=4) + ax1.tick_params(axis='x', pad=3) + ax1.tick_params(axis='y', pad=2) + + if box: + ax.plot([zoom_x[0],zoom_x[1],zoom_x[1],zoom_x[0],zoom_x[0]],[zoom_y[0],zoom_y[0],zoom_y[1],zoom_y[1],zoom_y[0]],color=box_color,alpha=box_alpha,linewidth=0.4) - if zoom_x[1] < axes_x[0]: - # the zoom box is on the left - x_connect = [zoom_x[1] + spacing_zoom , axes_x[0] - spacing_axes] - y_connect_bot = np.interp(x_connect,[zoom_x[1],axes_x[0]],[zoom_y[0],axes_y[0]]) - y_connect_top = np.interp(x_connect,[zoom_x[1],axes_x[0]],[zoom_y[1],axes_y[1]]) + if connect: + # estimate the width of the ticks + spacing_zoom = (ax.transData.inverted()).transform_point((spacing_zoom,0))[0]-(ax.transData.inverted()).transform_point((0,0))[0] + spacing_axes = (ax.transData.inverted()).transform_point((spacing_axes,0))[0]-(ax.transData.inverted()).transform_point((0,0))[0] + + if zoom_x[1] < axes_x[0]: + # the zoom box is on the left + x_connect_bot = [zoom_x[1] + spacing_zoom , axes_x[0] - 0.3*spacing_axes] + x_connect_top = [zoom_x[1] + spacing_zoom , axes_x[0] - 0.3*spacing_axes] + y_connect_bot = np.interp(x_connect_bot,[zoom_x[1],axes_x[0]],[zoom_y[0],axes_y[0]]) + y_connect_top = np.interp(x_connect_top,[zoom_x[1],axes_x[0]],[zoom_y[1],axes_y[1]]) + + if y_connect_bot[1] > axes_y[0]: + x_connect_bot = [zoom_x[1] + spacing_zoom , axes_x[0] - spacing_axes] + y_connect_bot = np.interp(x_connect_bot,[zoom_x[1],axes_x[0]],[zoom_y[0],axes_y[0]]) + + if y_connect_top[1] < axes_y[1]: + x_connect_top = [zoom_x[1] + spacing_zoom , axes_x[0] - spacing_axes] + y_connect_top = np.interp(x_connect_top,[zoom_x[1],axes_x[0]],[zoom_y[1],axes_y[1]]) + + else: + # the zoom box is on the right + # the zoom box is on the left + x_connect_bot = [axes_x[1] + 0.3*spacing_axes , zoom_x[0] - spacing_zoom] + x_connect_top = [axes_x[1] + 0.3*spacing_axes , zoom_x[0] - spacing_zoom] + y_connect_bot = np.interp(x_connect_bot,[axes_x[1],zoom_x[0]],[axes_y[0],zoom_y[0]]) + y_connect_top = np.interp(x_connect_top,[axes_x[1],zoom_x[0]],[axes_y[1],zoom_y[1]]) + + ax.plot(x_connect_bot,y_connect_bot,color=connect_color,alpha=connect_alpha,linewidth=0.4) + ax.plot(x_connect_top,y_connect_top,color=connect_color,alpha=connect_alpha,linewidth=0.4) - else: - # the zoom box is on the right - # the zoom box is on the left - x_connect = [axes_x[1] + 0.5*spacing_axes , zoom_x[0] - spacing_zoom] - y_connect_bot = np.interp(x_connect,[axes_x[1],zoom_x[0]],[axes_y[0],zoom_y[0]]) - y_connect_top = np.interp(x_connect,[axes_x[1],zoom_x[0]],[axes_y[1],zoom_y[1]]) - - ax.plot(x_connect,y_connect_bot,color=connect_color,alpha=connect_alpha) - ax.plot(x_connect,y_connect_top,color=connect_color,alpha=connect_alpha) - - return ax1 + return ax1 From e902023701155d9a4fb95803039fc9e532af00f6 Mon Sep 17 00:00:00 2001 From: BrechtBa Date: Fri, 10 Jun 2016 10:45:56 +0200 Subject: [PATCH 03/11] change publication rc params --- plottools/plottools.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plottools/plottools.py b/plottools/plottools.py index 29c1fa9..bba30e0 100644 --- a/plottools/plottools.py +++ b/plottools/plottools.py @@ -23,12 +23,13 @@ def set_publication_rc(): plt.rc('figure', autolayout=True) plt.rc('savefig',bbox='tight',pad_inches=0.01) - plt.rc('axes', linewidth=0.6) + plt.rc('font', family='serif', size=6) + plt.rc('axes', linewidth=0.4,labelsize=8) plt.rc('axes.formatter', useoffset=False) plt.rc('legend', fontsize=8, frameon=True) plt.rc('lines', linewidth=0.8,markersize=4) plt.rc('text', usetex=True) - plt.rc('font', family='serif', size=8) + def zoom_axes(fig,ax,zoom_x,zoom_y,axes_x,axes_y,box=True,box_color='k',box_alpha=0.8,connect=True,connect_color='k',connect_alpha=0.3,spacing_zoom=2,spacing_axes=20): """ From 5f89fc3a12e64b25ef0d7ae0f53a1975d6be17cf Mon Sep 17 00:00:00 2001 From: BrechtBa Date: Fri, 10 Jun 2016 13:40:09 +0200 Subject: [PATCH 04/11] added more options to publication defaults --- plottools/plottools.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/plottools/plottools.py b/plottools/plottools.py index bba30e0..b556fca 100644 --- a/plottools/plottools.py +++ b/plottools/plottools.py @@ -21,14 +21,25 @@ import numpy as np def set_publication_rc(): + # figure plt.rc('figure', autolayout=True) - plt.rc('savefig',bbox='tight',pad_inches=0.01) - plt.rc('font', family='serif', size=6) - plt.rc('axes', linewidth=0.4,labelsize=8) + plt.rc('savefig', format='pdf', dpi=150, bbox='tight', pad_inches=0.01) + # font + plt.rc('font', family='serif', serif=['computer modern roman'], size=6) + # axes + plt.rc('axes', linewidth=0.4, labelsize=8) plt.rc('axes.formatter', useoffset=False) + # legend plt.rc('legend', fontsize=8, frameon=True) + # lines plt.rc('lines', linewidth=0.8,markersize=4) + # text plt.rc('text', usetex=True) + # ticks + plt.rc('xtick.major', size=3, width=0.3, pad=3) + plt.rc('ytick.major', size=3, width=0.3, pad=3) + plt.rc('xtick.minor', size=2, width=0.3, pad=3) + plt.rc('ytick.minor', size=2, width=0.3, pad=3) def zoom_axes(fig,ax,zoom_x,zoom_y,axes_x,axes_y,box=True,box_color='k',box_alpha=0.8,connect=True,connect_color='k',connect_alpha=0.3,spacing_zoom=2,spacing_axes=20): From 35766c5ca53b890ae4f4a56ff7274247dab6e851 Mon Sep 17 00:00:00 2001 From: BrechtBa Date: Mon, 13 Jun 2016 15:07:46 +0200 Subject: [PATCH 05/11] small changes --- plottools/plottools.py | 154 ++++++++++++++++++++--------------------- 1 file changed, 77 insertions(+), 77 deletions(-) diff --git a/plottools/plottools.py b/plottools/plottools.py index b556fca..3144c4c 100644 --- a/plottools/plottools.py +++ b/plottools/plottools.py @@ -21,93 +21,93 @@ import numpy as np def set_publication_rc(): - # figure - plt.rc('figure', autolayout=True) - plt.rc('savefig', format='pdf', dpi=150, bbox='tight', pad_inches=0.01) - # font - plt.rc('font', family='serif', serif=['computer modern roman'], size=6) - # axes - plt.rc('axes', linewidth=0.4, labelsize=8) - plt.rc('axes.formatter', useoffset=False) - # legend - plt.rc('legend', fontsize=8, frameon=True) - # lines - plt.rc('lines', linewidth=0.8,markersize=4) - # text - plt.rc('text', usetex=True) - # ticks - plt.rc('xtick.major', size=3, width=0.3, pad=3) - plt.rc('ytick.major', size=3, width=0.3, pad=3) - plt.rc('xtick.minor', size=2, width=0.3, pad=3) - plt.rc('ytick.minor', size=2, width=0.3, pad=3) - - + # figure + plt.rc('figure', autolayout=True) + plt.rc('savefig', format='pdf', dpi=150, bbox='tight', pad_inches=0.01) + # font + plt.rc('font', family='serif', serif=['computer modern roman'], size=6) + # axes + plt.rc('axes', linewidth=0.4, labelsize=8) + plt.rc('axes.formatter', useoffset=False) + # legend + plt.rc('legend', fontsize=8, frameon=True) + # lines + plt.rc('lines', linewidth=0.8,markersize=4) + # text + plt.rc('text', usetex=True) + # ticks + plt.rc('xtick.major', size=3, width=0.3, pad=3) + plt.rc('ytick.major', size=3, width=0.3, pad=3) + plt.rc('xtick.minor', size=2, width=0.3, pad=3) + plt.rc('ytick.minor', size=2, width=0.3, pad=3) + + def zoom_axes(fig,ax,zoom_x,zoom_y,axes_x,axes_y,box=True,box_color='k',box_alpha=0.8,connect=True,connect_color='k',connect_alpha=0.3,spacing_zoom=2,spacing_axes=20): - """ - Creates a new axes which zooms in on a part of a given axes + """ + Creates a new axes which zooms in on a part of a given axes - Arguments: - fig: matplotlib figure - ax: matplotlib axes - zoom_x: list, specifying the zooming horizontal area - zoom_y: list, specifying the zooming vertical area - axes_x: list, specifying the new axes horizontal location in data coordinates - axes_y: list, specifying the new axes vertical location in data coordinates + Arguments: + fig: matplotlib figure + ax: matplotlib axes + zoom_x: list, specifying the zooming horizontal area + zoom_y: list, specifying the zooming vertical area + axes_x: list, specifying the new axes horizontal location in data coordinates + axes_y: list, specifying the new axes vertical location in data coordinates - Returns: - ax1: a new axes + Returns: + ax1: a new axes - Example: + Example: - """ + """ - plt.tight_layout() - ax1_p0 = (ax.transData + fig.transFigure.inverted()).transform_point((axes_x[0],axes_y[0])) - ax1_p1 = (ax.transData + fig.transFigure.inverted()).transform_point((axes_x[1],axes_y[1])) + plt.tight_layout() + ax1_p0 = (ax.transData + fig.transFigure.inverted()).transform_point((axes_x[0],axes_y[0])) + ax1_p1 = (ax.transData + fig.transFigure.inverted()).transform_point((axes_x[1],axes_y[1])) - ax1 = plt.axes([ax1_p0[0],ax1_p0[1],ax1_p1[0]-ax1_p0[0],ax1_p1[1]-ax1_p0[1]]) + ax1 = plt.axes([ax1_p0[0],ax1_p0[1],ax1_p1[0]-ax1_p0[0],ax1_p1[1]-ax1_p0[1]]) - ax1.set_xlim(zoom_x) - ax1.set_ylim(zoom_y) + ax1.set_xlim(zoom_x) + ax1.set_ylim(zoom_y) - plt.xticks(fontsize=4) - plt.yticks(fontsize=4) - ax1.tick_params(axis='x', pad=3) - ax1.tick_params(axis='y', pad=2) + plt.xticks(fontsize=4) + plt.yticks(fontsize=4) + ax1.tick_params(axis='x', pad=3) + ax1.tick_params(axis='y', pad=2) - if box: - ax.plot([zoom_x[0],zoom_x[1],zoom_x[1],zoom_x[0],zoom_x[0]],[zoom_y[0],zoom_y[0],zoom_y[1],zoom_y[1],zoom_y[0]],color=box_color,alpha=box_alpha,linewidth=0.4) + if box: + ax.plot([zoom_x[0],zoom_x[1],zoom_x[1],zoom_x[0],zoom_x[0]],[zoom_y[0],zoom_y[0],zoom_y[1],zoom_y[1],zoom_y[0]],color=box_color,alpha=box_alpha,linewidth=0.4) - if connect: - # estimate the width of the ticks - spacing_zoom = (ax.transData.inverted()).transform_point((spacing_zoom,0))[0]-(ax.transData.inverted()).transform_point((0,0))[0] - spacing_axes = (ax.transData.inverted()).transform_point((spacing_axes,0))[0]-(ax.transData.inverted()).transform_point((0,0))[0] - - if zoom_x[1] < axes_x[0]: - # the zoom box is on the left - x_connect_bot = [zoom_x[1] + spacing_zoom , axes_x[0] - 0.3*spacing_axes] - x_connect_top = [zoom_x[1] + spacing_zoom , axes_x[0] - 0.3*spacing_axes] - y_connect_bot = np.interp(x_connect_bot,[zoom_x[1],axes_x[0]],[zoom_y[0],axes_y[0]]) - y_connect_top = np.interp(x_connect_top,[zoom_x[1],axes_x[0]],[zoom_y[1],axes_y[1]]) - - if y_connect_bot[1] > axes_y[0]: - x_connect_bot = [zoom_x[1] + spacing_zoom , axes_x[0] - spacing_axes] - y_connect_bot = np.interp(x_connect_bot,[zoom_x[1],axes_x[0]],[zoom_y[0],axes_y[0]]) - - if y_connect_top[1] < axes_y[1]: - x_connect_top = [zoom_x[1] + spacing_zoom , axes_x[0] - spacing_axes] - y_connect_top = np.interp(x_connect_top,[zoom_x[1],axes_x[0]],[zoom_y[1],axes_y[1]]) - - else: - # the zoom box is on the right - # the zoom box is on the left - x_connect_bot = [axes_x[1] + 0.3*spacing_axes , zoom_x[0] - spacing_zoom] - x_connect_top = [axes_x[1] + 0.3*spacing_axes , zoom_x[0] - spacing_zoom] - y_connect_bot = np.interp(x_connect_bot,[axes_x[1],zoom_x[0]],[axes_y[0],zoom_y[0]]) - y_connect_top = np.interp(x_connect_top,[axes_x[1],zoom_x[0]],[axes_y[1],zoom_y[1]]) - - ax.plot(x_connect_bot,y_connect_bot,color=connect_color,alpha=connect_alpha,linewidth=0.4) - ax.plot(x_connect_top,y_connect_top,color=connect_color,alpha=connect_alpha,linewidth=0.4) + if connect: + # estimate the width of the ticks + spacing_zoom = (ax.transData.inverted()).transform_point((spacing_zoom,0))[0]-(ax.transData.inverted()).transform_point((0,0))[0] + spacing_axes = (ax.transData.inverted()).transform_point((spacing_axes,0))[0]-(ax.transData.inverted()).transform_point((0,0))[0] + + if zoom_x[1] < axes_x[0]: + # the zoom box is on the left + x_connect_bot = [zoom_x[1] + spacing_zoom , axes_x[0] - 0.3*spacing_axes] + x_connect_top = [zoom_x[1] + spacing_zoom , axes_x[0] - 0.3*spacing_axes] + y_connect_bot = np.interp(x_connect_bot,[zoom_x[1],axes_x[0]],[zoom_y[0],axes_y[0]]) + y_connect_top = np.interp(x_connect_top,[zoom_x[1],axes_x[0]],[zoom_y[1],axes_y[1]]) + + if y_connect_bot[1] > axes_y[0]: + x_connect_bot = [zoom_x[1] + spacing_zoom , axes_x[0] - spacing_axes] + y_connect_bot = np.interp(x_connect_bot,[zoom_x[1],axes_x[0]],[zoom_y[0],axes_y[0]]) + + if y_connect_top[1] < axes_y[1]: + x_connect_top = [zoom_x[1] + spacing_zoom , axes_x[0] - spacing_axes] + y_connect_top = np.interp(x_connect_top,[zoom_x[1],axes_x[0]],[zoom_y[1],axes_y[1]]) + + else: + # the zoom box is on the right + # the zoom box is on the left + x_connect_bot = [axes_x[1] + 0.3*spacing_axes , zoom_x[0] - spacing_zoom] + x_connect_top = [axes_x[1] + 0.3*spacing_axes , zoom_x[0] - spacing_zoom] + y_connect_bot = np.interp(x_connect_bot,[axes_x[1],zoom_x[0]],[axes_y[0],zoom_y[0]]) + y_connect_top = np.interp(x_connect_top,[axes_x[1],zoom_x[0]],[axes_y[1],zoom_y[1]]) + + ax.plot(x_connect_bot,y_connect_bot,color=connect_color,alpha=connect_alpha,linewidth=0.4) + ax.plot(x_connect_top,y_connect_top,color=connect_color,alpha=connect_alpha,linewidth=0.4) - return ax1 + return ax1 From 850e80879ed027880e43670ea9bb9c80da7ddfd9 Mon Sep 17 00:00:00 2001 From: BrechtBa Date: Mon, 13 Jun 2016 16:50:39 +0200 Subject: [PATCH 06/11] updated setup to use find_packages --- setup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 693c4f9..074c4a7 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,4 @@ -from setuptools import setup +from setuptools import setup, find_packages setup( name='plottools', @@ -8,7 +8,7 @@ url='', author='Brecht Baeten', author_email='brecht.baeten@gmail.com', - packages=['plottools'], + packages=find_packages(), install_requires=['matplotlib'], classifiers = ['Programming Language :: Python :: 2.7'], ) \ No newline at end of file From 683cab27f142c91e5ff9efcd66ce605e148e39bb Mon Sep 17 00:00:00 2001 From: brecht Date: Thu, 16 Jun 2016 16:54:46 +0200 Subject: [PATCH 07/11] updated colors to have more contrast in grayscale --- examples/color.py | 2 + examples/color.svg | 161 +++++++++++++++++++++-------------------- plottools/plotcolor.py | 34 ++++----- 3 files changed, 101 insertions(+), 96 deletions(-) diff --git a/examples/color.py b/examples/color.py index 629a0eb..739d97c 100644 --- a/examples/color.py +++ b/examples/color.py @@ -6,6 +6,8 @@ # line plot with thick lines plt.figure() x = np.linspace(0,2*np.pi,100) + +pt.color.set_as_default() n = len(pt.color.cycle) for i in range(n): diff --git a/examples/color.svg b/examples/color.svg index 40d7deb..2c86d3e 100644 --- a/examples/color.svg +++ b/examples/color.svg @@ -13,8 +13,8 @@ height="354.33069" id="svg3041" version="1.1" - inkscape:version="0.48.2 r9819" - sodipodi:docname="colors.svg"> + inkscape:version="0.91 r13725" + sodipodi:docname="color.svg"> + inkscape:window-width="766" + inkscape:window-height="690" + inkscape:window-x="584" + inkscape:window-y="0" + inkscape:window-maximized="0" + units="mm" + inkscape:snap-bbox="false" + inkscape:bbox-nodes="false" + inkscape:object-nodes="true"> - + + + + + + + + + id="rect4285" + style="opacity:1;fill:#6394dd;fill-opacity:1;fill-rule:evenodd;stroke:none" /> + id="rect4289" + style="fill:#2cce50;fill-opacity:1;fill-rule:evenodd;stroke:none" /> + id="rect4293" + style="opacity:1;fill:#6f6c71;fill-opacity:1;fill-rule:evenodd;stroke:none" /> - - - - - - - - + id="rect4297" + style="opacity:1;fill:#882126;fill-opacity:1;fill-rule:evenodd;stroke:none" /> + Date: Fri, 17 Jun 2016 11:25:30 +0200 Subject: [PATCH 08/11] changed colors to more colors with equally divided grayscale values added longnames --- examples/color.py | 3 +- examples/color.svg | 1555 ++++++++++++++++++++++++++++++++++++---- examples/grayscale.py | 47 ++ plottools/plotcolor.py | 179 +++-- 4 files changed, 1552 insertions(+), 232 deletions(-) create mode 100644 examples/grayscale.py diff --git a/examples/color.py b/examples/color.py index 739d97c..8aeb5c2 100644 --- a/examples/color.py +++ b/examples/color.py @@ -1,5 +1,6 @@ import matplotlib.pyplot as plt import numpy as np + import plottools as pt @@ -19,7 +20,7 @@ # some lines plt.figure() x = np.linspace(0,2*np.pi,100) -plt.plot(x,np.sin(x- 0*2*np.pi/12),color=pt.color['d'],label='d') +plt.plot(x,np.sin(x- 0*2*np.pi/12),color=pt.color['m'],label='m') plt.plot(x,np.sin(x- 2*2*np.pi/12),color=pt.color['b'],label='b') plt.plot(x,np.sin(x- 4*2*np.pi/12),color=pt.color['r'],label='r') plt.plot(x,np.sin(x- 6*2*np.pi/12),color=pt.color['y'],label='y') diff --git a/examples/color.svg b/examples/color.svg index 2c86d3e..b667636 100644 --- a/examples/color.svg +++ b/examples/color.svg @@ -9,60 +9,119 @@ xmlns="http://www.w3.org/2000/svg" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" - width="354.33069" - height="354.33069" - id="svg3041" + width="210mm" + height="297mm" + viewBox="0 0 744.09448819 1052.3622047" + id="svg2" version="1.1" inkscape:version="0.91 r13725" - sodipodi:docname="color.svg"> + sodipodi:docname="colors.svg"> - - - - - - - - - - - - - - + id="defs4"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + spacingx="3.5433071" + spacingy="3.5433072" + empspacing="10" /> + id="metadata7"> @@ -110,110 +163,1298 @@ - - - - - - - - - - + + r + + o + + y + + g + + l + + t + + a + + n + + b + + p + + f + + y="60.236214" /> + m + + k + + r + + o + y + + g + + l + + t + + a + + n + + b + + p + + f + + m + + height="141.73228" + x="-4.1114723e-007" + y="272.83463" /> + k + r + + o + + y + + g + + l + + t + + a + + n + + b + + p + + f + + m + + k + + o + + height="141.73228" + width="35.433071" + id="rect5958" + style="opacity:1;fill:#cddc23;fill-opacity:1;stroke:none;stroke-width:3.54330707;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" /> + y + + l + + t + + a + + n + + b + + p + + f + m + + id="rect6038" + style="opacity:1;fill:#0d0d0d;fill-opacity:1;stroke:none;stroke-width:3.54330707;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" /> + k + id="rect6084" + style="opacity:1;fill:#426924;fill-opacity:1;stroke:none;stroke-width:3.54330707;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" /> + g + r + + r + + o + + y + + g + + height="141.73228" + x="354.33072" + y="698.03149" /> + l + y="698.03149" /> + t + + a + + n + + b + + p + + f + + m + + k + color + lightcolor + cycle + edited + original - diff --git a/examples/grayscale.py b/examples/grayscale.py new file mode 100644 index 0000000..c3e6f18 --- /dev/null +++ b/examples/grayscale.py @@ -0,0 +1,47 @@ +import matplotlib.pyplot as plt +import matplotlib.patches as patches +import numpy as np + +import plottools as pt + + +# get the grayscale values of all colors +n = len(pt.color.cycle) + +colors = [] +names = [] +grayscales = [] +for i in range(n): + names.append( pt.color.cycle[i] ) + colors.append( pt.color[i] ) + grayscales.append( 0.21*pt.color[i][0] + 0.72*pt.color[i][1] + 0.07*pt.color[i][2] ) + +colors = np.array(colors) +names = np.array(names) +grayscales = np.array(grayscales) + + +index = np.argsort( grayscales ) + +# create a figure with the colors sorted by grayscale +fig = plt.figure() +ax = fig.add_subplot(111) +for i,ind in enumerate(index): + ax.add_patch( patches.Rectangle( (i, 0), 1.0, 5.0, facecolor=colors[ind,:], edgecolor='none') ) + +plt.xlim([0,n]) +plt.ylim([0,5]) + + +# plot the grayscale values in order +plt.figure() +plt.plot(np.arange(n),grayscales[index],'o',color=pt.color['k']) +for i,ind in enumerate(index): + plt.text(i+0.12,grayscales[ind],names[ind]) + +plt.xlim([0,n]) +plt.ylim([0.0,0.9]) + +plt.show() + + diff --git a/plottools/plotcolor.py b/plottools/plotcolor.py index e08aa0f..18ef505 100644 --- a/plottools/plotcolor.py +++ b/plottools/plotcolor.py @@ -24,89 +24,120 @@ # Color definitions ################################################################################ # colors that look ok and have considerable contrast in grayscale -basecolors = {'b': ( 69./255,128./255,215./255), - 'o': (255./255,172./255,103./255), - 'g': ( 40./255,186./255, 72./255), - 'r': (165./255, 30./255, 33./255), - 'k': ( 81./255, 79./255, 82./255), - 'p': (123./255, 90./255,174./255), - 'd': ( 90./255, 22./255, 25./255), - 'y': (185./255,174./255, 85./255)} +basecolors = { + 'k': ( 13./255, 13./255, 13./255), + 'n': ( 0./255, 30./255,104./255), + 'p': ( 77./255, 30./255, 92./255), + 'm': (140./255, 43./255, 0./255), + 'b': ( 7./255, 79./255,255./255), + 'g': ( 66./255,105./255, 36./255), + 'r': (221./255, 79./255, 69./255), + 't': ( 49./255,145./255,136./255), + 'o': (167./255,145./255, 21./255), + 'f': (212./255,136./255,194./255), + 'l': ( 90./255,202./255, 98./255), + 'a': (115./255,209./255,187./255), + 'y': (205./255,220./255, 35./255) } -lightcolors = {'b': ( 99./255,148./255,221./255), - 'o': (235./255,185./255,135./255), - 'g': ( 44./255,206./255, 80./255), - 'r': (217./255, 47./255, 50./255), - 'k': (111./255,108./255,113./255), - 'p': (140./255,111./255,184./255), - 'd': (136./255, 33./255, 38./255), - 'y': (190./255,180./255, 98./255)} - -basecycle = ['b','o','g','r','k','p','d','y'] +lightcolors = { + 'k': ( 33./255, 33./255, 33./255), + 'n': ( 0./255, 42./255,144./255), + 'p': (103./255, 40./255,122./255), + 'm': (180./255, 55./255, 0./255), + 'b': ( 47./255,107./255,255./255), + 'g': ( 85./255,136./255, 46./255), + 'r': (227./255,111./255,103./255), + 't': ( 59./255,175./255,164./255), + 'o': (203./255,173./255, 25./255), + 'f': (222./255,166./255,209./255), + 'l': (120./255,212./255,126./255), + 'a': (145./255,219./255,201./255), + 'y': (213./255,226./255, 70./255) } + +longnames = { + 'black': 'k', + 'navy': 'n', + 'purple': 'p', + 'maroon': 'm', + 'blue': 'b', + 'green': 'g', + 'red': 'r', + 'teal': 't', + 'olive': 'o', + 'fuchsia': 'f', + 'lime': 'l', + 'aqua': 'a', + 'yellow': 'y' } + +basecycle = ['b','r','l','y','m','o','a','n','p','g','t','f','k'] ################################################################################ # Colorscheme class ################################################################################ class Colorscheme(object): - def __init__(self,colors=basecolors,cycle=basecycle): - """ - defines a colorscheme object useful for plotting - - Arguments: - colors: dict, dictionary of named colors as RGB (0-1) tupples - cycle: list, list with the cycle order - - Example: - cs = Colorscheme({'r':(1.,0.,0.),'g':(0.,1.,0.),'b':(0.,0.,1.)},['b','g','r']) - print( cs[0] ) - - print( cs.next() ) - print( cs.next() ) - - cs.reset_index() - print( cs.next() ) - """ - - self.colors = colors - self.cycle = cycle - self.currentindex = 0 + def __init__(self,colors=basecolors,longnames=longnames,cycle=basecycle): + """ + defines a colorscheme object useful for plotting + + Arguments: + colors: dict, dictionary of named colors as RGB (0-1) tupples + cycle: list, list with the cycle order + + Example: + cs = Colorscheme({'r':(1.,0.,0.),'g':(0.,1.,0.),'b':(0.,0.,1.)},['b','g','r']) + print( cs[0] ) + + print( cs.next() ) + print( cs.next() ) + + cs.reset_index() + print( cs.next() ) + """ + + self.colors = colors + self.longnames = longnames + self.cycle = cycle + self.currentindex = 0 - def next(self): - """ - get the next color in the cycle - """ - c = self.colors[self.cycle[self.currentindex]] - self.currentindex += 1 - if self.currentindex >= len(self.cycle): - self.currentindex = 0 - - return c - - def reset_index(self): - """ - resets the current color index to 0 - """ - - self.currentindex = 0 - - def set_as_default(self): - """ - sets the colorscheme as the default color cycle in matplotlib figures - """ - - plt.rc('axes',prop_cycle=cycler('color', [self.colors[c] for c in self.cycle]) ) - - def __getitem__(self,key): - if isinstance(key,int): - self.currentindex = key+1 - return self.colors[self.cycle[key]] - else: - self.currentindex = self.cycle.index(key)+1 - return self.colors[key] + def next(self): + """ + get the next color in the cycle + """ + c = self.colors[self.cycle[self.currentindex]] + self.currentindex += 1 + if self.currentindex >= len(self.cycle): + self.currentindex = 0 + + return c + + def reset_index(self): + """ + resets the current color index to 0 + """ + + self.currentindex = 0 + + def set_as_default(self): + """ + sets the colorscheme as the default color cycle in matplotlib figures + """ + + plt.rc('axes',prop_cycle=cycler('color', [self.colors[c] for c in self.cycle]) ) + + def __getitem__(self,key): + if isinstance(key,int): + self.currentindex = key+1 + return self.colors[self.cycle[key]] + else: + if key in self.longnames: + key = self.longnames[key] + + self.currentindex = self.cycle.index(key)+1 + return self.colors[key] - - + + ################################################################################ # create default color schemes ################################################################################ From 808e52bf0597ea48d1d90a3c16b913886a46c3c9 Mon Sep 17 00:00:00 2001 From: brecht Date: Fri, 17 Jun 2016 11:58:17 +0200 Subject: [PATCH 09/11] made black a little lighter, as a result the total contrast diminishes a bit --- examples/color.svg | 396 ++++++++++++++++++++--------------------- plottools/plotcolor.py | 48 ++--- 2 files changed, 222 insertions(+), 222 deletions(-) diff --git a/examples/color.svg b/examples/color.svg index b667636..e45ffea 100644 --- a/examples/color.svg +++ b/examples/color.svg @@ -15,7 +15,7 @@ id="svg2" version="1.1" inkscape:version="0.91 r13725" - sodipodi:docname="colors.svg"> + sodipodi:docname="color.svg"> + style="opacity:1;fill:#e05f56;fill-opacity:1;stroke:none;stroke-width:3.54330707;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" /> r y + style="opacity:1;fill:#63cd6b;fill-opacity:1;stroke:none;stroke-width:3.54330707;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" /> + style="opacity:1;fill:#369e94;fill-opacity:1;stroke:none;stroke-width:3.54330707;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" /> t + style="opacity:1;fill:#0038be;fill-opacity:1;stroke:none;stroke-width:3.54330707;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" /> n + style="opacity:1;fill:#732d91;fill-opacity:1;stroke:none;stroke-width:3.54330707;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" /> p f + style="opacity:1;fill:#282828;fill-opacity:1;stroke:none;stroke-width:3.54330707;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" /> k - o - y - l - t - a - n - b - p - f - m - k - g - r - r - o - y - g - l - t - a - n - b - p - f - m - original + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/plottools/plotcolor.py b/plottools/plotcolor.py index 18ef505..b3c3b22 100644 --- a/plottools/plotcolor.py +++ b/plottools/plotcolor.py @@ -25,33 +25,33 @@ ################################################################################ # colors that look ok and have considerable contrast in grayscale basecolors = { - 'k': ( 13./255, 13./255, 13./255), - 'n': ( 0./255, 30./255,104./255), - 'p': ( 77./255, 30./255, 92./255), - 'm': (140./255, 43./255, 0./255), - 'b': ( 7./255, 79./255,255./255), - 'g': ( 66./255,105./255, 36./255), - 'r': (221./255, 79./255, 69./255), - 't': ( 49./255,145./255,136./255), - 'o': (167./255,145./255, 21./255), - 'f': (212./255,136./255,194./255), - 'l': ( 90./255,202./255, 98./255), - 'a': (115./255,209./255,187./255), + 'k': ( 40./255, 40./255, 40./255), + 'n': ( 0./255, 56./255,190./255), + 'p': (115./255, 45./255,145./255), + 'm': (186./255, 57./255, 0./255), + 'b': ( 33./255, 96./255,255./255), + 'g': ( 78./255,123./255, 43./255), + 'r': (224./255, 95./255, 86./255), + 't': ( 53./255,158./255,148./255), + 'o': (178./255,152./255, 22./255), + 'f': (214./255,144./255,198./255), + 'l': ( 99./255,205./255,107./255), + 'a': (119./255,211./255,189./255), 'y': (205./255,220./255, 35./255) } lightcolors = { - 'k': ( 33./255, 33./255, 33./255), - 'n': ( 0./255, 42./255,144./255), - 'p': (103./255, 40./255,122./255), - 'm': (180./255, 55./255, 0./255), - 'b': ( 47./255,107./255,255./255), - 'g': ( 85./255,136./255, 46./255), - 'r': (227./255,111./255,103./255), - 't': ( 59./255,175./255,164./255), - 'o': (203./255,173./255, 25./255), - 'f': (222./255,166./255,209./255), - 'l': (120./255,212./255,126./255), - 'a': (145./255,219./255,201./255), + 'k': ( 60./255, 60./255, 60./255), + 'n': ( 0./255, 68./255,230./255), + 'p': (139./255, 54./255,176./255), + 'm': (226./255, 69./255, 0./255), + 'b': ( 73./255,125./255,255./255), + 'g': ( 97./255,153./255, 53./255), + 'r': (230./255,127./255,120./255), + 't': ( 64./255,188./255,176./255), + 'o': (214./255,182./255, 26./255), + 'f': (225./255,173./255,213./255), + 'l': (129./255,215./255,136./255), + 'a': (149./255,221./255,204./255), 'y': (213./255,226./255, 70./255) } longnames = { From 7ced01706f743c116722a68627f0f6aa01671a97 Mon Sep 17 00:00:00 2001 From: brecht Date: Fri, 8 Jul 2016 09:54:15 +0200 Subject: [PATCH 10/11] added functions to linearize the grey scales of a color library and changed the colors --- plottools/linearize_greyscale.py | 139 +++++++++++++++++++++++++++++++ plottools/plotcolor.py | 57 ++++++------- 2 files changed, 163 insertions(+), 33 deletions(-) create mode 100644 plottools/linearize_greyscale.py diff --git a/plottools/linearize_greyscale.py b/plottools/linearize_greyscale.py new file mode 100644 index 0000000..5c213f6 --- /dev/null +++ b/plottools/linearize_greyscale.py @@ -0,0 +1,139 @@ +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.patches as patches +import colorsys + + + +def plot_colors(ax,coldict,order={}): + n = len(coldict.keys()) + + if order == {}: + for i,k in enumerate(coldict.keys()): + order[k] = i + + for k,c in coldict.items(): + o = order[k] + ax.add_patch( patches.Rectangle( (o, 0), 1.0, 5.0, facecolor=c, edgecolor='none') ) + ax.text(o+0.5,2.5,k,ha='center') + + plt.xlim([0,n]) + plt.ylim([0,5]) + + +def rgb_to_grey(r,g,b): + return 0.21*r + 0.72*g + 0.07*b + + +def change_lightness_to_match_greyscale(col,grey): + h,l,s = colorsys.rgb_to_hls(col[0],col[1],col[2]) + + ls = np.linspace(0,1,100) + gs = [] + + for li in ls: + ri,gi,bi = colorsys.hls_to_rgb(h,li,s) + gs.append(rgb_to_grey(ri,gi,bi)) + + + ll = np.interp(grey,gs,ls) + + newcol = colorsys.hls_to_rgb(h,ll,s) + + return newcol + + + + +def linearize_greyscale(colors_rgb,grey_min,grey_max,order={},plot=False): + # convert all colors ro greyscale and determine the order + colors_grey_rgb = {} + grey = [] + keys = [] + for k,c in colors_rgb.items(): + g = rgb_to_grey(c[0],c[1],c[2]) + keys.append(k) + grey.append(g) + colors_grey_rgb[k] = (g,g,g) + + + # define the order + if order == {}: + for i,o in enumerate(np.argsort(grey)): + order[keys[o]] = i + + + + + # rescale to greyscale min-max + grey_edit = np.linspace(grey_min,grey_max,len(order.keys())) + colors_edit_rgb = {} + for k,c in colors_rgb.items(): + colors_edit_rgb[k] = change_lightness_to_match_greyscale(c,grey_edit[order[k]]) + + + # convert all colors ro greyscale and determine the order + colors_edit_grey_rgb = {} + for k,c in colors_edit_rgb.items(): + g = rgb_to_grey(c[0],c[1],c[2]) + colors_edit_grey_rgb[k] = (g,g,g) + + + # print final colors + if plot: + for k,c in colors_edit_rgb.items(): + print( '\'{}\': ({:>3.0f}./255,{:>3.0f}./255,{:>3.0f}./255)'.format(k,c[0]*255,c[1]*255,c[2]*255)) + + fig = plt.figure() + ax1 = fig.add_subplot(221) + plot_colors(ax1,colors_rgb,order) + plt.title('original') + + ax2 = fig.add_subplot(223) + plot_colors(ax2,colors_grey_rgb,order) + + ax3 = fig.add_subplot(222) + plot_colors(ax3,colors_edit_rgb,order) + plt.title('edited') + + ax4 = fig.add_subplot(224) + plot_colors(ax4,colors_edit_grey_rgb,order) + + + return colors_edit_rgb + + +if __name__ == '__main__': + + colors_rgb = { + 'r': (245./255, 82./255, 45./255), + 'o': (244./255,154./255, 26./255), + 'y': (242./255,244./255, 66./255), + 'g': ( 32./255, 81./255, 37./255), + 'l': ( 80./255,180./255, 54./255), + 'a': ( 95./255,173./255,209./255), + 'b': ( 31./255, 57./255,101./255), + 'p': (114./255, 31./255,119./255), + 'k': ( 49./255, 49./255, 49./255) + } + + order = { + 'k': 0, + 'p': 1, + 'b': 2, + 'r': 3, + 'g': 4, + 'o': 5, + 'l': 6, + 'a': 7, + 'y': 8 + } + + print('\nbase colors:') + linearize_greyscale(colors_rgb,0.20,0.90,order=order,plot=True) + + print('\nlight colors:') + linearize_greyscale(colors_rgb,0.40,0.95,order=order,plot=True) + + + plt.show() diff --git a/plottools/plotcolor.py b/plottools/plotcolor.py index b3c3b22..df7dbc5 100644 --- a/plottools/plotcolor.py +++ b/plottools/plotcolor.py @@ -25,51 +25,42 @@ ################################################################################ # colors that look ok and have considerable contrast in grayscale basecolors = { - 'k': ( 40./255, 40./255, 40./255), - 'n': ( 0./255, 56./255,190./255), - 'p': (115./255, 45./255,145./255), - 'm': (186./255, 57./255, 0./255), - 'b': ( 33./255, 96./255,255./255), - 'g': ( 78./255,123./255, 43./255), - 'r': (224./255, 95./255, 86./255), - 't': ( 53./255,158./255,148./255), - 'o': (178./255,152./255, 22./255), - 'f': (214./255,144./255,198./255), - 'l': ( 99./255,205./255,107./255), - 'a': (119./255,211./255,189./255), - 'y': (205./255,220./255, 35./255) } + 'a': (175./255,214./255,232./255) + 'b': ( 54./255,100./255,177./255) + 'g': ( 66./255,168./255, 77./255) + 'k': ( 51./255, 51./255, 51./255) + 'l': (126./255,210./255,104./255) + 'o': (244./255,152./255, 23./255) + 'p': (153./255, 42./255,160./255) + 'r': (245./255, 87./255, 51./255) + 'y': (241./255,243./255, 53./255) +} lightcolors = { - 'k': ( 60./255, 60./255, 60./255), - 'n': ( 0./255, 68./255,230./255), - 'p': (139./255, 54./255,176./255), - 'm': (226./255, 69./255, 0./255), - 'b': ( 73./255,125./255,255./255), - 'g': ( 97./255,153./255, 53./255), - 'r': (230./255,127./255,120./255), - 't': ( 64./255,188./255,176./255), - 'o': (214./255,182./255, 26./255), - 'f': (225./255,173./255,213./255), - 'l': (129./255,215./255,136./255), - 'a': (149./255,221./255,204./255), - 'y': (213./255,226./255, 70./255) } + 'a': (204./255,229./255,240./255) + 'b': (101./255,141./255,208./255) + 'g': (107./255,197./255,116./255) + 'k': (102./255,102./255,102./255) + 'l': (167./255,224./255,152./255) + 'o': (247./255,183./255, 91./255) + 'p': (204./255, 86./255,211./255) + 'r': (248./255,132./255,106./255) + 'y': (248./255,249./255,154./255) +} longnames = { 'black': 'k', - 'navy': 'n', 'purple': 'p', - 'maroon': 'm', 'blue': 'b', 'green': 'g', 'red': 'r', - 'teal': 't', - 'olive': 'o', - 'fuchsia': 'f', + 'orange': 'o', 'lime': 'l', 'aqua': 'a', - 'yellow': 'y' } + 'yellow': 'y' +} -basecycle = ['b','r','l','y','m','o','a','n','p','g','t','f','k'] +basecycle = ['b','r','g','o','y','p','l','a','k'] ################################################################################ From 91c103b1db32f3ce2cb3d4b39e2afa00f260ba32 Mon Sep 17 00:00:00 2001 From: brecht Date: Fri, 8 Jul 2016 10:00:08 +0200 Subject: [PATCH 11/11] bug fixes and tested examples --- examples/color.py | 2 +- ...ze_greyscale.py => linearize_grayscale.py} | 48 +++++++++---------- plottools/plotcolor.py | 32 ++++++------- 3 files changed, 41 insertions(+), 41 deletions(-) rename plottools/{linearize_greyscale.py => linearize_grayscale.py} (68%) diff --git a/examples/color.py b/examples/color.py index 8aeb5c2..91f8bbf 100644 --- a/examples/color.py +++ b/examples/color.py @@ -20,7 +20,7 @@ # some lines plt.figure() x = np.linspace(0,2*np.pi,100) -plt.plot(x,np.sin(x- 0*2*np.pi/12),color=pt.color['m'],label='m') +plt.plot(x,np.sin(x- 0*2*np.pi/12),color=pt.color['l'],label='l') plt.plot(x,np.sin(x- 2*2*np.pi/12),color=pt.color['b'],label='b') plt.plot(x,np.sin(x- 4*2*np.pi/12),color=pt.color['r'],label='r') plt.plot(x,np.sin(x- 6*2*np.pi/12),color=pt.color['y'],label='y') diff --git a/plottools/linearize_greyscale.py b/plottools/linearize_grayscale.py similarity index 68% rename from plottools/linearize_greyscale.py rename to plottools/linearize_grayscale.py index 5c213f6..0f247d8 100644 --- a/plottools/linearize_greyscale.py +++ b/plottools/linearize_grayscale.py @@ -21,11 +21,11 @@ def plot_colors(ax,coldict,order={}): plt.ylim([0,5]) -def rgb_to_grey(r,g,b): +def rgb_to_gray(r,g,b): return 0.21*r + 0.72*g + 0.07*b -def change_lightness_to_match_greyscale(col,grey): +def change_lightness_to_match_grayscale(col,gray): h,l,s = colorsys.rgb_to_hls(col[0],col[1],col[2]) ls = np.linspace(0,1,100) @@ -33,10 +33,10 @@ def change_lightness_to_match_greyscale(col,grey): for li in ls: ri,gi,bi = colorsys.hls_to_rgb(h,li,s) - gs.append(rgb_to_grey(ri,gi,bi)) + gs.append(rgb_to_gray(ri,gi,bi)) - ll = np.interp(grey,gs,ls) + ll = np.interp(gray,gs,ls) newcol = colorsys.hls_to_rgb(h,ll,s) @@ -45,44 +45,44 @@ def change_lightness_to_match_greyscale(col,grey): -def linearize_greyscale(colors_rgb,grey_min,grey_max,order={},plot=False): - # convert all colors ro greyscale and determine the order - colors_grey_rgb = {} - grey = [] +def linearize_grayscale(colors_rgb,gray_min,gray_max,order={},plot=False): + # convert all colors ro grayscale and determine the order + colors_gray_rgb = {} + gray = [] keys = [] for k,c in colors_rgb.items(): - g = rgb_to_grey(c[0],c[1],c[2]) + g = rgb_to_gray(c[0],c[1],c[2]) keys.append(k) - grey.append(g) - colors_grey_rgb[k] = (g,g,g) + gray.append(g) + colors_gray_rgb[k] = (g,g,g) # define the order if order == {}: - for i,o in enumerate(np.argsort(grey)): + for i,o in enumerate(np.argsort(gray)): order[keys[o]] = i - # rescale to greyscale min-max - grey_edit = np.linspace(grey_min,grey_max,len(order.keys())) + # rescale to grayscale min-max + gray_edit = np.linspace(gray_min,gray_max,len(order.keys())) colors_edit_rgb = {} for k,c in colors_rgb.items(): - colors_edit_rgb[k] = change_lightness_to_match_greyscale(c,grey_edit[order[k]]) + colors_edit_rgb[k] = change_lightness_to_match_grayscale(c,gray_edit[order[k]]) - # convert all colors ro greyscale and determine the order - colors_edit_grey_rgb = {} + # convert all colors ro grayscale and determine the order + colors_edit_gray_rgb = {} for k,c in colors_edit_rgb.items(): - g = rgb_to_grey(c[0],c[1],c[2]) - colors_edit_grey_rgb[k] = (g,g,g) + g = rgb_to_gray(c[0],c[1],c[2]) + colors_edit_gray_rgb[k] = (g,g,g) # print final colors if plot: for k,c in colors_edit_rgb.items(): - print( '\'{}\': ({:>3.0f}./255,{:>3.0f}./255,{:>3.0f}./255)'.format(k,c[0]*255,c[1]*255,c[2]*255)) + print( '\'{}\': ({:>3.0f}./255,{:>3.0f}./255,{:>3.0f}./255),'.format(k,c[0]*255,c[1]*255,c[2]*255)) fig = plt.figure() ax1 = fig.add_subplot(221) @@ -90,14 +90,14 @@ def linearize_greyscale(colors_rgb,grey_min,grey_max,order={},plot=False): plt.title('original') ax2 = fig.add_subplot(223) - plot_colors(ax2,colors_grey_rgb,order) + plot_colors(ax2,colors_gray_rgb,order) ax3 = fig.add_subplot(222) plot_colors(ax3,colors_edit_rgb,order) plt.title('edited') ax4 = fig.add_subplot(224) - plot_colors(ax4,colors_edit_grey_rgb,order) + plot_colors(ax4,colors_edit_gray_rgb,order) return colors_edit_rgb @@ -130,10 +130,10 @@ def linearize_greyscale(colors_rgb,grey_min,grey_max,order={},plot=False): } print('\nbase colors:') - linearize_greyscale(colors_rgb,0.20,0.90,order=order,plot=True) + linearize_grayscale(colors_rgb,0.20,0.90,order=order,plot=True) print('\nlight colors:') - linearize_greyscale(colors_rgb,0.40,0.95,order=order,plot=True) + linearize_grayscale(colors_rgb,0.40,0.95,order=order,plot=True) plt.show() diff --git a/plottools/plotcolor.py b/plottools/plotcolor.py index df7dbc5..285b4b4 100644 --- a/plottools/plotcolor.py +++ b/plottools/plotcolor.py @@ -25,26 +25,26 @@ ################################################################################ # colors that look ok and have considerable contrast in grayscale basecolors = { - 'a': (175./255,214./255,232./255) - 'b': ( 54./255,100./255,177./255) - 'g': ( 66./255,168./255, 77./255) - 'k': ( 51./255, 51./255, 51./255) - 'l': (126./255,210./255,104./255) - 'o': (244./255,152./255, 23./255) - 'p': (153./255, 42./255,160./255) - 'r': (245./255, 87./255, 51./255) + 'a': (175./255,214./255,232./255), + 'b': ( 54./255,100./255,177./255), + 'g': ( 66./255,168./255, 77./255), + 'k': ( 51./255, 51./255, 51./255), + 'l': (126./255,210./255,104./255), + 'o': (244./255,152./255, 23./255), + 'p': (153./255, 42./255,160./255), + 'r': (245./255, 87./255, 51./255), 'y': (241./255,243./255, 53./255) } lightcolors = { - 'a': (204./255,229./255,240./255) - 'b': (101./255,141./255,208./255) - 'g': (107./255,197./255,116./255) - 'k': (102./255,102./255,102./255) - 'l': (167./255,224./255,152./255) - 'o': (247./255,183./255, 91./255) - 'p': (204./255, 86./255,211./255) - 'r': (248./255,132./255,106./255) + 'a': (204./255,229./255,240./255), + 'b': (101./255,141./255,208./255), + 'g': (107./255,197./255,116./255), + 'k': (102./255,102./255,102./255), + 'l': (167./255,224./255,152./255), + 'o': (247./255,183./255, 91./255), + 'p': (204./255, 86./255,211./255), + 'r': (248./255,132./255,106./255), 'y': (248./255,249./255,154./255) }