-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Tidying up and tweaking mplot3d examples [MEP12] #6690
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
Changes from 1 commit
885a82b
890e880
13bb609
3243b51
bfbf0ca
d47dcce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…nd refactoring. Notably, viewing angles on tricontour examples are tweaked and the graphs of trisurf3d_demo2 are made into subplots. [MEP12]
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,31 @@ | ||
""" | ||
Contour plots of unstructured triangular grids. | ||
Filled contour plots of unstructured triangular grids. | ||
|
||
The data used is the same as in the second plot of trisurf3d_demo2. | ||
tricontour3d_demo shows the unfilled version of this example. | ||
""" | ||
|
||
import matplotlib.pyplot as plt | ||
from mpl_toolkits.mplot3d import Axes3D | ||
import matplotlib.tri as tri | ||
import numpy as np | ||
import math | ||
|
||
# First create the x and y coordinates of the points. | ||
# First create the x, y, z coordinates of the points. | ||
n_angles = 48 | ||
n_radii = 8 | ||
min_radius = 0.25 | ||
radii = np.linspace(min_radius, 0.95, n_radii) | ||
|
||
angles = np.linspace(0, 2*math.pi, n_angles, endpoint=False) | ||
# Create the mesh in polar coordinates and compute x, y, z. | ||
radii = np.linspace(min_radius, 0.95, n_radii) | ||
angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False) | ||
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1) | ||
angles[:, 1::2] += math.pi/n_angles | ||
angles[:, 1::2] += np.pi/n_angles | ||
|
||
x = (radii*np.cos(angles)).flatten() | ||
y = (radii*np.sin(angles)).flatten() | ||
z = (np.cos(radii)*np.cos(angles*3.0)).flatten() | ||
|
||
# Create a custom triangulation | ||
# Create a custom triangulation. | ||
triang = tri.Triangulation(x, y) | ||
|
||
# Mask off unwanted triangles. | ||
|
@@ -30,7 +34,11 @@ | |
mask = np.where(xmid*xmid + ymid*ymid < min_radius*min_radius, 1, 0) | ||
triang.set_mask(mask) | ||
|
||
plt.figure() | ||
plt.gca(projection='3d') | ||
plt.tricontourf(triang, z) | ||
fig = plt.figure() | ||
ax = fig.gca(projection='3d') | ||
ax.tricontourf(triang, z, cmap=plt.cm.CMRmap) | ||
|
||
# Customize the view angle so it's easier to understand the plot. | ||
ax.view_init(elev=45.) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same color map and elevation tweaks as in tricontour3d example. |
||
|
||
plt.show() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,28 @@ | ||
''' | ||
Two additional examples of plotting surfaces with triangular mesh. | ||
|
||
The first demonstrates use of plot_trisurf's triangles argument, and the | ||
second sets a Triangulation object's mask and passes the object directly | ||
to plot_trisurf. | ||
''' | ||
|
||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
from mpl_toolkits.mplot3d import Axes3D | ||
import matplotlib.tri as mtri | ||
|
||
# u, v are parameterisation variables | ||
u = (np.linspace(0, 2.0 * np.pi, endpoint=True, num=50) * np.ones((10, 1))).flatten() | ||
v = np.repeat(np.linspace(-0.5, 0.5, endpoint=True, num=10), repeats=50).flatten() | ||
|
||
fig = plt.figure(figsize=plt.figaspect(0.5)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed this example to display the two plots side by side as subplots. Previously the code was only actually displaying one of them when run. |
||
|
||
#============ | ||
# First plot | ||
#============ | ||
|
||
# Make a mesh in the space of parameterisation variables u and v | ||
u = np.linspace(0, 2.0 * np.pi, endpoint=True, num=50) | ||
v = np.linspace(-0.5, 0.5, endpoint=True, num=10) | ||
u, v = np.meshgrid(u, v) | ||
u, v = u.flatten(), v.flatten() | ||
|
||
# This is the Mobius mapping, taking a u, v pair and returning an x, y, z | ||
# triple | ||
|
@@ -16,16 +33,18 @@ | |
# Triangulate parameter space to determine the triangles | ||
tri = mtri.Triangulation(u, v) | ||
|
||
fig = plt.figure() | ||
ax = fig.add_subplot(1, 1, 1, projection='3d') | ||
|
||
# The triangles in parameter space determine which x, y, z points are | ||
# connected by an edge | ||
# Plot the surface. The triangles in parameter space determine which x, y, z | ||
# points are connected by an edge. | ||
ax = fig.add_subplot(1, 2, 1, projection='3d') | ||
ax.plot_trisurf(x, y, z, triangles=tri.triangles, cmap=plt.cm.Spectral) | ||
|
||
ax.set_zlim(-1, 1) | ||
|
||
# First create the x and y coordinates of the points. | ||
|
||
#============ | ||
# Second plot | ||
#============ | ||
|
||
# Make parameter spaces radii and angles. | ||
n_angles = 36 | ||
n_radii = 8 | ||
min_radius = 0.25 | ||
|
@@ -35,6 +54,7 @@ | |
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1) | ||
angles[:, 1::2] += np.pi/n_angles | ||
|
||
# Map radius, angle pairs to x, y, z points. | ||
x = (radii*np.cos(angles)).flatten() | ||
y = (radii*np.sin(angles)).flatten() | ||
z = (np.cos(radii)*np.cos(angles*3.0)).flatten() | ||
|
@@ -45,11 +65,12 @@ | |
# Mask off unwanted triangles. | ||
xmid = x[triang.triangles].mean(axis=1) | ||
ymid = y[triang.triangles].mean(axis=1) | ||
mask = np.where(xmid*xmid + ymid*ymid < min_radius*min_radius, 1, 0) | ||
mask = np.where(xmid**2 + ymid**2 < min_radius**2, 1, 0) | ||
triang.set_mask(mask) | ||
|
||
# tripcolor plot. | ||
fig = plt.figure() | ||
ax = fig.add_subplot(1, 1, 1, projection='3d') | ||
# Plot the surface. | ||
ax = fig.add_subplot(1, 2, 2, projection='3d') | ||
ax.plot_trisurf(triang, z, cmap=plt.cm.CMRmap) | ||
|
||
|
||
plt.show() |
Uh oh!
There was an error while loading. Please reload this page.
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.
Use the same colour map as in trisurf3d_demo2 so it's easier for users to make the connection between that and this. I also raised the viewing elevation to make it a little more top down so it's easier to figure out the shape.