| """ |
| .. redirect-from:: /tutorials/introductory/lifecycle |
| |
| ======================= |
| The Lifecycle of a Plot |
| ======================= |
| |
| This tutorial aims to show the beginning, middle, and end of a single |
| visualization using Matplotlib. We'll begin with some raw data and |
| end by saving a figure of a customized visualization. Along the way we try |
| to highlight some neat features and best-practices using Matplotlib. |
| |
| .. currentmodule:: matplotlib |
| |
| .. note:: |
| |
| This tutorial is based on |
| `this excellent blog post |
| <https://pbpython.com/effective-matplotlib.html>`_ |
| by Chris Moffitt. It was transformed into this tutorial by Chris Holdgraf. |
| |
| A note on the explicit vs. implicit interfaces |
| ============================================== |
| |
| Matplotlib has two interfaces. For an explanation of the trade-offs between the |
| explicit and implicit interfaces see :ref:`api_interfaces`. |
| |
| In the explicit object-oriented (OO) interface we directly utilize instances of |
| :class:`axes.Axes` to build up the visualization in an instance of |
| :class:`figure.Figure`. In the implicit interface, inspired by and modeled on |
| MATLAB, we use a global state-based interface which is encapsulated in the |
| :mod:`.pyplot` module to plot to the "current Axes". See the :ref:`pyplot |
| tutorials <pyplot_tutorial>` for a more in-depth look at the |
| pyplot interface. |
| |
| Most of the terms are straightforward but the main thing to remember |
| is that: |
| |
| * The `.Figure` is the final image, and may contain one or more `~.axes.Axes`. |
| * The `~.axes.Axes` represents an individual plot (not to be confused with |
| `~.axis.Axis`, which refers to the x-, y-, or z-axis of a plot). |
| |
| We call methods that do the plotting directly from the Axes, which gives |
| us much more flexibility and power in customizing our plot. |
| |
| .. note:: |
| |
| In general, use the explicit interface over the implicit pyplot interface |
| for plotting. |
| |
| Our data |
| ======== |
| |
| We'll use the data from the post from which this tutorial was derived. |
| It contains sales information for a number of companies. |
| |
| """ |
|
|
| import matplotlib.pyplot as plt |
| |
| import numpy as np |
|
|
| data = {'Barton LLC': 109438.50, |
| 'Frami, Hills and Schmidt': 103569.59, |
| 'Fritsch, Russel and Anderson': 112214.71, |
| 'Jerde-Hilpert': 112591.43, |
| 'Keeling LLC': 100934.30, |
| 'Koepp Ltd': 103660.54, |
| 'Kulas Inc': 137351.96, |
| 'Trantow-Barrows': 123381.38, |
| 'White-Trantow': 135841.99, |
| 'Will LLC': 104437.60} |
| group_data = list(data.values()) |
| group_names = list(data.keys()) |
| group_mean = np.mean(group_data) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| fig, ax = plt.subplots() |
|
|
| |
| |
|
|
| fig, ax = plt.subplots() |
| ax.barh(group_names, group_data) |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| print(plt.style.available) |
|
|
| |
| |
|
|
| plt.style.use('fivethirtyeight') |
|
|
| |
| |
|
|
| fig, ax = plt.subplots() |
| ax.barh(group_names, group_data) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| fig, ax = plt.subplots() |
| ax.barh(group_names, group_data) |
| labels = ax.get_xticklabels() |
|
|
| |
| |
| |
| |
|
|
| fig, ax = plt.subplots() |
| ax.barh(group_names, group_data) |
| labels = ax.get_xticklabels() |
| plt.setp(labels, rotation=45, horizontalalignment='right') |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| plt.rcParams.update({'figure.autolayout': True}) |
|
|
| fig, ax = plt.subplots() |
| ax.barh(group_names, group_data) |
| labels = ax.get_xticklabels() |
| plt.setp(labels, rotation=45, horizontalalignment='right') |
|
|
| |
| |
| |
| |
|
|
| fig, ax = plt.subplots() |
| ax.barh(group_names, group_data) |
| labels = ax.get_xticklabels() |
| plt.setp(labels, rotation=45, horizontalalignment='right') |
| ax.set(xlim=[-10000, 140000], xlabel='Total Revenue', ylabel='Company', |
| title='Company Revenue') |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| fig, ax = plt.subplots(figsize=(8, 4)) |
| ax.barh(group_names, group_data) |
| labels = ax.get_xticklabels() |
| plt.setp(labels, rotation=45, horizontalalignment='right') |
| ax.set(xlim=[-10000, 140000], xlabel='Total Revenue', ylabel='Company', |
| title='Company Revenue') |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| def currency(x, pos): |
| """The two arguments are the value and tick position""" |
| if x >= 1e6: |
| s = f'${x*1e-6:1.1f}M' |
| else: |
| s = f'${x*1e-3:1.0f}K' |
| return s |
|
|
| |
| |
| |
| |
|
|
| fig, ax = plt.subplots(figsize=(6, 8)) |
| ax.barh(group_names, group_data) |
| labels = ax.get_xticklabels() |
| plt.setp(labels, rotation=45, horizontalalignment='right') |
|
|
| ax.set(xlim=[-10000, 140000], xlabel='Total Revenue', ylabel='Company', |
| title='Company Revenue') |
| ax.xaxis.set_major_formatter(currency) |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| fig, ax = plt.subplots(figsize=(8, 8)) |
| ax.barh(group_names, group_data) |
| labels = ax.get_xticklabels() |
| plt.setp(labels, rotation=45, horizontalalignment='right') |
|
|
| |
| ax.axvline(group_mean, ls='--', color='r') |
|
|
| |
| for group in [3, 5, 8]: |
| ax.text(145000, group, "New Company", fontsize=10, |
| verticalalignment="center") |
|
|
| |
| ax.title.set(y=1.05) |
|
|
| ax.set(xlim=[-10000, 140000], xlabel='Total Revenue', ylabel='Company', |
| title='Company Revenue') |
| ax.xaxis.set_major_formatter(currency) |
| ax.set_xticks([0, 25e3, 50e3, 75e3, 100e3, 125e3]) |
| fig.subplots_adjust(right=.1) |
|
|
| plt.show() |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| print(fig.canvas.get_supported_filetypes()) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
|
|