2024

The Basics: plt.plot vs. fig, ax = plt.subplots()

  • plt.plot: A convenient way to create figures and axes implicitly for quick and simple plots.

  • fig, ax = plt.subplots(): Provides explicit control over figures and axes, ideal for complex and customized visualizations.

plt-level Customization

  • plt-level functions affect the current Axes in the current Figure, often used in the pyplot API for quick adjustments.
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [4, 5, 6])
plt.xticks([1, 2, 3], ['One', 'Two', 'Three'])
plt.yticks([4, 5, 6], ['Four', 'Five', 'Six'])
plt.show()

ax-level Customization

  • ax-level functions specifically target specific axes.
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 5, 6])
ax.set_xticks([1, 2, 3])
ax.set_xticklabels(['One', 'Two', 'Three'])
ax.set_yticks([4, 5, 6])
ax.set_yticklabels(['Four', 'Five', 'Six'])
plt.show()

Key Differences

  • Scope of Impact: plt-level functions affect the current or most recent figure or axes, while ax-level methods target specific axes objects.

  • Control: ax-level provides more precise control for customizing plots, especially when working with multiple subplots.

Conclusion

  • No practice exercises here.

  • Just wanted to clarify some of the different syntax we have seen along the way.