8000 DOC: OO interface in api and other examples by phobson · Pull Request #7067 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

DOC: OO interface in api and other examples #7067

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

Merged
merged 3 commits into from
Oct 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
10000 Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions examples/api/bbox_intersect.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
from matplotlib.transforms import Bbox
from matplotlib.path import Path

rect = plt.Rectangle((-1, -1), 2, 2, facecolor="#aaaaaa")
plt.gca().add_patch(rect)
bbox = Bbox.from_bounds(-1, -1, 2, 2)
left, bottom, width, height = (-1, -1, 2, 2)
rect = plt.Rectangle((left, bottom), width, height, facecolor="#aaaaaa")

fig, ax = plt.subplots()
ax.add_patch(rect)

bbox = Bbox.from_bounds(left, bottom, width, height)

for i in range(12):
vertices = (np.random.random((2, 2)) - 0.5) * 6.0
Expand All @@ -14,6 +18,6 @@
color = 'r'
else:
color = 'b'
plt.plot(vertices[:, 0], vertices[:, 1], color=color)
ax.plot(vertices[:, 0], vertices[:, 1], color=color)

plt.show()
28 changes: 28 additions & 0 deletions examples/api/colorbar_basics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import numpy as np
import matplotlib.pyplot as plt

# setup some generic data
N = 37
x, y = np.mgrid[:N, :N]
Z = (np.cos(x*0.2) + np.sin(y*0.3))

# mask out the negative and positve values, respectively
Zpos = np.ma.masked_less(Z, 0)
Zneg = np.ma.masked_greater(Z, 0)

fig, (ax1, ax2) = plt.subplots(figsize=(8, 3), ncols=2)

# plot just the positive data and save the
# color "mappable" object returned by ax1.imshow
pos = ax1.imshow(Zpos, cmap='Blues', interpolation='none')

# add the colorbar using the figure's method,
# telling which mappable we're talking about and
# which axes object it should be near
fig.colorbar(pos, ax=ax1)

# repeat everything above for the the negative data
neg = ax2.imshow(Zneg, cmap='Reds_r', interpolation='none')
fig.colorbar(neg, ax=ax2)

plt.show()
9 changes: 5 additions & 4 deletions examples/api/legend_demo.py
10000
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
d = c[::-1]

# Create plots with pre-defined labels.
plt.plot(a, c, 'k--', label='Model length')
plt.plot(a, d, 'k:', label='Data length')
plt.plot(a, c + d, 'k', label='Total message length')
fig, ax = plt.subplots()
ax.plot(a, c, 'k--', label='Model length')
ax.plot(a, d, 'k:', label='Data length')
ax.plot(a, c + d, 'k', label='Total message length')

legend = plt.legend(loc='upper center', shadow=True, fontsize='x-large')
legend = ax.legend(loc='upper center', shadow=True, fontsize='x-large')

# Put a nicer background color on the legend.
legend.get_frame().set_facecolor('#00FFCC')
Expand Down
2 changes: 1 addition & 1 deletion examples/api/patch_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@
p = PatchCollection(patches, alpha=0.4)
p.set_array(np.array(colors))
ax.add_collection(p)
plt.colorbar(p)
fig.colorbar(p, ax=ax)

plt.show()
27 changes: 13 additions & 14 deletions examples/api/power_norm_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,23 @@
import numpy as np
from numpy.random import multivariate_normal

data = np.vstack([multivariate_normal([10, 10], [[3, 2], [2, 3]], size=100000),
multivariate_normal([30, 20], [[2, 3], [1, 3]], size=1000)
])
data = np.vstack([
multivariate_normal([10, 10], [[3, 2], [2, 3]], size=100000),
multivariate_normal([30, 20], [[2, 3], [1, 3]], size=1000)
])

gammas = [0.8, 0.5, 0.3]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoa, really? That was unnecessarily confusing.

Sorry, something went wrong.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed! As much as I appreciate clever ways to place axes, it's my opinion
that the examples should show off the smart ways to use subplots and
gridspec.
On Thu, Sep 8, 2016 at 18:45 Elliott Sales de Andrade <
notifications@github.com> wrote:

In examples/api/power_norm_demo.py
#7067 (comment):

gammas = [0.8, 0.5, 0.3]
-xgrid = np.floor((len(gammas) + 1.) / 2)

Whoa, really? That was unnecessarily confusing.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/matplotlib/matplotlib/pull/7067/files/182694fce5b1a78eb119d92772145b1b94a751a3#r78118307,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABHCo90lvE6McNphKazUnd28VNOkfaXIks5qoLo0gaJpZM4J4nIw
.

xgrid = np.floor((len(gammas) + 1.) / 2)
ygrid = np.ceil((len(gammas) + 1.) / 2)

plt.subplot(xgrid, ygrid, 1)
plt.title('Linear normalization')
plt.hist2d(data[:, 0], data[:, 1], bins=100)
fig, axes = plt.subplots(nrows=2, ncols=2)

for i, gamma in enumerate(gammas):
plt.subplot(xgrid, ygrid, i + 2)
plt.title('Power law\n$(\gamma=%1.1f)$' % gamma)
plt.hist2d(data[:, 0], data[:, 1],
bins=100, norm=mcolors.PowerNorm(gamma))
axes[0, 0].set_title('Linear normalization')
axes[0, 0].hist2d(data[:, 0], data[:, 1], bins=100)

plt.tight_layout()
for ax, gamma in zip(axes.flat[1:], gammas):
ax.set_title('Power law $(\gamma=%1.1f)$' % gamma)
ax.hist2d(data[:, 0], data[:, 1],
bins=100, norm=mcolors.PowerNorm(gamma))

