Skip to content

Ternary plotting functionality for use with matplotlib including heatmaps.

License

Notifications You must be signed in to change notification settings

Swanson-Hysell/python-ternary

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

python-ternary

This is a plotting library for use with matplotlib to make ternary plots, plots in the two dimensional simplex projected onto a two dimensional plane.

The library provides functions for plotting projected lines, curves (trajectories), and heatmaps.

Basic Plotting Functions

Most ternary functions expect the simplex to be partititioned into some number of steps. A few functions will do this partitioning for you, but when working with real data or simulation output, you may have partitioned already.

Simplex Boundary and Gridlines

The following code draws a boundary for the simplex and gridlines.

from matplotlib import pyplot
import ternary

steps = 30

ax = ternary.draw_boundary(steps)
ternary.draw_gridlines(steps, ax=ax)
ax.set_title("Simplex Boundary and Gridlines")

pyplot.show()

Drawing lines

You can draw individual lines between any two points with draw_line and lines parallel to the axes with draw_horizonal_line, draw_left_parallel_line, and draw_right_parallel_line:

from matplotlib import pyplot

import ternary
steps = 30

ax = ternary.draw_boundary(steps)
ternary.draw_horizontal_line(ax, steps, 10)
ternary.draw_left_parallel_line(ax, steps, 15, linewidth=2., color='red')
ternary.draw_right_parallel_line(ax, steps, 15, linewidth=3., color='blue')

ax.set_title("Various Lines")

pyplot.show()

The line drawing functions accept the matplotlib keyword arguments of Line2D

Curves

Curves can be plotted by specifying the points of the curve, just like matplotlib's plot. Simply use:

ternary.plot(points)

Points is a list of tuples or numpy arrays, e.g. [(0.5, 0.25, 0.25), (1./3, 1./3, 1//3)]. Ternary assumes that the points are probability distributions (e.g. x+y+z=1) unless you specify otherwise. Again you can specify axes and line options:

ternary.plot(points, ax=ax, steps=100, linewidth=2.0)

There are many more examples in this paper.

Heatmaps

Ternary can plot heatmaps in two ways and two styles. Given a function, ternary will evaluate the function at the specified number of steps. The simplex can be split up into triangles or hexagons (thanks to contributor btweinstein for the hexagonal heatmap functionality). There is a large set of examples here.

For example:

def shannon_entropy(p):
    """Computes the Shannon Entropy at a distribution in the simplex."""
    s = 0.
    for i in range(len(p)):
        try:
            s += p[i] * math.log(p[i])
        except ValueError:
            continue
    return -1.*s

We can get a heatmap of this function as follows:

from matplotlib import pyplot

import ternary
pyplot.figure()
ax = ternary.function_heatmap(func, steps=steps, boundary=True, style="triangular")
ternary.draw_boundary(steps, ax=ax)
ax.set_title("Shannon Entropy Heatmap")

pyplot.show()

In this case the keyword argument boundary indicates whether you wish to evaluate points on the boundary of the partition (which is sometimes undesirable). Specify style="hexagonal" for hexagons.

Ternary can also take a dictionary mapping (i,j) for i + j + k = steps to a float as input for a heatmap, using the function

ternary.heatmap(d, steps, cmap_name=None, ax=None, scientific=False)

You may specify a matplotlib colormap in the cmap_name argument.

Citation

Please cite as follows:

Marc Harper, Python-ternary: A python library for ternary plots, 2011, available at: https://github.com/marcharper/python-ternary

Contributors

About

Ternary plotting functionality for use with matplotlib including heatmaps.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%