Machine Learning
39.2K subscribers
3.83K photos
32 videos
41 files
1.3K links
Machine learning insights, practical tutorials, and clear explanations for beginners and aspiring data scientists. Follow the channel for models, algorithms, coding guides, and real-world ML applications.

Admin: @HusseinSheikho || @Hussein_Sheikho
Download Telegram
Topic: Python Matplotlib – From Easy to Top: Part 6 of 6: 3D Plotting, Animation, and Interactive Visuals

---

### 1. Introduction

Matplotlib supports advanced visualizations including:

3D plots using mpl_toolkits.mplot3d
Animations with FuncAnimation
Interactive plots using widgets and event handling

---

### 2. Creating 3D Plots

You need to import the 3D toolkit:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np


---

### 3. 3D Line Plot

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

z = np.linspace(0, 15, 100)
x = np.sin(z)
y = np.cos(z)

ax.plot3D(x, y, z, 'purple')
ax.set_title("3D Line Plot")
plt.show()


---

### 4. 3D Surface Plot

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

X = np.linspace(-5, 5, 50)
Y = np.linspace(-5, 5, 50)
X, Y = np.meshgrid(X, Y)
Z = np.sin(np.sqrt(X**2 + Y**2))

surf = ax.plot_surface(X, Y, Z, cmap='viridis')
fig.colorbar(surf)

ax.set_title("3D Surface Plot")
plt.show()


---

### 5. 3D Scatter Plot

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x = np.random.rand(100)
y = np.random.rand(100)
z = np.random.rand(100)

ax.scatter(x, y, z, c=z, cmap='plasma')
ax.set_title("3D Scatter Plot")
plt.show()


---

### 6. Creating Animations

Use FuncAnimation for animated plots.

import matplotlib.animation as animation

fig, ax = plt.subplots()
x = np.linspace(0, 2*np.pi, 128)
line, = ax.plot(x, np.sin(x))

def update(frame):
line.set_ydata(np.sin(x + frame / 10))
return line,

ani = animation.FuncAnimation(fig, update, frames=100, interval=50)
plt.title("Sine Wave Animation")
plt.show()


---

### 7. Save Animation as a File

ani.save("sine_wave.gif", writer='pillow')


Make sure to install pillow using:

pip install pillow


---

### 8. Adding Interactivity with Widgets

import matplotlib.widgets as widgets

fig, ax = plt.subplots()
plt.subplots_adjust(left=0.1, bottom=0.25)

x = np.linspace(0, 2*np.pi, 100)
freq = 1
line, = ax.plot(x, np.sin(freq * x))

ax_slider = plt.axes([0.25, 0.1, 0.65, 0.03])
slider = widgets.Slider(ax_slider, 'Frequency', 0.1, 5.0, valinit=freq)

def update(val):
line.set_ydata(np.sin(slider.val * x))
fig.canvas.draw_idle()

slider.on_changed(update)
plt.title("Interactive Sine Wave")
plt.show()


---

### 9. Mouse Interaction with Events

def onclick(event):
print(f'You clicked at x={event.xdata:.2f}, y={event.ydata:.2f}')

fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 5, 6])
fig.canvas.mpl_connect('button_press_event', onclick)
plt.title("Click to Print Coordinates")
plt.show()


---

### 10. Summary

3D plots are ideal for visualizing spatial data and surfaces
Animations help convey dynamic changes in data
Widgets and events add interactivity for data exploration
• Mastering these tools enables the creation of interactive dashboards and visual storytelling

---

### Exercise

• Plot a 3D surface of z = cos(sqrt(x² + y²)).
• Create a slider to change frequency of a sine wave in real-time.
• Animate a circle that rotates along time.
• Build a 3D scatter plot of 3 correlated variables.

---

#Python #Matplotlib #3DPlots #Animations #InteractiveVisuals #DataVisualization

https://t.iss.one/DataScienceM
3