-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
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
Update colormap usage documentation to prioritize string colormap names #29028
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Kaustbh Sorry, I have not been clear on the intended change in #28915.
With the recommendation
So the recommendation would be:
- Use str colormap names on
cmap
arguments.- Use
plt.colormaps[name]
to get the colormap instance.
I primarily intended to change the way we access colormaps in the documentation; i.e. demonstrate by example. The concrete action is: "Go through our docs and apply these rules."
I'm a bit torn whether it's worth to spell the recommendations out explicitly for the user. While it gives direction, it's one more think to read/learn. Also, there's no canonical place to put that recommendation. It certainly does not belong in the diverging_colormaps what's new entry - that link was only where I saw the plt.cm*
use.
Okay, I will give it a try. |
Hi @timhoffm while going through the documentation I saw an example, https://matplotlib.org/stable/gallery/mplot3d/imshow3d.html#sphx-glr-gallery-mplot3d-imshow3d-py, in this example code,
which caused because we are having |
Please leave this as is for this PR - it’s off-topic. (General note: |
Thank you for the clarification! I understand that |
Yes. |
I have made the commit please review and suggest me any changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost good to go. Please still fix the flake8/precommit errors. - The failing test on OSX is unrelated.
Pre commit isort fails. I don’t see directly what is wrong in the code. Can you please check what code changes it suggests? |
Why are 132 files changed? Please don't changed unrelated files. |
I think it happened because of |
I assume, you have triggered isort locally and added all changes it has found? While I don't know why there are that many changes (maybe an update to isort?) these should not be part of this PR. We only want changes that are necessary in the context of this PR. Before your last force-push there have been two or three isort errors in the precommit action. Please do the following:
|
matplotlib/.pre-commit-config.yaml Lines 63 to 68 in f8900ea
|
If I have understood, all your deliberate changes are under
This checks out the |
@@ -39,7 +39,7 @@ | |||
t = Affine2D().scale(skill).rotate_deg(takeoff) | |||
m = MarkerStyle(SUCCESS_SYMBOLS[mood], transform=t) | |||
ax.plot(pos[0], pos[1], marker=m, color=cmap(thrust)) | |||
fig.colorbar(plt.cm.ScalarMappable(norm=Normalize(0, 1), cmap=cmap), | |||
fig.colorbar(plt.cm.ScalarMappable(norm=Normalize(0, 1), cmap="plasma"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now the cmap
variable in unused here, but still on the line above, so you should change cmap
instead of this line.
@@ -104,19 +104,19 @@ def bullseye_plot(ax, data, seg_bold=None, cmap="viridis", norm=None): | |||
|
|||
# Set the colormap and norm to correspond to the data for which | |||
# the colorbar will be used. | |||
cmap = mpl.cm.viridis | |||
cmap = mpl.colormaps["viridis"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cmap
is now unused, and should be removed (as well as the comment about it.)
cax=axs[0].inset_axes([0, -.15, 1, .1]), | ||
orientation='horizontal', label='Some units') | ||
|
||
|
||
# And again for the second colorbar. | ||
cmap2 = mpl.cm.cool | ||
cmap2 = mpl.colormaps["cool"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also unused and should be deleted.
@@ -108,7 +108,7 @@ def test_double_register_builtin_cmap(): | |||
|
|||
|
|||
def test_colormap_copy(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is about the Colormap
class; you should not be replacing it with strings.
Fixes: #28915
This update suggests using string colormap names directly for cmap arguments due to their readability and ease of use. The update also recommends plt.colormaps[name] to retrieve colormap instances, offering a scalable approach that supports both built-in and user-registered colormaps.