from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np
def heart_3d(x, y, z):
return (
**2 + (9 / 4) * y**2 + z**2 - 1) ** 3 - x**2 * z**3 - (9 / 80) * y**2 * z**3
(x
)
= (-1.5, 1.5)
bbox
= bbox * 3
xmin, xmax, ymin, ymax, zmin, zmax = plt.figure()
fig
= fig.add_subplot(111, projection="3d")
ax = np.linspace(xmin, xmax, 100) # resolution of the contour
A = np.linspace(xmin, xmax, 40) # number of slices
B = np.meshgrid(A, A) # grid on which the contour is plotted
A1, A2
for z in B: # plot contours in the XY plane
= A1, A2
X, Y = heart_3d(X, Y, z)
Z = ax.contour(X, Y, Z + z, [z], zdir="z", colors=("r",))
cset
for y in B: # plot contours in the XZ plane
= A1, A2
X, Z = heart_3d(X, y, Z)
Y = ax.contour(X, Y + y, Z, [y], zdir="y", colors=("r",))
cset
for x in B: # plot contours in the YZ plane
= A1, A2
Y, Z = heart_3d(x, Y, Z)
X = ax.contour(X + x, Y, Z, [x], zdir="x", colors=("r",))
cset
= ax.set_zlim3d(zmin, zmax);
_ = ax.set_xlim3d(xmin, xmax);
_ = ax.set_ylim3d(ymin, ymax);
_
1.0, 1.0, 1.0, 0.0))
ax.xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax.yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax.zaxis.set_pane_color((
plt.show()
Let’s celebrate Valentine’s Day with some nerdy love! In this post, we will create heart-shaped plots using Python, R, and Matlab. These heart-shaped plots are a fun and creative way to express your love for programming and data visualization. Let’s get started!
Python
Here is a Python code snippet to plot a 3D heart shape using Matplotlib:
R
In R, we can use the rgl
and misc3d
packages to create 3D mesh plots. Here is the R code to plot a heart shape:
options(rgl.useNULL = TRUE) # Use NULL device if display is not available
library(misc3d)
library(rgl)
<- function(x, y, z) {
heart_3d ^2 + (9 / 4) * y^2 + z^2 - 1)^3 - x^2 * z^3 - (9 / 80) * y^2 * z^3
(x
}
# Create a grid of points in 3D space
<- seq(-1.5, 1.5, length.out = 50)
x <- seq(-1.5, 1.5, length.out = 50)
y <- seq(-1.5, 1.5, length.out = 50)
z
# Generate 3D grid of function values
<- expand.grid(x = x, y = y, z = z)
grid <- with(grid, heart_3d(x, y, z))
values
# Reshape to 3D array for contour3d
<- c(length(x), length(y), length(z))
dim_values <- array(values, dim = dim_values)
values
# Create 3D contour plot
contour3d(values,
level = 0,
x = x, y = y, z = z, col.mesh = "red", alpha = 0.5, engine = "grid"
)
Matlab
The code for plotting 3D shape in Matlab is much more laconic than in Python or R. Here is how to plot a heart shape in Matlab:
% volume data
step = 0.05;
X,Y,Z] = meshgrid(-3:step:3, -3:step:3, -3:step:3);
[F = (-(X.^2).*(Z.^3)-(9/80).*(Y.^2).*(Z.^3))+((X.^2)+(9/4).*(Y.^2)+(Z.^2)-1).^3;
% wireframe
patch(isosurface(X,Y,Z,F,0), 'FaceColor','w', 'EdgeColor','r')
daspect([1 1 1])
view(3)
axis tight equal
set(gcf, 'Color','w')
Now you have three beautiful heart-shaped plots created using Python, R, and Matlab. Share these plots with your loved ones and spread the nerdy love this Valentine’s Day!