-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add the ability to change the focal length of the camera for 3D plots #22046
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
WeatherGod
merged 6 commits into
matplotlib:main
from
scottshambaugh:3d_plot_focal_length
Jan 20, 2022
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e8a3f57
Specify the focal length for 3d plots
scottshambaugh 81f3a23
Merge branch 'main' into 3d_plot_focal_length
scottshambaugh c1e5e0a
Code review changes
scottshambaugh de45ae1
Merge branch 'main' into 3d_plot_focal_length
scottshambaugh 7bd9bd9
More code review changes and tests
scottshambaugh b0135c7
Additional code review changes
scottshambaugh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
Give the 3D camera a custom focal length | ||
---------------------------------------- | ||
|
||
Users can now better mimic real-world cameras by specifying the focal length of | ||
the virtual camera in 3D plots. The default focal length of 1 corresponds to a | ||
Field of View (FOV) of 90 deg, and is backwards-compatible with existing 3D | ||
plots. An increased focal length between 1 and infinity "flattens" the image, | ||
while a decreased focal length between 1 and 0 exaggerates the perspective and | ||
gives the image more apparent depth. | ||
|
||
The focal length can be calculated from a desired FOV via the equation: | ||
|
||
.. mathmpl:: | ||
|
||
focal\_length = 1/\tan(FOV/2) | ||
|
||
.. plot:: | ||
:include-source: true | ||
|
||
from mpl_toolkits.mplot3d import axes3d | ||
import matplotlib.pyplot as plt | ||
fig, axs = plt.subplots(1, 3, subplot_kw={'projection': '3d'}, | ||
constrained_layout=True) | ||
X, Y, Z = axes3d.get_test_data(0.05) | ||
focal_lengths = [0.25, 1, 4] | ||
for ax, fl in zip(axs, focal_lengths): | ||
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10) | ||
ax.set_proj_type('persp', focal_length=fl) | ||
ax.set_title(f"focal_length = {fl}") | ||
plt.tight_layout() | ||
plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
""" | ||
======================== | ||
3D plot projection types | ||
======================== | ||
|
||
Demonstrates the different camera projections for 3D plots, and the effects of | ||
changing the focal length for a perspective projection. Note that Matplotlib | ||
corrects for the 'zoom' effect of changing the focal length. | ||
|
||
The default focal length of 1 corresponds to a Field of View (FOV) of 90 deg. | ||
An increased focal length between 1 and infinity "flattens" the image, while a | ||
decreased focal length between 1 and 0 exaggerates the perspective and gives | ||
the image more apparent depth. In the limiting case, a focal length of | ||
infinity corresponds to an orthographic projection after correction of the | ||
zoom effect. | ||
|
||
You can calculate focal length from a FOV via the equation: | ||
|
||
.. mathmpl:: | ||
|
||
1 / \tan (FOV / 2) | ||
|
||
Or vice versa: | ||
|
||
.. mathmpl:: | ||
|
||
FOV = 2 * \atan (1 / focal length) | ||
|
||
""" | ||
|
||
from mpl_toolkits.mplot3d import axes3d | ||
import matplotlib.pyplot as plt | ||
|
||
|
||
fig, axs = plt.subplots(1, 3, subplot_kw={'projection': '3d'}) | ||
|
||
# Get the test data | ||
X, Y, Z = axes3d.get_test_data(0.05) | ||
|
||
# Plot the data | ||
for ax in axs: | ||
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10) | ||
|
||
# Set the orthographic projection. | ||
axs[0].set_proj_type('ortho') # FOV = 0 deg | ||
axs[0].set_title("'ortho'\nfocal_length = ∞", fontsize=10) | ||
|
||
# Set the perspective projections | ||
axs[1].set_proj_type('persp') # FOV = 90 deg | ||
axs[1].set_title("'persp'\nfocal_length = 1 (default)", fontsize=10) | ||
|
||
axs[2].set_proj_type('persp', focal_length=0.2) # FOV = 157.4 deg | ||
axs[2].set_title("'persp'\nfocal_length = 0.2", fontsize=10) | ||
|
||
plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+63.1 KB
lib/mpl_toolkits/tests/baseline_images/test_mplot3d/axes3d_focal_length.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Uh oh!
There was an error while loading. Please reload this page.