Voxels as mappable for colorbar & changed return type of voxels #30982
+140
−41
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR summary
This PR is in response to and closes #22969 [ENH]: Voxels as mappable for colorbar
This change allows axes3d.voxels() to take scalar data as input, and map it to color using a colorbar. The colors are applied to the voxels before shading.
The following code demonstrates the new functionality:
The technical implementation is such that:
filledargumentPoly3DCollectionto a singlePoly3DCollection. This change is needed so that we can callfig.colorbar()on the return value.Poly3DCollectiongets a new member function.update_scalarmappable()which overloadsCollection.update_scalarmappable(). The new member function is needed to combine the color from the colormap with the shading. This has the additional impact that we can also allow other 3d surface plots to use both cmap and shading [ax.surface(),ax.plot_trisurf()].Next step
Before we move further with this I would like to have some discussion with the maintainers if it is OK to change the return type here. I think is is really important that the user is able to call
fig.colorbar()in an easy way, but there may be other ways to achieve this goal. It is probably a good idea to discuss this at a weekly developer meeting.If we choose to move on with this I will need to add an example to the docs, a release note, and I think it would also be prudent to add more testing.
NOTE on the modification of the test images:
The change in return type change impacts how the rendering is executed, which leads to a very minor change in how PNGs are rendered [notably, no differences are observed when exporting as SVG]. This change causes all existing image-comparison tests to fail. I believe the current behaviour is a [relatively insignificant] bug, and the changes in this PR improves the rendering quality ever so slightly.
The change is illustrated using the following test image:

Rendered with 300 DPI and zoomed in:


before:
after:
I have added a line as a guide to the eye, so that the difference is visible. The discrepancy becomes greater with lower DPI, but it is also more difficult to see what is going on if the DPI is lower.
PR checklist