NumPy Temperature Analysis – Simple
Examples
Suppose we have a NumPy array called `temps` that stores temperatures for 365 days and
24 hours each day (shape: `(365, 24)`).
———————————————————————————————————————
———————————
✅ Example 1: Maximum Temperature of Each Day
Code:
max_daily_temps = np.max(temps, axis=1)
What it does:
Finds the hottest temperature of each day.
`axis=1` tells NumPy to look across the 24 hours of each day.
Result:
An array of 365 values—one maximum temperature per day.
✅ Example 2: Count Days with Average Temp > 30 °C
Code:
avg_daily_temps = np.mean(temps, axis=1)
days_above_30 = np.sum(avg_daily_temps > 30)
What it does:
1. Computes the average temperature for each day.
2. Counts how many days had an average temperature above 30 °C.
Example Result:
125 (meaning 125 days were hotter than 30 °C).
✅ Example 3: Plot Min, Max, and Average Temperatures
Code:
import matplotlib.pyplot as plt
min_daily_temps = np.min(temps, axis=1)
max_daily_temps = np.max(temps, axis=1)
avg_daily_temps = np.mean(temps, axis=1)
plt.plot(min_daily_temps, label='Min')
plt.plot(max_daily_temps, label='Max')
plt.plot(avg_daily_temps, label='Avg')
plt.legend()
plt.title("Daily Temperatures")
plt.xlabel("Day")
plt.ylabel("Temperature (°C)")
plt.show()
What it does:
Draws a line graph showing daily minimum, maximum, and average temperatures.
Result:
Three curves that visually show temperature trends across the year.
✅ Example 4: Hottest Hour of the Year
Code:
hottest_temp = np.max(temps)
day, hour = np.unravel_index(np.argmax(temps), temps.shape)
What it does:
1. Finds the highest temperature recorded in the year.
2. Determines on which day and hour it happened.
Example Result:
“The hottest temperature was 40.00 °C on day 152 at 12:00 PM.”
💡 Why Use NumPy Instead of Python Lists?
Feature NumPy Python Lists
✅ Speed Vectorized operations are Slower—they use loops
much faster
✅ Memory More efficient for big Higher memory usage
datasets
✅ Functions Built-in `mean`, `max`, You’d need to implement
`sum`, etc. manually
✅ Integration Works smoothly with Less compatible with data
Matplotlib, Pandas, SciPy tools