Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Solutions numpy & matplotlib

Technische Universität Berlin

Task 1: Create an array with hourly demands 200, 220, 240, 260, 280, and 300 MW. Print its shape and datatype.

Notebook Cell
Notebook Cell
Notebook Cell
(6,)
Notebook Cell
dtype('int64')

Task 2: Create eleven evenly spaced temperature values between 15 and 25 degrees Celsius using np.linspace().

Notebook Cell
array([15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25.])

Task 3: Print the first 3 values and the last 3 values of the following array:

demand = np.array([300, 320, 340, 360, 380, 400, 420, 440, 460, 480])
Notebook Cell
Notebook Cell
array([300, 320, 340])
Notebook Cell
array([440, 460, 480])

Task 4: Convert the following temperature array to a heating energy demand using the formula demand = 1000 - 20 * temperature.

temperature = np.array([10, 12, 15, 20, 25])
Notebook Cell
Notebook Cell
array([800, 760, 700, 600, 500])

Task 5: You have daily demand demand data for 3 days and 3 hours

demand = np.array([[300, 320, 340],
                    [280, 300, 310],
                    [260, 270, 290]])

Multiply each hour (columns) by scaling factors 0.9, 1.0, and 1.1 respectively using broadcasting. What are the dimensions and values of the resulting array? What do the new values represent?

Notebook Cell
Notebook Cell
Notebook Cell
array([[270., 320., 374.], [252., 300., 341.], [234., 270., 319.]])
Notebook Cell
(3, 3)

Task 6: Consider the following values of hourly wind speeds in m/s.

wind_speeds = np.array([5, 10, 15, 20, 25])

Calculate the average wind speed, minimum wind speed, and maximum wind speed using reduction operations.

Notebook Cell
Notebook Cell
np.float64(15.0)
Notebook Cell
np.int64(5)
Notebook Cell
np.int64(25)

Task 7: Consider the following values for demand and generation for 8 hours.

demand = np.array([400, 420, 450, 480, 500, 520, 550, 580])
generation = np.array([300, 350, 400, 450, 480, 500, 530, 600])

Create two line plots on the same figure showing both demand and generation over time. Add appropriate labels and a legend.

Notebook Cell
Notebook Cell
Notebook Cell
<Figure size 640x480 with 1 Axes>

Task 7: Sometimes we want to compare two different variables side by side. Use subplots to visualize the following demand and price time series over six hours.

demand = np.array([300, 340, 320, 380, 360, 400])
price = np.array([55, 50, 65, 60, 75, 70])

Create one figure with two subplots arranged in one row and two columns. The first subplot should show demand over time, and the second subplot should show price over time. Add appropriate titles and labels to each subplot and enable grid lines. Create another separate figure that presents the relation between demand and price using a scatter plot.

Notebook Cell
Notebook Cell
<Figure size 1000x400 with 2 Axes>
Notebook Cell
<Figure size 640x480 with 1 Axes>