Ranges of shared and unshared axes

Predict the output · Medium · §3.3 Shared axes and fair comparisons

This code draws the weekly output of three plants. The three Axes in the first figure share the y-axis, and the two Axes in the second figure share the x-axis. get_ylim() returns the lower and upper ends of the y-axis range currently shown.

Write what the code prints when you run it.

Code

import matplotlib.pyplot as plt

weeks = ["W1", "W2", "W3", "W4"]
plant_a = [410, 380, 450, 470]
plant_b = [220, 260, 240, 300]
plant_c = [150, 180, 130, 210]

fig, axes = plt.subplots(1, 3, sharey=True)
axes[0].bar(weeks, plant_a)
axes[1].bar(weeks, plant_b)
axes[2].bar(weeks, plant_c)
axes[2].set_ylim(0, 600)
low, high = axes[0].get_ylim()
print(int(low), int(high))

fig, axes = plt.subplots(2, 1, sharex=True)
axes[0].plot(weeks, plant_a)
axes[1].plot(weeks, plant_b)
axes[0].set_ylim(0, 600)
axes[1].set_ylim(0, 400)
low, high = axes[0].get_ylim()
print(int(low), int(high))

Solve this problem in your browser

Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.