3D Heart Plots with Python's Matplotlib: Prepare to be Amazed!
in case you're a person who loves information visualization and desires to take it to the subsequent stage, you're in for a treat! Python's Matplotlib library offers a extensive range of alternatives for creating lovely visualizations, which includes 3-D plots. In this article, we are able to discover how to create charming 3D coronary heart plots the usage of Matplotlib. Get prepared to be surprised with the aid of the consequences!
What is Matplotlib?
Before we dive into the fascinating world of 3D heart plots, let's briefly understand what Matplotlib is. Matplotlib is a powerful plotting library that allows you to create a wide variety of static, animated, and interactive visualizations in Python. It offers a high level of customization, making it a favorite among data scientists, engineers, and researchers. ;[
Plotting the Heart
Now comes the exciting part – plotting the heart! We can use mathematical equations and the power of NumPy to generate the coordinates of the heart shape. By plotting these coordinates onto our 3D axes, we'll create a stunning visualization that resembles a heart in three dimensions.
import numpy as np
import matplotlib.pyplot as plt
from skimage import measure
# Set up mesh
n = 100
x = np.linspace(-3, 3, n)
y = np.linspace(-3, 3, n)
z = np.linspace(-3, 3, n)
X, Y, Z = np.meshgrid(x, y, z)
# Create cardioid function
def f_heart(x, y, z):
F = 320 * ((-x**2 * z**3 - 9*y**2 * z**3/80) +
(x**2 + 9*y**2/4 + z**2-1)**3)
return F
# Obtain value at every point in mesh
vol = f_heart(X, Y, Z)
# Extract a 2D surface mesh from a 3D volume (F=0)
verts, faces, normals, values = measure.marching_cubes(vol, 0, spacing=(0.1, 0.1, 0.1))
# Create a 3D figure
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(111, projection='3d')
# Plot the surface
ax.plot_trisurf(verts[:, 0], verts[:, 1], faces, verts[:, 2],
cmap='Spectral', lw=1)
# Change the angle of view and title
ax.view_init(15, -15)
ax.set_title("Made with <3 (and Python)", fontsize=15)
# Show the plot
plt.show()
OUTPUT:
HAPPY CODING!!!....
0 Comments