Skip to content

Functionalizing examples/pie_and_polar_charts #3577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions examples/pie_and_polar_charts/pie_demo_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
49 changes: 40 additions & 9 deletions examples/pie_and_polar_charts/polar_bar_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
45 changes: 39 additions & 6 deletions examples/pie_and_polar_charts/polar_scatter_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
16 changes: 9 additions & 7 deletions examples/specialty_plots/hinton_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()