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..a9274ba5dbef 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,11 @@ 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()