8000 Removed unnecessary origin keywords by gautamsagar99 · Pull Request #26608 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Removed unnecessary origin keywords #26608

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
41 changes: 28 additions & 13 deletions galleries/examples/images_contours_and_fields/contourf_demo.py
< 8000 td class="blob-num blob-num-addition empty-cell">
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import matplotlib.pyplot as plt
import numpy as np

origin = 'lower'

delta = 0.025

x = y = np.arange(-3.0, 3.01, delta)
Expand Down Expand Up @@ -41,14 +39,14 @@
# for purposes of illustration.

fig1, ax2 = plt.subplots(layout='constrained')
CS = ax2.contourf(X, Y, Z, 10, cmap=plt.cm.bone, origin=origin)
CS = ax2.contourf(X, Y, Z, 10, cmap=plt.cm.bone)

# Note that in the following, we explicitly pass in a subset of the contour
# levels used for the filled contours. Alternatively, we could pass in
# additional levels to provide extra resolution, or leave out the *levels*
# keyword argument to use all of the original levels.

CS2 = ax2.contour(CS, levels=CS.levels[::2], colors='r', origin=origin)
CS2 = ax2.contour(CS, levels=CS.levels[::2], colors='r')

ax2.set_title('Nonsense (3 masked regions)')
ax2.set_xlabel('word length anomaly')
Expand All @@ -68,20 +66,14 @@

fig2, ax2 = plt.subplots(layout='constrained')
levels = [-1.5, -1, -0.5, 0, 0.5, 1]
CS3 = ax2.contourf(X, Y, Z, levels,
colors=('r', 'g', 'b'),
origin=origin,
extend='both')
CS3 = ax2.contourf(X, Y, Z, levels, colors=('r', 'g', 'b'), extend='both')
# Our data range extends outside the range of levels; make
# data below the lowest contour level yellow, and above the
# highest level cyan:
CS3.cmap.set_under('yellow')
CS3.cmap.set_over('cyan')

CS4 = ax2.contour(X, Y, Z, levels,
colors=('k',),
linewidths=(3,),
origin=origin)
CS4 = ax2.contour(X, Y, Z, levels, colors=('k',), linewidths=(3,))
ax2.set_title('Listed colors (3 masked regions)')
ax2.clabel(CS4, fmt='%2.1f', colors='w', fontsize=14)

Expand All @@ -104,13 +96,36 @@
fig, axs = plt.subplots(2, 2, layout="constrained")

for ax, extend in zip(axs.flat, extends):
cs = ax.contourf(X, Y, Z, levels, cmap=cmap, extend=extend, origin=origin)
cs = ax.contourf(X, Y, Z, levels, cmap=cmap, extend=extend)
fig.colorbar(cs, ax=ax, shrink=0.9)
ax.set_title("extend = %s" % extend)
ax.locator_params(nbins=4)

plt.show()

# %%
# Customizing Contour Plots with the origin Keyword
# -------------------------------------------------
# This code showcases contour plot customization using the origin keyword,
# demonstrating distinct upper and lower origin perspectives.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# Customizing Contour Plots with the origin Keyword
# -------------------------------------------------
# This code showcases contour plot customization using the origin keyword,
# demonstrating distinct upper and lower origin perspectives.
# Orient contour plots using the `origin` keyword
# -------------------------------------------------
# This code demonstrates orienting contour plot data using the *origin* keyword.

Our convention is Title case - first word in title upper case, everything else lower case.
Also origin isn't a customization , it's a tell Matplotlib my data needs to be read from the lower left or upper left corner

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@story645 Thank you for sharing. I will update the changes and push it.

Copy link
Member

Choose a reason for hiding this comment

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

hi, changes aren't showing up

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh is it, Let me recommit and push the latest changes again.


x = np.arange(1, 10)
y = x.reshape(-1, 1)
h = x * y

fig, (ax1, ax2) = plt.subplots(ncols=2)
# Note: lower and upper are the values provided
# to origin argument in contourf.
for (ax, origin) in [(ax1, 'upper'), (ax2, 'lower')]:
ax.set_title(f"origin={origin}")
cs = ax.contourf(h, levels=[10, 30, 50], colors=[
'#808080', '#A0A0A0', '#C0C0C0'], extend='both', origin=origin)
cs.cmap.set_over('red')
cs.cmap.set_under('blue')
cs.changed()
Copy link
Member
@timhoffm timhoffm Aug 30, 2023

Choose a reason for hiding this comment

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

You can simplify this to:

Suggested change
fig, (ax1, ax2) = plt.subplots(ncols=2)
# Note: lower and upper are the values provided
# to origin argument in contourf.
for (ax, origin) in [(ax1, 'upper'), (ax2, 'lower')]:
ax.set_title(f"origin={origin}")
cs = ax.contourf(h, levels=[10, 30, 50], colors=[
'#808080', '#A0A0A0', '#C0C0C0'], extend='both', origin=origin)
cs.cmap.set_over('red')
cs.cmap.set_under('blue')
cs.changed()
fig, (ax1, ax2) = plt.subplots(ncols=2)
ax1.set_title("origin='upper'")
ax2.set_title("origin='lower'")
ax1.contourf(h, levels=np.arange(5, 70, 5), extend='both', origin="upper")
ax2.contourf(h, levels=np.arange(5, 70, 5), extend='both', origin="lower")

grafik

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This look more optimized. Thanks, Let me add it.


plt.show()

# %%
#
# .. admonition:: References
Expand Down
0