Basic Visualization with ``matplotlib`` ======================================= Python’s most commonly used plotting library is `matplotlib `__. The library has an interface which mirrors that of Mathworks’ `Matlab `__ software, and so those with matlab familiarity will find themselves already high up on the learning curve. Loading ``matplotlib`` and setting up the notebook environment ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The matplotlib plotting library has a `magic `__ connection with the iPython shell and the notebook environment that allows static images of plots to be rendered in the notebook. Instead of using the normal ``import ...`` syntax, we’ll use this iPython ‘magic’ to not only import the library, but set up the environment we’ll need to create plots. .. code:: ipython3 %pylab inline .. parsed-literal:: Populating the interactive namespace from numpy and matplotlib Load data to plot ~~~~~~~~~~~~~~~~~ We’ll use the emissions data we saw before in the Pandas tutorial, as it’s familiar: .. code:: ipython3 import pandas as pd emissions = pd.read_csv('../../data/Climate/global_emissions.csv', skiprows=2, index_col='Year', names=['Year', 'Total Emissions', 'Gas Emissions', 'Liquid Emissions', 'Solid Emissions', 'Cement Emissions', 'Flare Emissions', 'Per Capita Emissions']) emissions.head(3) .. raw:: html
Total Emissions Gas Emissions Liquid Emissions Solid Emissions Cement Emissions Flare Emissions Per Capita Emissions
Year
1751 3 0 0 3 0 0 NaN
1752 3 0 0 3 0 0 NaN
1753 3 0 0 3 0 0 NaN
Basic plotting ~~~~~~~~~~~~~~ The basic plot command takes as its first two arguments the x and y values of the points which we wish to plot: .. code:: ipython3 plt.plot(emissions.index, emissions['Total Emissions']); .. image:: matplotlib_files/matplotlib_6_0.png Labeling axes and title ~~~~~~~~~~~~~~~~~~~~~~~ Following out plot command we can submit commands to `add text to the figure `__, such as adding labels to the x and y axes, and a title to the figure. .. code:: ipython3 plt.plot(emissions.index, emissions['Total Emissions']) plt.xlabel('Year') plt.ylabel('Million Metric Tons CO2/Year') plt.title('Historical CO2 Emissions', fontsize=18); .. image:: matplotlib_files/matplotlib_8_0.png Changing line properties ~~~~~~~~~~~~~~~~~~~~~~~~ We can include various elements into the plot command to `specify how the line will look `__: .. code:: ipython3 plt.plot(emissions.index, emissions['Total Emissions'], 'ro', alpha=.5); .. image:: matplotlib_files/matplotlib_10_0.png Specifying axis bounds ~~~~~~~~~~~~~~~~~~~~~~ We can specify that we want our plot to be bounded by various x and y values: .. code:: ipython3 plt.plot(emissions.index, emissions['Total Emissions']) plt.xlim(1950,2000) plt.ylim(1500,7500); .. image:: matplotlib_files/matplotlib_12_0.png Multiple lines ~~~~~~~~~~~~~~ We can add lines to our plot simply by adding additional calls to the plot function. Passing the plot function an argument called ‘label’ allows us to format a `legend `__ with appropriate references to each line: .. code:: ipython3 plt.plot(emissions.index, emissions['Liquid Emissions'], 'r', label='Liquid') plt.plot(emissions.index, emissions['Solid Emissions'], 'b', label='Solid') plt.plot(emissions.index, emissions['Gas Emissions'], 'g', label='Gas') plt.legend(loc='upper left'); .. image:: matplotlib_files/matplotlib_14_0.png Other plot types ~~~~~~~~~~~~~~~~ There are a number of other plot types available, such as histograms, radial plots, plots with logarithmic axes, or stackplots: .. code:: ipython3 plt.stackplot(emissions.index, [emissions['Liquid Emissions'], emissions['Gas Emissions'], emissions['Solid Emissions']], labels=['Liquid', 'Gas', 'Solid']) plt.legend(loc='upper left'); .. image:: matplotlib_files/matplotlib_16_0.png Saving figures ~~~~~~~~~~~~~~ We can save a figure to the disk by calling matplotlib’s ``savefig`` function: .. code:: ipython3 plt.plot(emissions.index, emissions['Total Emissions']) plt.savefig('Figure_1_Total_Emissions.png') .. image:: matplotlib_files/matplotlib_18_0.png Matplotlib and Pandas --------------------- Pandas uses matplot lib to provide a basic plotting interface of its own. The dataframe we have been working with has a convenience method called ``.plot()``, which assumes some basic format for how you would like your data presented, and tries to do so for you. This is handy when you are just interested in having a quick look at your data, without going to the trouble to create finished plots. .. code:: ipython3 emissions.plot(); .. image:: matplotlib_files/matplotlib_20_0.png The Dataframe’s wrapper of matplotlib gives us a number of basic options for how our plots are shown: .. code:: ipython3 emissions.plot(subplots=True, figsize=(10,6)); .. image:: matplotlib_files/matplotlib_22_0.png Matplotlib and PySD ------------------- As PySD returns a Pandas Dataframe, we can either use the plotting interface directly, or Pandas’s convenience wrapper. Here we’ll load a model which produces a chaotic output in three dimensions to use in our demonstration. .. code:: ipython3 import pysd model = pysd.read_vensim('../../models/Roessler_Chaos/roessler_chaos.mdl') res = model.run() res.head() .. raw:: html
FINAL TIME INITIAL TIME SAVEPER TIME STEP a b c dxdt dydt dzdt x y z
0.00000 100 0 0.03125 0.03125 0.2 0.2 5.7 -0.900000 0.600000 -1.880000 0.500000 0.500000 0.400000
0.03125 100 0 0.03125 0.03125 0.2 0.2 5.7 -0.860000 0.575625 -1.584098 0.471875 0.518750 0.341250
0.06250 100 0 0.03125 0.03125 0.2 0.2 5.7 -0.828485 0.552348 -1.333130 0.445000 0.536738 0.291747
0.09375 100 0 0.03125 0.03125 0.2 0.2 5.7 -0.804086 0.529910 -1.120680 0.419110 0.553999 0.250087
0.12500 100 0 0.03125 0.03125 0.2 0.2 5.7 -0.785624 0.508094 -0.941141 0.393982 0.570559 0.215065
Plotting vs. time. .. code:: ipython3 plt.plot(res.index, res['x'], 'r') plt.plot(res.index, res['y'], 'b') plt.plot(res.index, res['z'], 'g'); .. image:: matplotlib_files/matplotlib_26_0.png Plotting variables against one another .. code:: ipython3 plt.plot(res['x'], res['y']); .. image:: matplotlib_files/matplotlib_28_0.png While so far I have shown mostly basic, 2d plots, we can also call on `matplotlib’s 3d plotting engine `__ .. code:: ipython3 import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot(res['x'], res['y'], res['z']); .. image:: matplotlib_files/matplotlib_30_0.png Resources --------- - `Gallery `__ of different matplotlib graphics, showing what types of plots are possible. - Getting started with matplotlib `video series `__