Programming with Python · free preview
Visualizing Tabular Data
Plot simple graphs from data. Plot multiple graphs in a single figure.
1. Overview
Overview
2. Visualizing data
Visualizing data
3. Grouping plots
Grouping plots
4. Importing libraries with shortcuts
Importing libraries with shortcuts
5. Plot Scaling
Plot Scaling
6. Drawing Straight Lines
Drawing Straight Lines
7. Make Your Own Plot
Make Your Own Plot
8. Moving Plots Around
Moving Plots Around
9. Choose the middle subplot
A local Matplotlib script places three plots side by side. Which add_subplot position selects the middle plot?
10. Trace, three daily summaries in one figure
Run the complete local plotting program where matplotlib is installed, predict each subplot and axis, and check the numeric output before running the figure.
Example. Trace, three daily summaries in one figure
Problem. Run the complete local plotting program where matplotlib is installed, predict each subplot and axis, and check the numeric output before running the figure.
Correct solution.
Complete program.```python-local import numpy import matplotlib.pyplot
data = numpy.loadtxt(fname='data/inflammation-01.csv', delimiter=',')
fig = matplotlib.pyplot.figure(figsize=(10.0, 3.0))
axes1 = fig.add_subplot(1, 3, 1) axes2 = fig.add_subplot(1, 3, 2) axes3 = fig.add_subplot(1, 3, 3)
axes1.set_ylabel('average') axes1.plot(numpy.mean(data, axis=0))
axes2.set_ylabel('max') axes2.plot(numpy.max(data, axis=0))
axes3.set_ylabel('min') axes3.plot(numpy.min(data, axis=0))
fig.tight_layout() matplotlib.pyplot.savefig('daily-summaries.png') matplotlib.pyplot.show()
Browser-safe numeric check。
```python
print(numpy.mean(data, axis=0)[:3])
print(numpy.max(data, axis=0)[:3])Expected numeric output.```output [0. 0.45 1.11666667] [0. 1. 2.]
```
Trace.
loadtxtgives shape(60, 40).axis=0collapses the 60 rows and returns 40 daily values for each plot.figsize=(10.0, 3.0)supports one row of three plots.add_subplot(1, 3, i)places the axes at left, middle, and right.tight_layoutreduces clipping;savefigwrites the PNG file;showopens the figure.
关键提醒。
The figure uses axis=0 for daily summaries, add_subplot(rows, columns, index) for placement, and figsize for overall proportions.
11. Retrieval, correct a broken subplot layout
A new exercise script uses figsize=(3.0, 10.0) and fig.add_subplot(2, 3, 1), fig.add_subplot(2, 3, 2), fig.add_subplot(2, 3, 3). Explain why this does not produce three side-by-side plots and write the corrected figure size and three subplot calls.
Name the parameter order mistake and give exact replacement values.
12. Mastery, build a 2 by 2 summary figure
Use the new input readings = numpy.array([[10, 12, 14], [8, 9, 11], [7, 7, 7], [6, 5, 4]]) and write a local matplotlib script with four subplots showing the row-wise mean, row-wise maximum, row-wise minimum, and row-wise standard deviation. State the axis argument and the expected arrays.
Name each subplot, the shared axis argument, and the exact output arrays.
Practice this interactively
Free account · instant grading · spaced review that schedules itself.
Start this course for free