PART 3 — Advanced Visualization & 3D Plots

Python tutorial · PySpark.in

3D PLOTS

Used to visualize data in three dimensions (X, Y, Z) using projection='3d'.

Import

```

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

import numpy as np

```

3D Line Plot

Shows a continuous 3D curve, useful for trajectories and time-series in 3D.

```

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

import numpy as np

fig = plt.figure()

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

t = np.linspace(0, 10, 100)

x = np.sin(t)

y = np.cos(t)

z = t

ax.plot3D(x, y, z)

plt.show()

```

3D Scatter Plot

Displays individual 3D data points good for clusters and distributions.

```

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

import numpy as np

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.scatter3D(x, y, z)

plt.show()

```

3D Surface Plot

Creates a smooth 3D surface from matrix values, used for mathematical surfaces and heat-topography representation.

```

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

import numpy as np

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(X) * np.cos(Y)

ax.plot_surface(X, Y, Z)

plt.show()

```

Polar Plot

Plots data in circular coordinates instead of Cartesian coordinates. Best for angles, waves, signal processing, antenna patterns, etc.

Example

```

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

import numpy as np

theta = np.linspace(0, 2*np.pi, 100)

r = np.sin(2 * theta)

plt.polar(theta, r)

plt.show()

```

Violin Plot

Shows data distribution, density, and probability shape similar to boxplot but more detailed. Useful for comparing spread and skewness of multiple datasets.

Example

```

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

import numpy as np

data = [np.random.normal(0, std, 200) for std in range(1, 5)]

plt.violinplot(data)

plt.show()

```

Stem Plot

Displays data as vertical lines with markers as ideal for discrete signals, sampling theory, impulse functions.

```

import matplotlib.pyplot as plt

import numpy as np

x = np.linspace(0, 10, 20)

y = np.sin(x)

plt.stem(x, y)

plt.show()

```

Mathematical Function Plotting

Plots mathematical expressions by generating values using NumPy (sin, cos, exp, sinh, etc.). Important for scientific and engineering visualization.

```

import matplotlib.pyplot as plt

import numpy as np

x = np.linspace(-10, 10, 400)

y = np.sinh(x)

plt.plot(x, y)

plt.title("y = sinh(x)")

plt.show()

```

Multiple Figures

Creates separate windows/figures in the same script. Useful when working with different plots independently.

```

import matplotlib.pyplot as plt

plt.figure(1)

plt.plot([1,2,3], [2,4,6])

plt.figure(2)

plt.plot([1,2,3], [1,4,9])

plt.show()

```

Log Scale Plot

Converts axes into logarithmic scale using yscale("log") or xscale("log"). Helpful when values grow exponentially (e.g., population, signal magnitude, ML loss).

```

import matplotlib.pyplot as plt

import numpy as np

x = np.linspace(1, 100, 100)

y = x**2

plt.plot(x, y)

plt.yscale("log")

plt.show()

```

Animations (FuncAnimation)

Creates dynamic, real-time updating plots. Used for simulations, live data, algorithm visualization, and time-series animation.

Import

```

import matplotlib.pyplot as plt

from matplotlib.animation import FuncAnimation

import numpy as np

```

Example

```

import matplotlib.pyplot as plt

from matplotlib.animation import FuncAnimation

import numpy as np

fig, ax = plt.subplots()

x = []

y = []

line, = ax.plot([], [])

def update(frame):

x.append(frame)

y.append(np.sin(frame))

line.set_data(x, y)

ax.relim()

ax.autoscale_view()

return line,

anim = FuncAnimation(fig, update, frames=np.linspace(0, 10, 100))

plt.show() ```

Advanced Color Maps

Maps numeric values to colors and visualizes intensity using cmap. Essential for heatmaps, density plots, 3D surfaces, and scientific imaging.

```

import matplotlib.pyplot as plt

import numpy as np

x = np.random.rand(100)

y = np.random.rand(100)

colors = np.random.rand(100)

plt.scatter(x, y, c=colors, cmap='viridis')

plt.colorbar()

plt.show()

```

Advanced Styling (Seaborn-like themes)

Applies pre-designed global styles for professional and attractive visuals.
Example styles include 'ggplot', 'seaborn', 'dark_background', etc.

```

import matplotlib.pyplot as plt

plt.style.use('ggplot')

plt.plot([1,2,3,4], [10,20,25,30])

plt.show()

```

Contour Plot

Displays curves connecting equal values (iso-lines) over a 2D grid. Common for terrain maps, electromagnetic fields, mathematical equation visualization.

```

import matplotlib.pyplot as plt

import numpy as np

x = np.linspace(-5, 5, 50)

y = np.linspace(-5, 5, 50)

X, Y = np.meshgrid(x, y)

Z = np.sin(X) * np.cos(Y)

plt.contour(X, Y, Z)

plt.colorbar()

plt.show()

```

Image Display (imshow)

Shows 2D image matrices or grayscale/rgb images using Matplotlib. Often used for deep learning, CNN image preprocessing, heat images, medical images.

```

import matplotlib.pyplot as plt

import numpy as np

img = np.random.rand(100, 100)

plt.imshow(img, cmap='gray')

plt.colorbar()

plt.show()

```

More Python tutorials

All tutorials · Try the free PySpark compiler · Practice challenges