Overview of Popular Python Libraries for Data Visualization and Image Processing
This article introduces a collection of widely used Python libraries—including Matplotlib, Pillow, NumPy, scikit-image, OpenCV, Seaborn, Plotly, Bokeh, Pycairo, and SimpleCV—detailing their core functionalities, typical use cases, and providing ready‑to‑run code examples for data visualization and computer‑vision tasks.
Matplotlib is a data‑visualization library that creates static charts such as line, bar, scatter, histogram, and pie plots, supporting subplots, color mapping, legends, and annotations. Example code:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
plt.figure(figsize=(8, 6))
plt.plot(x, y)
plt.title('Sine of Squared X')
plt.xlabel('X', fontsize=14)
plt.ylabel('Y', fontsize=14)
plt.grid(True)
plt.show()PIL/Pillow is an image‑processing library for opening, editing, and saving many image formats, offering pixel‑level operations, cropping, rotating, scaling, color‑space conversion, and filters. Example code:
from PIL import Image, ImageDraw, ImageFont
img = Image.new('RGB', (500, 300), color='white')
draw = ImageDraw.Draw(img)
font = ImageFont.truetype('arial.ttf', size=50)
text = "Hello, World!"
text_width, text_height = font.getsize(text)
draw.text((img.width // 2 - text_width // 2, img.height // 2 - text_height // 2), text=text, fill='black', font=font)
img.save('hello_world.png')NumPy provides efficient multi‑dimensional array objects (ndarray) that form the basis for numerical computation and image data handling. Example code:
import numpy as np
import matplotlib.pyplot as plt
img_array = np.random.randint(0, 256, size=(5, 5), dtype=np.uint8)
plt.imshow(img_array, cmap='gray')
plt.colorbar()
plt.title('Random 5x5 Image Array')
plt.show()scikit-image (skimage) is a computer‑vision library offering algorithms for filtering, edge detection, morphology, segmentation, feature extraction, and color‑space conversion. Example code:
from skimage import io
from skimage.filters import sobel
from matplotlib import pyplot as plt
image = io.imread('example.jpg', as_gray=True)
edges = sobel(image)
fig, ax = plt.subplots(1, 2, figsize=(10, 5))
ax[0].imshow(image, cmap='gray')
ax[0].set_title('Original Image')
ax[1].imshow(edges, cmap='gray')
ax[1].set_title('Sobel Edge Detection')
plt.tight_layout()
plt.show()OpenCV is a high‑performance computer‑vision library for image/video processing, object detection, tracking, stereo vision, and machine‑learning algorithms. Example code:
import cv2
import numpy as np
image = cv2.imread('example.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 100, 200)
cv2.imshow('Original Image', image)
cv2.imshow('Canny Edge Detection', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()Seaborn builds on Matplotlib to provide aesthetically pleasing statistical visualizations such as heatmaps, boxplots, violin plots, and joint distributions. Example code:
import seaborn as sns
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
data = pd.DataFrame({
'x': np.random.normal(size=100),
'y': np.random.normal(size=100),
'class': np.repeat(['A', 'B'], 50)
})
sns.scatterplot(data=data, x='x', y='y', hue='class', palette=['red', 'blue'])
plt.title('Seaborn Scatterplot with Class Coloring')
plt.show()Plotly creates interactive 2D/3D charts, maps, and dashboards that can be embedded in web pages or exported as HTML. Example code:
import plotly.graph_objs as go
import numpy as np
t = np.linspace(0, 2 * np.pi, 100)
x = np.cos(t)
y = np.sin(t)
fig = go.Figure(data=[
go.Scatter(x=t, y=x, name='Cosine'),
go.Scatter(x=t, y=y, name='Sine')
])
fig.update_layout(title='Plotly Interactive Plot', xaxis_title='Time', yaxis_title='Value')
fig.show()Bokeh produces high‑performance interactive visualizations for large datasets, outputting HTML that runs in modern browsers with real‑time updates. Example code:
from bokeh.plotting import figure, show
from bokeh.io import output_notebook
import numpy as np
output_notebook()
t = np.linspace(0, 2*np.pi, 400)
x = np.cos(t)
y = np.sin(t)
p = figure(title='Bokeh Interactive Plot', plot_width=600, plot_height=400)
p.line(t, x, legend_label='cosine', line_width=2)
p.line(t, y, legend_label='sine', line_color='orange', line_width=2)
show(p)Pycairo provides Python bindings to the Cairo vector‑graphics library, enabling creation of SVG, PDF, and PNG files with lossless scaling. Example code:
import cairo
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 200, 200)
ctx = cairo.Context(surface)
ctx.set_source_rgb(1, 1, 1)
ctx.rectangle(0, 0, surface.get_width(), surface.get_height())
ctx.fill()
ctx.set_source_rgb(0, 0, 0)
ctx.select_font_face('Sans')
ctx.set_font_size(32)
ctx.move_to(50, 50)
ctx.show_text('Hello, Pycairo!')
surface.write_to_png('hello_pycairo.png')SimpleCV wraps OpenCV, PIL, and NumPy to offer a beginner‑friendly API for rapid prototyping of computer‑vision tasks such as image capture, preprocessing, feature detection, and object recognition. Example code:
from SimpleCV import Camera, Display
cam = Camera()
disp = Display()
while True:
img = cam.getImage()
gray_img = img.grayscale()
gray_img.show(disp)
if disp.isDone():
break
disp.destroy()How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
