Making A Single Plot#

This page focuses on creating a single plot, such as generating a scatter plot on a single axis.

Making a plot from Data#

Basic usage#

The general method for generating a plot is by using the plots() method, as demonstrated in the example below:

from pyttop.table import Data

data = Data(name='data')
data['x'] = [1, 2, 3]
data['y'] = [2, 4, 7]

data.plots(
    'scatter', # the plotting function used
    cols=('x', 'y'), # the columns used as inputs
    marker='*', color='black', # more options for the plotting function
    );
../_images/eee87d2991213a3c439d2d5dfcc1718d6067dc05951d09d9b28b006e9e7963ce.png

Where:

  • 'scatter' specifies the type of plot, i.e., the name of the plotting function.

  • cols specifies the columns to be used as positinoal arguments for the plotting function;

  • marker and color are additional arguments passed to the plotting function.

In this example, the effect is similar to:

import matplotlib.pyplot as plt
plt.scatter(data['x'], data['y'], marker='*', color='black')

However, the x- and y-axis labels are set automatically, and the title ‘All’ indicates that all data rows within the table are shown on the plot.

The column data can also be passed as keyword arguments:

data['m'] = [0, 3, 2]
data.plots(
    'scatter',
    cols=('x', 'y'),
    kwcols={'c': 'm'},
    marker='*', 
    );
../_images/6a73077ec54cc6c8c60f7bbad3e041af8d93bc3239248eaca90d06b4b24f0182.png

Which is similar to:

plt.scatter(data['x'], data['y'], c=data['m'], marker='*')

In PyTTOP, a colorbar and its label are also automatically generated.

As shown, the plots() method allows you to simply specify the column names to be plotted, and the corresponding column data will be automatically passed to the plotting function (e.g., scatter). The type of plot and the available arguments are defined by the plotting function itself. You can use any of the built-in plotting functions, as introduced here, or define your own plotting functions by following the instructions here.

Enabling expression evaluation#

You can also provide expressions that can be evaluated by the eval() method (see the description here) instead of column names by setting eval=True:

data.plots(
    'scatter',
    cols=('x', 'y**2'), eval=True,
    marker='*', 
    );
../_images/3090d95d38c0055072c90dd6a3d11820c60b3357ce11999a1b4ccc2bf0a0585a.png

If you need to pass additional arguments to the eval() method, you can provide them via eval_kwargs:

data.plots(
    'scatter',
    cols=('x', 'a * y**2'), 
    eval=True, eval_kwargs={'a': -1},
    marker='*', 
    );
../_images/c7e8f0eba62d88b9c2ecf5bc59ab26bd32ea56bfeb1520c1c65ad8a8c47cd4d6.png

Built-in basic plotting functions#

In data.plots(), you can specify any of the built-in plotting functions by its name, such as 'hist'. However, if you would like to access the plotting function directly, you can import it from pyttop.plot:

from pyttop.plot import hist

plot#

This is the plt.plot counterpart in PyTTOP, and the available arguments are similar to those of plt.plot.

data.plots(
    'plot',
    cols=('x', 'y'),
    linestyle='--', marker='o',
    );
../_images/52bdebf5690d32caae30104a78acc7475ee2437d8cc24d647047226291206892.png

scatter#

This is the plt.scatter counterpart in PyTTOP, and the available arguments are similar to those of plt.scatter.

data.plots(
    'scatter',
    cols=('x', 'y'),
    kwcols={'c': 'm'},
    s=[10, 30, 50],
    marker='o',
    );
../_images/c72e687e78c3daeb966dab3025c38809a62e9be2e3797d5db88d44c42b2ed456.png

A colorbar is automatically generated by default. To suppress colorbar generation, set autobar=False:

data.plots(
    'scatter',
    cols=('x', 'y'),
    kwcols={'c': 'm'},
    s=[10, 30, 50],
    marker='o',
    autobar=False,
    );
../_images/10bf0c1cfea91479a011890f809f04c2ba8dbf2f823cb0e9451c81767af95ffa.png

hist#

This is the plt.hist counterpart in PyTTOP, and the available arguments are similar to those of plt.hist.

data.plots(
    'hist',
    cols=('x'),
    );
../_images/bb9d36caa3cdcb1205c8d46846d79e5c628d3199f9427dd9f9198358f9f35e15.png

hist2d#

This is the plt.hist2d counterpart in PyTTOP, and the available arguments are similar to those of plt.hist2d.

errorbar#

This is the plt.errorbar counterpart in PyTTOP, and the available arguments are similar to those of plt.errorbar.

data.plots(
    'errorbar',
    cols=('x', 'y'),
    kwcols={'yerr': 'm'},
    marker='o', markersize=10,
    );
../_images/a662334d6fb747d54dd4176548d2f25aa19a38269cc19183c9e22e5866cb48d5.png

Convenient plotting methods#

PyTTOP offers several convenient methods to further simplify the code for generating plots. The methods and examples are listed below. Note that the default plot settings may differ from those used with data.plots(<...>).

data.scatter()#

This method simplifies using data.plots('scatter', <...>). For example:

data.scatter('x', 'y', c='y*2', s='y*10', eval=True, marker='o');
../_images/0e7600ba452614f25c49b45c1fdeda74f0708391d96ac6d2878b7736bbd0715e.png

Note that if the value of the c argument appears as a color string (e.g., 'r' for red), it will be interpreted as a color rather than a column name.

data.hist()#

This method simplifies using data.plots('hist', <...>). For example:

data.hist('x');
../_images/053d858b0e5773b8764f7f07b6111d16a06658ed9a3a9939db9d18487d057e28.png