Data and Visualization with Python’s NumPy, Matplotlib, and SciPy

Python has become one of the top choices for developers and data scientists. Libraries like NumPy, Matplotlib, and SciPy provide tools for numerical computing, data visualization, and scientific analysis. This article explores seven real-world examples showcasing these libraries in action, explaining each step in detail.

Setting Up the Environment

Before diving into the code, create a virtual environment to keep the project dependencies isolated.

On Windows:

python -m venv venv
venv\Scripts\activate
pip numpy matplotlib scipy

On Linux:

python3 -m venv venv venv
source venv/bin/activate
pip numpy matplotlib scipy

1. Analyzing Daily Temperatures

This code uses NumPy to calculate basic statistics (mean, median, and standard deviation) for daily temperatures in Celsius. It then converts the results to Fahrenheit and rounds the values for better readability.

import numpy as np

# Simulated daily temperatures (in Celsius) for a month
temperatures = np.array([22, 21, 23, 25, 26, 27, 24, 22, 20, 19, 23, 24, 28, 30])

# Calculate basic statistics
mean_temp = np.mean(temperatures)
median_temp = np.median(temperatures)
std_dev_temp = np.std(temperatures)

# Round to two decimal places
mean_temp_rounded = round(mean_temp, 2)
median_temp_rounded = round(median_temp, 2)
std_dev_temp_rounded = round(std_dev_temp, 2)

# Convert to Fahrenheit (no rounding)
mean_temp_f = (mean_temp * 9/5) + 32
median_temp_f = (median_temp * 9/5) + 32
std_dev_temp_f = (std_dev_temp * 9/5) + 32

# Convert to Fahrenheit (rounded)
mean_temp_f_rounded = round(mean_temp_f, 2)
median_temp_f_rounded = round(median_temp_f, 2)
std_dev_temp_f_rounded = round(std_dev_temp_f, 2)

# Print the results
print("### Actual Numbers ###")
print(f"Mean Temperature (C): {mean_temp}")
print(f"Median Temperature (C): {median_temp}")
print(f"Standard Deviation (C): {std_dev_temp}")

print("\n### Rounded Numbers ###")
print(f"Mean Temperature (C, Rounded): {mean_temp_rounded}")
print(f"Median Temperature (C, Rounded): {median_temp_rounded}")
print(f"Standard Deviation (C, Rounded): {std_dev_temp_rounded}")

print("\n### Fahrenheit Conversion ###")
print(f"Mean Temperature (F): {mean_temp_f} (Rounded: {mean_temp_f_rounded})")
print(f"Median Temperature (F): {median_temp_f} (Rounded: {median_temp_f_rounded})")
print(f"Standard Deviation (F): {std_dev_temp_f} (Rounded: {std_dev_temp_f_rounded})")
  • Mean: The average value. Formula: mean = (sum of all temperatures) / (number of temperatures)
  • Median: The middle value when data is sorted.
  • Standard Deviation: Measures the spread of the data. Formula: std_dev = square root((sum of (each value — mean)²) / (number of values))
  • Fahrenheit Conversion: F = (C * 9/5) + 32

Result:

### Actual Numbers ###
Mean Temperature (C): 23.857142857142858
Median Temperature (C): 23.5
Standard Deviation (C): 2.996596709057576

### Rounded Numbers ###
Mean Temperature (C, Rounded): 23.86
Median Temperature (C, Rounded): 23.5
Standard Deviation (C, Rounded): 3.0

### Fahrenheit Conversion ###
Mean Temperature (F): 74.94285714285715 (Rounded: 74.94)
Median Temperature (F): 74.3 (Rounded: 74.3)
Standard Deviation (F): 37.39387407630364 (Rounded: 37.39)

2. Converting Images to Grayscale

Using NumPy and Pillow, this code fetches an image from a URL, converts it to grayscale, and save it locally. NumPy processes the color image into grayscale by applying a weighted formula that mimics human perception of brightness.

from PIL import Image
import numpy as np
import requests
from io import BytesIO

# URL of the image
image_url = 'https://images.pexels.com/photos/1181671/pexels-photo-1181671.jpeg'

# Fetch the image from the URL
response = requests.get(image_url)
if response.status_code == 200:
    image = Image.open(BytesIO(response.content))
else:
    raise Exception(f"Failed to fetch image. HTTP Status Code: {response.status_code}")

# Convert the image to a NumPy array
image_array = np.array(image)

# Convert to grayscale using weighted average
gray_image = np.dot(image_array[..., :3], [0.2989, 0.5870, 0.1140])

# Save the grayscale image
gray_image = Image.fromarray(gray_image.astype(np.uint8))
gray_image.save('example_grayscale.jpg')

print("Grayscale image saved as 'example_grayscale.jpg'")

Grayscale Formula: grayscale_value = (0.2989 * red) + (0.5870 * green) + (0.1140 * blue)

Result: The example_grayscale.jpg saved to your folder.

3. Data Normalization

This code rescales all values to a range between 0 and 1, preserving the relationships between data points. The goal is to ensure all features in a dataset have the same scale, which makes them comparable and prevents one feature from dominating others due to its larger magnitude.

import numpy as np

# Sample dataset
data = np.array([[2.5, 3.0], [1.0, 4.0], [3.5, 5.0]])

# Normalize data (min-max scaling)
data_min = np.min(data, axis=0)
data_max = np.max(data, axis=0)
normalized_data = (data - data_min) / (data_max - data_min)

