Make Figures Comparable and Reusable
Preserve labels, units, fair scales, and the source context needed to compare a figure now and reproduce it later.
A figure carries numerical evidence beyond the program that created it. A reader should be able to tell what was measured, which units were used, and whether two visible distances mean the same numerical change. The program must also save the intended figure and release it when its work is done.
We will compare three stages of the recurring measurement calculation:
All three tables have axes (observations, sensors). Their values are raw
readings, calibrated readings, and differences from each sensor's calibrated
mean. We will measure each quantity in degrees Celsius. The numerical context
belongs in the text and labels; a plot should not make the reader recover it
from the source code.
Label the Quantities Being Compared
Create the figure and its two axes explicitly:
fig owns the complete figure. axes[0] and axes[1] own their individual
panels. Calling methods on those objects makes the target of each operation
visible:
“Reading” names the vertical quantity, while “°C” supplies its unit. “Observation” explains the horizontal positions. Labels such as “value” and “index” would leave too much unstated.
A legend is useful when one panel contains more than one series and their identity is not already clear from direct labels. Here each panel contains two sensors, so a legend has work to do:
Markers and line styles distinguish the sensors in addition to color. The comparison remains readable in grayscale and for readers who do not perceive the colors as distinct. A legend for a single line named completely by the title and axis labels would add little.
Q1. Choose labels and a useful legend
Which setup gives enough context for two sensor lines measured in degrees Celsius over observation number?
Select one choice, then check.
HintImagine the code is unavailable
The exported image should still say what each position means and which line belongs to which sensor.
SolutionName quantities, units, and series
Use Observation and Reading (°C) as axis labels. Give each sensor a
label and show the legend because two series share the panel.
Keep Numerical Scales Comparable
If raw and calibrated values are placed in separate panels, unrelated vertical
limits can make equal numerical changes look different. sharey=True gives
both panels the same vertical scale. We can also set the intended limits once:
The shared range means the same vertical distance represents the same number of degrees in both panels. It does not prove that the calibration is correct; the numerical calculation and its checks supply that evidence.
Other plot forms need the same fairness in their own terms:
- histogram comparisons need the same bin edges, not merely the same number of bins;
- heatmap comparisons need the same
vminandvmax, and the same color meaning; - bar and line comparisons need compatible axis limits and scales;
- every comparison needs matching quantities and units before shared limits can be meaningful.
Q2. Preserve a fair visual comparison
Two histograms compare readings from two sensors. Two heatmaps compare a table before and after a transformation. Which setup supports direct comparison?
Select one choice, then check.
HintMatch numerical boundaries
Ask what determines a bar's count in a histogram and what determines a cell's color in a heatmap.
SolutionShare bins and color limits
Pass the same bin-edge array to both histograms. Pass the same vmin and
vmax to both heatmaps.
The following executable example puts all three stages together. Its printed limits are numerical evidence that the panels share a scale, while the panel labels distinguish readings from differences around a mean.
Compare raw, calibrated, and centered readings
All panels use the same observation positions and numerical scale. Labels distinguish readings from centered differences, and sensor identity uses marker and line style as well as color.
Ready to run.
Shared limits support a comparison of numerical magnitudes; they do not turn the three quantities into one quantity. Zero in a centered panel means the sensor's calibrated mean, while zero in a reading panel means a zero reading. The titles and axis labels preserve that distinction.
Save the Intended Figure, Then Close It
An output path should be explicit. Create its directory, save through the known figure object, then close that same object:
fig.savefig(...) cannot accidentally select another active figure. The path
states where the file belongs, and the .png extension selects the file
format. plt.close(fig) releases this figure from pyplot after the export is
complete. Saving must come first.
In Browser Python, the path belongs to the temporary browser-side file system for that page session. In a local program, it is resolved from the program's working directory unless an absolute path is used. Keep the plotting code, source data, transformation, limits, and output path together so the image can be recreated rather than treated as unexplained evidence.
Q3. Label, save, and close one comparison
Complete the program so both panels share vertical limits, carry quantity and unit labels, save to the stated path, and close the intended figure.
Editable Python
Ready to run.
HintUse the objects that own the work
Set sharey=True. Label the left axis Reading (°C) and the right axis
Difference from sensor mean (°C). Save with fig.savefig(output_path, ...),
then call plt.close(fig).
SolutionKeep comparison and lifecycle explicit
A reusable figure keeps its numerical context: quantities, units, series identity, and fair scales. Use shared bins or color limits when those define the comparison, and distinguish series by more than color alone. Construct through known axes, save through the owning figure, and close that figure after the export succeeds.
References
- Matplotlib documentation:
pyplot.subplots— figure and axes creation, including shared axes. - Matplotlib documentation:
Figure.savefig— saving a specific figure to a path. - Matplotlib documentation:
pyplot.close— closing a specific figure.