{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# 3D plot projection types\n\nDemonstrates the different camera projections for 3D plots, and the effects of\nchanging the focal length for a perspective projection. Note that Matplotlib\ncorrects for the 'zoom' effect of changing the focal length.\n\nThe default focal length of 1 corresponds to a Field of View (FOV) of 90 deg.\nAn increased focal length between 1 and infinity \"flattens\" the image, while a\ndecreased focal length between 1 and 0 exaggerates the perspective and gives\nthe image more apparent depth. In the limiting case, a focal length of\ninfinity corresponds to an orthographic projection after correction of the\nzoom effect.\n\nYou can calculate focal length from a FOV via the equation:\n\n\\begin{align}1 / \\tan (\\mathrm{FOV} / 2)\\end{align}\n\nOr vice versa:\n\n\\begin{align}\\mathrm{FOV} = 2 \\arctan (1 / \\mathrm{focal length})\\end{align}\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n\nfrom mpl_toolkits.mplot3d import axes3d\n\nfig, axs = plt.subplots(1, 3, subplot_kw={'projection': '3d'})\n\n# Get the test data\nX, Y, Z = axes3d.get_test_data(0.05)\n\n# Plot the data\nfor ax in axs:\n ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)\n\n# Set the orthographic projection.\naxs[0].set_proj_type('ortho') # FOV = 0 deg\naxs[0].set_title(\"'ortho'\\nfocal_length = \u221e\", fontsize=10)\n\n# Set the perspective projections\naxs[1].set_proj_type('persp') # FOV = 90 deg\naxs[1].set_title(\"'persp'\\nfocal_length = 1 (default)\", fontsize=10)\n\naxs[2].set_proj_type('persp', focal_length=0.2) # FOV = 157.4 deg\naxs[2].set_title(\"'persp'\\nfocal_length = 0.2\", fontsize=10)\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. tags::\n plot-type: 3D,\n styling: small-multiples,\n component: subplot,\n level: intermediate\n\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }