Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
DOC: use OO-ish interface in image, contour, field examples#10865
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -8,6 +8,7 @@ | ||
See also contour_demo.py. | ||
""" | ||
import matplotlib | ||
import numpy as np | ||
import matplotlib.cm as cm | ||
@@ -32,14 +33,10 @@ | ||
# Make contour labels using creative float classes | ||
# Follows suggestion of Manuel Metz | ||
# Define a class that forces representation of float to look a certain way | ||
# This remove trailing zero so '1.0' becomes '1' | ||
class nf(float): | ||
def __repr__(self): | ||
str = '%.1f' % (self.__float__(),) | ||
@@ -49,6 +46,10 @@ def __repr__(self): | ||
return '%.1f' % self.__float__() | ||
# Basic contour plot | ||
fig, ax = plt.subplots() | ||
CS = ax.contour(X, Y, Z) | ||
# Recast levels to new class | ||
CS.levels = [nf(val) for val in CS.levels] | ||
@@ -57,33 +58,34 @@ def __repr__(self): | ||
fmt = r'%r \%%' | ||
else: | ||
fmt = '%r %%' | ||
ax.clabel(CS, CS.levels, inline=True, fmt=fmt, fontsize=10) | ||
############################################################################### | ||
# Label contours with arbitrary strings using a dictionary | ||
fig1, ax1 =plt.subplots() | ||
# Basic contour plot | ||
CS1 =ax1.contour(X, Y, Z) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Why not reuse There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. My thought (which is behind e.g., | ||
fmt = {} | ||
strs = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh'] | ||
for l, s in zip(CS1.levels, strs): | ||
fmt[l] = s | ||
# Label every other level using strings | ||
ax1.clabel(CS1, CS1.levels[::2], inline=True, fmt=fmt, fontsize=10) | ||
############################################################################### | ||
# Use a Formatter | ||
fig2, ax2 =plt.subplots() | ||
CS2 =ax2.contour(X, Y, 100**Z, locator=plt.LogLocator()) | ||
fmt = ticker.LogFormatterMathtext() | ||
fmt.create_dummy_axis() | ||
ax2.clabel(CS2, CS2.levels, fmt=fmt) | ||
ax2.set_title("$100^Z$") | ||
plt.show() |
Uh oh!
There was an error while loading.Please reload this page.