print("Normalized Data:")
print(normalized_data)

Min-Max Scaling Formula: normalized_value = (value — min) / (max — min)

Result:

Normalized Data:
[[0.6 0. ]
 [0.  0.5]
 [1.  1. ]]

4. Stock Analysis with Moving Averages

This code calculates a 3-day simple moving average (SMA) for stock prices then smoothens price trends to make data patterns more apparent.

import numpy as np

# Simulated daily stock prices
prices = np.array([100, 102, 104, 103, 105, 110, 115])

# Calculate 3-day SMA
window = 3
sma = np.convolve(prices, np.ones(window)/window, mode='valid')

print("3-Day Simple Moving Average:")
print(sma)

SMA Formula: SMA = (price_1 + price_2 + price_3) / 3

Result:

3-Day Simple Moving Average:
[102. 103. 104. 106. 110.]

5. Analyzing Audio Frequencies

This code analyzes the frequency spectrum of an audio file using Fourier Transform. This output is a visualization of the frequency components of an audio signal.

import numpy as np
import matplotlib.pyplot as plt
from scipy.io import wavfile
import requests
from io import BytesIO

# URL of the audio file
audio_url = "https://github.com/schreibfaul1/ESP32-audioI2S/raw/master/additional_info/Testfiles/Pink-Panther.wav"

# Fetch the audio file from the URL
response = requests.get(audio_url)
if response.status_code == 200:
    audio_data = BytesIO(response.content)
else:
    raise Exception(f"Failed to fetch audio file. HTTP Status Code: {response.status_code}")

# Read the audio file
sample_rate, audio_data = wavfile.read(audio_data)

# Convert to mono if stereo
if audio_data.ndim > 1:
    audio_data = np.mean(audio_data, axis=1)

# Perform FFT to get frequency domain
frequencies = np.fft.rfftfreq(len(audio_data), d=1/sample_rate)
fft_magnitude = np.abs(np.fft.rfft(audio_data))

# Plot the frequency spectrum
plt.figure(figsize=(10, 6))
plt.plot(frequencies, fft_magnitude)
plt.title("Frequency Spectrum of Pink Panther Audio")
plt.xlabel("Frequency (Hz)")
plt.ylabel("Magnitude")
plt.grid()
plt.show()

FFT Formula: FFT transforms time-domain data into frequency-domain data, enabling analysis of sound frequencies.

Result is a Visualization:

Visual of Pink Panther Audio

6. Visualizing Sea Surface Temperatures

This code generates temperature patterns based on latitude, reflecting the natural phenomenon where regions near the equator are warmer than those closer to the poles. The resulting visualization highlights this expected temperature gradient.

import numpy as np
import matplotlib.pyplot as plt

# Simulated realistic SST data (degrees Celsius)
# Warmer temperatures near the equator (-10 to 10 latitude) and cooler near the poles
latitudes = np.linspace(-90, 90, 180)  # 180 latitude points
longitudes = np.linspace(-180, 180, 360)  # 360 longitude points

# Generate SST values based on latitude
sst_data = np.zeros((180, 360))
for i, lat in enumerate(latitudes):
    # Simulate temperature as a function of latitude
    # Warmer near equator, cooler near poles
    equator_temp = 28 - abs(lat) * 0.2  # Base temperature
    sst_data[i, :] = np.random.normal(equator_temp, 1.5, size=360)

# Compute global mean SST
mean_sst = np.mean(sst_data)

# Identify hotspots (SST > 28°C)
hotspots = sst_data > 28

# Visualize the data
plt.figure(figsize=(12, 6))
plt.imshow(sst_data, cmap='coolwarm', extent=[-180, 180, -90, 90], aspect='auto')
plt.colorbar(label="Sea Surface Temperature (°C)")
plt.title(f"Sea Surface Temperature Map (Mean SST: {mean_sst:.2f}°C)")
plt.xlabel("Longitude")
plt.ylabel("Latitude")
plt.show()

Temperature Formula: temperature = 28 — (latitude * 0.2)

Result is a Visualization:

Sea Surface Temperatures

7. Physics Simulation

The code models gravitational acceleration on multiple objects, updating their positions over time.

import numpy as np

# Initialize positions (x, y) and velocities (vx, vy) for 5 objects
positions = np.random.rand(5, 2) * 100  # Initial positions
velocities = np.zeros((5, 2))           # Initial velocities
gravity = np.array([0, -9.8])           # Gravity (m/s²)
time_step = 0.1                         # Time step (s)

# Simulate for 10 seconds
for _ in range(int(10 / time_step)):
    velocities += gravity * time_step
    positions += velocities * time_step

# Print final positions
print("Final Positions of Objects:")
print(positions)
  • Velocity Formula: velocity = velocity_initial + (gravity * time)
  • Position Formula: position = position_initial + (velocity * time)

Result:

Final Positions of Objects:
[[  66.47631175 -415.9807347 ]
 [  77.66233924 -473.14627517]
 [  50.04023763 -482.57367456]
 [  26.93396105 -453.48578856]
 [  79.99651167 -414.04736297]]

Thank you for reading this article. I hope you found it helpful and informative. If you have any questions, or if you would like to suggest new Python code examples or topics for future tutorials, please feel free to reach out. Your feedback and suggestions are always welcome!

Happy coding!
C. C. Python Programming

You can also find this article at Medium.com

Leave a Reply