From 26f44f862361c8bba3c89a455175630e27d8a8e5 Mon Sep 17 00:00:00 2001 From: Matthew Conway Date: Sat, 27 Sep 2014 11:30:01 -0400 Subject: [PATCH 1/7] functionalize polar_bar_demo --- .../pie_and_polar_charts/polar_bar_demo.py | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/examples/pie_and_polar_charts/polar_bar_demo.py b/examples/pie_and_polar_charts/polar_bar_demo.py index 459dd6f958f1..d261702b5490 100644 --- a/examples/pie_and_polar_charts/polar_bar_demo.py +++ b/examples/pie_and_polar_charts/polar_bar_demo.py @@ -6,16 +6,22 @@ N = 20 -theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False) -radii = 10 * np.random.rand(N) -width = np.pi / 4 * np.random.rand(N) +ex_theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False) +ex_radii = 10 * np.random.rand(N) +ex_width = np.pi / 4 * np.random.rand(N) -ax = plt.subplot(111, polar=True) -bars = ax.bar(theta, radii, width=width, bottom=0.0) + +def polar_bar_demo(theta,radii,width): + + ax = plt.subplot(111, polar=True) + bars = ax.bar(theta, radii, width=width, bottom=0.0) # Use custom colors and opacity -for r, bar in zip(radii, bars): - bar.set_facecolor(plt.cm.jet(r / 10.)) - bar.set_alpha(0.5) + for r, bar in zip(radii, bars): + bar.set_facecolor(plt.cm.jet(r / 10.)) + bar.set_alpha(0.5) + + plt.show() + return ax -plt.show() +polar_bar_demo(ex_theta,ex_radii,ex_width) From a7b8dfff76fd939eb113f6189e487ba1772bd7fd Mon Sep 17 00:00:00 2001 From: Matthew Conway Date: Sat, 27 Sep 2014 11:46:29 -0400 Subject: [PATCH 2/7] add docstring and ax as parameter --- .../pie_and_polar_charts/polar_bar_demo.py | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/examples/pie_and_polar_charts/polar_bar_demo.py b/examples/pie_and_polar_charts/polar_bar_demo.py index d261702b5490..e59dfede73e7 100644 --- a/examples/pie_and_polar_charts/polar_bar_demo.py +++ b/examples/pie_and_polar_charts/polar_bar_demo.py @@ -10,10 +10,34 @@ ex_radii = 10 * np.random.rand(N) ex_width = np.pi / 4 * np.random.rand(N) +ax = plt.subplot(111, polar=True) -def polar_bar_demo(theta,radii,width): +def polar_bar_demo(ax,theta,radii,width): + """ + produces a randomly colored polar plot given three arrays, + theta, radii,width . + + Parameters + ---------- + ax : PolarAxesSubplot + Axes on which to plot polar_bar + + theta : array + Angles at which to plot polar bars + + radii : array + lengths of polar bar + + width : array + widths of polars bars + + Returns + ------- + ax : PolarAxesSubplot + + Returns axes for further modification. + """ - ax = plt.subplot(111, polar=True) bars = ax.bar(theta, radii, width=width, bottom=0.0) # Use custom colors and opacity @@ -24,4 +48,4 @@ def polar_bar_demo(theta,radii,width): plt.show() return ax -polar_bar_demo(ex_theta,ex_radii,ex_width) +polar_bar_demo(ax,ex_theta,ex_radii,ex_width) From 2c95a4c95fa09239319b0f994d19c6e9f40d5ea7 Mon Sep 17 00:00:00 2001 From: Matthew Conway Date: Sat, 27 Sep 2014 12:49:07 -0400 Subject: [PATCH 3/7] fixed pie_demo to be more functional --- .../pie_and_polar_charts/pie_demo_features.py | 41 ++++++++++++++++--- .../pie_and_polar_charts/polar_bar_demo.py | 12 +++--- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/examples/pie_and_polar_charts/pie_demo_features.py b/examples/pie_and_polar_charts/pie_demo_features.py index ae71b172c10d..f3f4ea0fb6a8 100644 --- a/examples/pie_and_polar_charts/pie_demo_features.py +++ b/examples/pie_and_polar_charts/pie_demo_features.py @@ -18,16 +18,47 @@ """ import matplotlib.pyplot as plt - +# Example data: # The slices will be ordered and plotted counter-clockwise. labels = 'Frogs', 'Hogs', 'Dogs', 'Logs' sizes = [15, 30, 45, 10] colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral'] explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs') +ax = plt.subplot(111) + +def pie_demo_features(ax,sizes,labels,colors,explode): + """ + produces a simple pie plot on ax according to sizes, labels, colors and explode. + + Parameters + ---------- + ax : PolarAxesSubplot + Axes on which to plot polar_bar + + sizes : array + comparative sizes of pie slices + + labels : array + names of pie slices + + colors : array + colors of pie slices + + explode : tuple + how far to explode each slice (0 for don't explode) + + Returns + ------- + pi : artist object returned + + Returns artist for further modification. + """ -plt.pie(sizes, explode=explode, labels=labels, colors=colors, - autopct='%1.1f%%', shadow=True, startangle=90) -# Set aspect ratio to be equal so that pie is drawn as a circle. -plt.axis('equal') + pi = ax.pie(sizes,explode=explode,labels=labels,colors=colors, + autopct='%1.1f%%', shadow=True, startangle=90) + # Set aspect rtio to be equal so that pie is drawn as a circle. + ax.axis('equal'); + return pi; +pie_demo_features(ax,sizes,labels,colors,explode) plt.show() diff --git a/examples/pie_and_polar_charts/polar_bar_demo.py b/examples/pie_and_polar_charts/polar_bar_demo.py index e59dfede73e7..ed3fbfb81727 100644 --- a/examples/pie_and_polar_charts/polar_bar_demo.py +++ b/examples/pie_and_polar_charts/polar_bar_demo.py @@ -5,13 +5,12 @@ import matplotlib.pyplot as plt +# Generate Example Data. N = 20 ex_theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False) ex_radii = 10 * np.random.rand(N) ex_width = np.pi / 4 * np.random.rand(N) -ax = plt.subplot(111, polar=True) - def polar_bar_demo(ax,theta,radii,width): """ produces a randomly colored polar plot given three arrays, @@ -33,7 +32,7 @@ def polar_bar_demo(ax,theta,radii,width): Returns ------- - ax : PolarAxesSubplot + bars : artist object returned Returns axes for further modification. """ @@ -45,7 +44,10 @@ def polar_bar_demo(ax,theta,radii,width): bar.set_facecolor(plt.cm.jet(r / 10.)) bar.set_alpha(0.5) - plt.show() - return ax + return bars + +ax = plt.subplot(111, polar=True) polar_bar_demo(ax,ex_theta,ex_radii,ex_width) + +plt.show() From 578f31422779212029112cc971c66af20f11d072 Mon Sep 17 00:00:00 2001 From: Matthew Conway Date: Sat, 27 Sep 2014 12:55:13 -0400 Subject: [PATCH 4/7] functionalize polar_scatter_demo --- .../polar_scatter_demo.py | 45 ++++++++++++++++--- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/examples/pie_and_polar_charts/polar_scatter_demo.py b/examples/pie_and_polar_charts/polar_scatter_demo.py index 90eea4e2b3a8..ede2b296a3ae 100644 --- a/examples/pie_and_polar_charts/polar_scatter_demo.py +++ b/examples/pie_and_polar_charts/polar_scatter_demo.py @@ -7,15 +7,46 @@ import numpy as np import matplotlib.pyplot as plt - +# example data N = 150 -r = 2 * np.random.rand(N) -theta = 2 * np.pi * np.random.rand(N) -area = 200 * r**2 * np.random.rand(N) -colors = theta +ex_r = 2 * np.random.rand(N) +ex_theta = 2 * np.pi * np.random.rand(N) +ex_area = 200 * r**2 * np.random.rand(N) +ex_colors = theta + +def polar_scatter_demo(ax,theta,r,area,colors): + """ + produces a randomly colored polar plot given three arrays, + theta, radii,width . + + Parameters + ---------- + ax : PolarAxesSubplot + Axes on which to plot polar_scatter + + theta : array + Angles at which to plot points + + r : array + radii at which to plot points + + area : array + sizes of points + + colors : array + color values of points. + + Returns + ------- + c : artist object returned + + """ + c = ax.scatter(theta, r, c=colors, s=area, cmap=plt.cm.hsv) + c.set_alpha(0.75) + return c ax = plt.subplot(111, polar=True) -c = plt.scatter(theta, r, c=colors, s=area, cmap=plt.cm.hsv) -c.set_alpha(0.75) +polar_scatter_demo(ax,ex_theta,ex_r,ex_area,ex_colors) + plt.show() From b5c9b77e72d8ecc284a5a00c839eaf0517867aba Mon Sep 17 00:00:00 2001 From: Matthew Conway Date: Sat, 27 Sep 2014 13:21:02 -0400 Subject: [PATCH 5/7] make hinton match functional example format --- examples/specialty_plots/hinton_demo.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/specialty_plots/hinton_demo.py b/examples/specialty_plots/hinton_demo.py index 3839004a7ce0..9bab5c4b18fe 100644 --- a/examples/specialty_plots/hinton_demo.py +++ b/examples/specialty_plots/hinton_demo.py @@ -12,9 +12,8 @@ import matplotlib.pyplot as plt -def hinton(matrix, max_weight=None, ax=None): +def hinton(ax, matrix, max_weight=None): """Draw Hinton diagram for visualizing a weight matrix.""" - ax = ax if ax is not None else plt.gca() if not max_weight: max_weight = 2**np.ceil(np.log(np.abs(matrix).max())/np.log(2)) @@ -33,8 +32,9 @@ def hinton(matrix, max_weight=None, ax=None): ax.autoscale_view() ax.invert_yaxis() + return ax +ax = plt.subplot(111) -if __name__ == '__main__': - hinton(np.random.rand(20, 20) - 0.5) - plt.show() +hinton(ax,np.random.rand(20, 20) - 0.5) +plt.show() From c035e4f4c70de26539d521972c413b7d24d88e3c Mon Sep 17 00:00:00 2001 From: Matthew Conway Date: Sat, 27 Sep 2014 13:45:25 -0400 Subject: [PATCH 6/7] pep8 and move data to after function --- .../pie_and_polar_charts/pie_demo_features.py | 25 ++++++++++--------- .../pie_and_polar_charts/polar_bar_demo.py | 17 ++++++------- .../polar_scatter_demo.py | 18 +++++++------ 3 files changed, 31 insertions(+), 29 deletions(-) diff --git a/examples/pie_and_polar_charts/pie_demo_features.py b/examples/pie_and_polar_charts/pie_demo_features.py index f3f4ea0fb6a8..e47837471e32 100644 --- a/examples/pie_and_polar_charts/pie_demo_features.py +++ b/examples/pie_and_polar_charts/pie_demo_features.py @@ -18,15 +18,8 @@ """ import matplotlib.pyplot as plt -# Example data: -# The slices will be ordered and plotted counter-clockwise. -labels = 'Frogs', 'Hogs', 'Dogs', 'Logs' -sizes = [15, 30, 45, 10] -colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral'] -explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs') -ax = plt.subplot(111) -def pie_demo_features(ax,sizes,labels,colors,explode): +def pie_demo_features(ax, sizes, labels, colors, explode): """ produces a simple pie plot on ax according to sizes, labels, colors and explode. @@ -54,11 +47,19 @@ def pie_demo_features(ax,sizes,labels,colors,explode): Returns artist for further modification. """ - pi = ax.pie(sizes,explode=explode,labels=labels,colors=colors, + pi = ax.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=90) # Set aspect rtio to be equal so that pie is drawn as a circle. - ax.axis('equal'); - return pi; + ax.axis('equal') + return pi + +# Example data: +# The slices will be ordered and plotted counter-clockwise. +labels = 'Frogs', 'Hogs', 'Dogs', 'Logs' +sizes = [15, 30, 45, 10] +colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral'] +explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs') +ax = plt.subplot(111) -pie_demo_features(ax,sizes,labels,colors,explode) +pie_demo_features(ax, sizes, labels, colors, explode) plt.show() diff --git a/examples/pie_and_polar_charts/polar_bar_demo.py b/examples/pie_and_polar_charts/polar_bar_demo.py index ed3fbfb81727..a06b4ca75428 100644 --- a/examples/pie_and_polar_charts/polar_bar_demo.py +++ b/examples/pie_and_polar_charts/polar_bar_demo.py @@ -5,13 +5,7 @@ import matplotlib.pyplot as plt -# Generate Example Data. -N = 20 -ex_theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False) -ex_radii = 10 * np.random.rand(N) -ex_width = np.pi / 4 * np.random.rand(N) - -def polar_bar_demo(ax,theta,radii,width): +def polar_bar_demo(ax, theta, radii, width): """ produces a randomly colored polar plot given three arrays, theta, radii,width . @@ -44,10 +38,15 @@ def polar_bar_demo(ax,theta,radii,width): bar.set_facecolor(plt.cm.jet(r / 10.)) bar.set_alpha(0.5) - return bars +# Generate Example Data. +N = 20 +ex_theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False) +ex_radii = 10 * np.random.rand(N) +ex_width = np.pi / 4 * np.random.rand(N) + ax = plt.subplot(111, polar=True) -polar_bar_demo(ax,ex_theta,ex_radii,ex_width) +polar_bar_demo(ax, ex_theta, ex_radii, ex_width) plt.show() diff --git a/examples/pie_and_polar_charts/polar_scatter_demo.py b/examples/pie_and_polar_charts/polar_scatter_demo.py index ede2b296a3ae..7a5b02b6a6f8 100644 --- a/examples/pie_and_polar_charts/polar_scatter_demo.py +++ b/examples/pie_and_polar_charts/polar_scatter_demo.py @@ -7,14 +7,8 @@ import numpy as np import matplotlib.pyplot as plt -# example data -N = 150 -ex_r = 2 * np.random.rand(N) -ex_theta = 2 * np.pi * np.random.rand(N) -ex_area = 200 * r**2 * np.random.rand(N) -ex_colors = theta -def polar_scatter_demo(ax,theta,r,area,colors): +def polar_scatter_demo(ax, theta, r, area, colors): """ produces a randomly colored polar plot given three arrays, theta, radii,width . @@ -45,8 +39,16 @@ def polar_scatter_demo(ax,theta,r,area,colors): c.set_alpha(0.75) return c + +# example data +N = 150 +ex_r = 2 * np.random.rand(N) +ex_theta = 2 * np.pi * np.random.rand(N) +ex_area = 200 * r ** 2 * np.random.rand(N) +ex_colors = theta + ax = plt.subplot(111, polar=True) -polar_scatter_demo(ax,ex_theta,ex_r,ex_area,ex_colors) +polar_scatter_demo(ax, ex_theta, ex_r, ex_area, ex_colors) plt.show() From de58c9570021cd49187246a4787af43bfc3679f7 Mon Sep 17 00:00:00 2001 From: Matthew Conway Date: Sat, 27 Sep 2014 17:03:07 -0400 Subject: [PATCH 7/7] fix pep8 --- .../pie_and_polar_charts/pie_demo_features.py | 44 ++++++++++++++--- .../pie_and_polar_charts/polar_bar_demo.py | 49 +++++++++++++++---- .../polar_scatter_demo.py | 45 ++++++++++++++--- examples/specialty_plots/hinton_demo.py | 14 +++--- 4 files changed, 124 insertions(+), 28 deletions(-) diff --git a/examples/pie_and_polar_charts/pie_demo_features.py b/examples/pie_and_polar_charts/pie_demo_features.py index ae71b172c10d..e47837471e32 100644 --- a/examples/pie_and_polar_charts/pie_demo_features.py +++ b/examples/pie_and_polar_charts/pie_demo_features.py @@ -19,15 +19,47 @@ import matplotlib.pyplot as plt +def pie_demo_features(ax, sizes, labels, colors, explode): + """ + produces a simple pie plot on ax according to sizes, labels, colors and explode. + + Parameters + ---------- + ax : PolarAxesSubplot + Axes on which to plot polar_bar + + sizes : array + comparative sizes of pie slices + + labels : array + names of pie slices + + colors : array + colors of pie slices + + explode : tuple + how far to explode each slice (0 for don't explode) + + Returns + ------- + pi : artist object returned + + Returns artist for further modification. + """ + + pi = ax.pie(sizes, explode=explode, labels=labels, colors=colors, + autopct='%1.1f%%', shadow=True, startangle=90) + # Set aspect rtio to be equal so that pie is drawn as a circle. + ax.axis('equal') + return pi + +# Example data: # The slices will be ordered and plotted counter-clockwise. labels = 'Frogs', 'Hogs', 'Dogs', 'Logs' sizes = [15, 30, 45, 10] colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral'] -explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs') - -plt.pie(sizes, explode=explode, labels=labels, colors=colors, - autopct='%1.1f%%', shadow=True, startangle=90) -# Set aspect ratio to be equal so that pie is drawn as a circle. -plt.axis('equal') +explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs') +ax = plt.subplot(111) +pie_demo_features(ax, sizes, labels, colors, explode) plt.show() diff --git a/examples/pie_and_polar_charts/polar_bar_demo.py b/examples/pie_and_polar_charts/polar_bar_demo.py index 459dd6f958f1..a06b4ca75428 100644 --- a/examples/pie_and_polar_charts/polar_bar_demo.py +++ b/examples/pie_and_polar_charts/polar_bar_demo.py @@ -5,17 +5,48 @@ import matplotlib.pyplot as plt -N = 20 -theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False) -radii = 10 * np.random.rand(N) -width = np.pi / 4 * np.random.rand(N) +def polar_bar_demo(ax, theta, radii, width): + """ + produces a randomly colored polar plot given three arrays, + theta, radii,width . -ax = plt.subplot(111, polar=True) -bars = ax.bar(theta, radii, width=width, bottom=0.0) + Parameters + ---------- + ax : PolarAxesSubplot + Axes on which to plot polar_bar + + theta : array + Angles at which to plot polar bars + + radii : array + lengths of polar bar + + width : array + widths of polars bars + + Returns + ------- + bars : artist object returned + + Returns axes for further modification. + """ + + bars = ax.bar(theta, radii, width=width, bottom=0.0) # Use custom colors and opacity -for r, bar in zip(radii, bars): - bar.set_facecolor(plt.cm.jet(r / 10.)) - bar.set_alpha(0.5) + for r, bar in zip(radii, bars): + bar.set_facecolor(plt.cm.jet(r / 10.)) + bar.set_alpha(0.5) + + return bars + +# Generate Example Data. +N = 20 +ex_theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False) +ex_radii = 10 * np.random.rand(N) +ex_width = np.pi / 4 * np.random.rand(N) + +ax = plt.subplot(111, polar=True) +polar_bar_demo(ax, ex_theta, ex_radii, ex_width) plt.show() diff --git a/examples/pie_and_polar_charts/polar_scatter_demo.py b/examples/pie_and_polar_charts/polar_scatter_demo.py index 90eea4e2b3a8..7a5b02b6a6f8 100644 --- a/examples/pie_and_polar_charts/polar_scatter_demo.py +++ b/examples/pie_and_polar_charts/polar_scatter_demo.py @@ -8,14 +8,47 @@ import matplotlib.pyplot as plt +def polar_scatter_demo(ax, theta, r, area, colors): + """ + produces a randomly colored polar plot given three arrays, + theta, radii,width . + + Parameters + ---------- + ax : PolarAxesSubplot + Axes on which to plot polar_scatter + + theta : array + Angles at which to plot points + + r : array + radii at which to plot points + + area : array + sizes of points + + colors : array + color values of points. + + Returns + ------- + c : artist object returned + + """ + c = ax.scatter(theta, r, c=colors, s=area, cmap=plt.cm.hsv) + c.set_alpha(0.75) + return c + + +# example data N = 150 -r = 2 * np.random.rand(N) -theta = 2 * np.pi * np.random.rand(N) -area = 200 * r**2 * np.random.rand(N) -colors = theta +ex_r = 2 * np.random.rand(N) +ex_theta = 2 * np.pi * np.random.rand(N) +ex_area = 200 * r ** 2 * np.random.rand(N) +ex_colors = theta ax = plt.subplot(111, polar=True) -c = plt.scatter(theta, r, c=colors, s=area, cmap=plt.cm.hsv) -c.set_alpha(0.75) + +polar_scatter_demo(ax, ex_theta, ex_r, ex_area, ex_colors) plt.show() diff --git a/examples/specialty_plots/hinton_demo.py b/examples/specialty_plots/hinton_demo.py index 3839004a7ce0..b16bb94c0dcb 100644 --- a/examples/specialty_plots/hinton_demo.py +++ b/examples/specialty_plots/hinton_demo.py @@ -12,19 +12,18 @@ import matplotlib.pyplot as plt -def hinton(matrix, max_weight=None, ax=None): +def hinton(ax, matrix, max_weight=None): """Draw Hinton diagram for visualizing a weight matrix.""" - ax = ax if ax is not None else plt.gca() if not max_weight: - max_weight = 2**np.ceil(np.log(np.abs(matrix).max())/np.log(2)) + max_weight = 2 ** np.ceil(np.log(np.abs(matrix).max()) / np.log(2)) ax.patch.set_facecolor('gray') ax.set_aspect('equal', 'box') ax.xaxis.set_major_locator(plt.NullLocator()) ax.yaxis.set_major_locator(plt.NullLocator()) - for (x,y),w in np.ndenumerate(matrix): + for (x, y), w in np.ndenumerate(matrix): color = 'white' if w > 0 else 'black' size = np.sqrt(np.abs(w)) rect = plt.Rectangle([x - size / 2, y - size / 2], size, size, @@ -33,8 +32,9 @@ def hinton(matrix, max_weight=None, ax=None): ax.autoscale_view() ax.invert_yaxis() + return ax +ax = plt.subplot(111) -if __name__ == '__main__': - hinton(np.random.rand(20, 20) - 0.5) - plt.show() +hinton(ax, np.random.rand(20, 20) - 0.5) +plt.show()