fig.tight_layout()

plt.show()
19 changes: 10 additions & 9 deletions examples/api/radar_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,14 @@ def example_data():
data = example_data()
spoke_labels = data.pop(0)

fig = plt.figure(figsize=(9, 9))
fig, axes = plt.subplots(figsize=(9, 9), nrows=2, ncols=2,
subplot_kw=dict(projection='radar'))
fig.subplots_adjust(wspace=0.25, hspace=0.20, top=0.85, bottom=0.05)

colors = ['b', 'r', 'g', 'm', 'y']
# Plot the four cases from the example data on separate axes
for n, (title, case_data) in enumerate(data):
ax = fig.add_subplot(2, 2, n + 1, projection='radar')
plt.rgrids([0.2, 0.4, 0.6, 0.8])
for ax, (title, case_data) in zip(axes.flatten(), data):
ax.set_rgrids([0.2, 0.4, 0.6, 0.8])
ax.set_title(title, weight='bold', size='medium', position=(0.5, 1.1),
horizontalalignment='center', verticalalignment='center')
for d, color in zip(case_data, colors):
Expand All @@ -185,11 +185,12 @@ def example_data():
ax.set_varlabels(spoke_labels)

# add legend relative to top-left plot
plt.subplot(2, 2, 1)
ax = axes[0, 0]
labels = ('Factor 1', 'Factor 2', 'Factor 3', 'Factor 4', 'Factor 5')
legend = plt.legend(labels, loc=(0.9, .95), labelspacing=0.1)
plt.setp(legend.get_texts(), fontsize='small')
legend = ax.legend(labels, loc=(0.9, .95), labelspacing=0.1, fontsize='small')

fig.text(0.5, 0.965, '5-Factor Solution Profiles Across Four Scenarios',
horizontalalignment='center', color='black', weight='bold',
size='large')

plt.figtext(0.5, 0.965, '5-Factor Solution Profiles Across Four Scenarios',
ha='center', color='black', weight='bold', size='large')
plt.show()
2 changes: 1 addition & 1 deletion examples/color/color_cycle_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
ax1.set_title('Set axes color cycle to cmyk')

# Tweak spacing between subplots to prevent labels from overlapping
plt.subplots_adjust(hspace=0.3)
fig.subplots_adjust(hspace=0.3)
plt.show()
5 changes: 3 additions & 2 deletions examples/images_contours_and_fields/contourf_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@

# Automatic selection of levels works; setting the
# log locator tells contourf to use a log scale:
cs = plt.contourf(X, Y, z, locator=ticker.LogLocator(), cmap=cm.PuBu_r)
fig, ax = plt.subplots()
cs = ax.contourf(X, Y, z, locator=ticker.LogLocator(), cmap=cm.PuBu_r)

# Alternatively, you can manually set the levels
# and the norm:
Expand All @@ -41,6 +42,6 @@

# The 'extend' kwarg does not work yet with a log scale.

cbar = plt.colorbar()
cbar = fig.colorbar(cs)

plt.show()
5 changes: 3 additions & 2 deletions examples/images_contours_and_fields/image_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
image_file = cbook.get_sample_data('ada.png')
image = plt.imread(image_file)

plt.imshow(image)
plt.axis('off') # clear x- and y-axes
fig, ax = plt.subplots()
ax.imshow(image)
ax.axis('off') # clear x- and y-axes
plt.show()
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
patch = patches.Circle((260, 200), radius=200, transform=ax.transData)
im.set_clip_path(patch)

plt.axis('off')
ax.axis('off')
plt.show()
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@
[0.6, 0.3, 0.0, 0.2], [0.7, 0.9, 0.4, 0.6]])

# Create a 2x2 table of plots
fig = plt.figure(figsize=[8.0, 7.5])
ax = plt.subplot(2, 2, 1)
ax.imshow(big_im, interpolation='none')
ax = plt.subplot(2, 2, 2)
ax.imshow(big_im, interpolation='nearest')
ax = plt.subplot(2, 2, 3)
ax.imshow(small_im, interpolation='none')
ax = plt.subplot(2, 2, 4)
ax.imshow(small_im, interpolation='nearest')
plt.subplots_adjust(left=0.24, wspace=0.2, hspace=0.1,
fig, axes = plt.subplots(figsize=[8.0, 7.5], ncols=2, nrows=2)

axes[0, 0].imshow(big_im, interpolation='none')
axes[0, 1].imshow(big_im, interpolation='nearest')
axes[1, 0].imshow(small_im, interpolation='none')
axes[1, 1].imshow(small_im, interpolation='nearest')
fig.subplots_adjust(left=0.24, wspace=0.2, hspace=0.1,
bottom=0.05, top=0.86)

# Label the rows and columns of the table
Expand Down Expand Up @@ -60,6 +57,6 @@
pdf_im_path = cbook.get_sample_data('None_vs_nearest-pdf.png')
pdf_im = plt.imread(pdf_im_path)
fig2 = plt.figure(figsize=[8.0, 7.5])
plt.figimage(pdf_im)
fig2.figimage(pdf_im)

plt.show()
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
U = np.ma.array(U, mask=mask)
U[:20, :20] = np.nan

plt.streamplot(X, Y, U, V, color='r')
fig, ax = plt.subplots()
ax.streamplot(X, Y, U, V, color='r')

plt.imshow(~mask, extent=(-w, w, -w, w), alpha=0.5,
interpolation='nearest', cmap=plt.cm.gray)
ax.imshow(~mask, extent=(-w, w, -w, w), alpha=0.5,
interpolation='nearest', cmap=plt.cm.gray)

plt.show()
0