BACK TO HOME PAGE

Graph module

Description

This module is a front-end to matplotlib. Matplotlib built-in front-end is very powerful but lacks the following features:

Moreover, this module provides a simple object-oriented API to edit the graphs and curves.

Syntax

This module defines the following objects:

Curve objects

To create a curve object, use the following syntax:

curve = Curve(data, color = 'auto', marker='auto', label='auto')

The ‘data’ argument can be either:

The ‘color’ argument can be any color string understood by matplotlib. Possible colors include:

The ‘marker’ agument can be any marker string understood by matplotlib. Possible markers include:

Graph objects

To build a Graph object, the syntax is the following:

graph = Graph(list_of_curves, xmin = 'auto', xmax = 'auto', ymin = 'auto', ymax= 'auto', xlabel = '', ylabel = '', title = '', nb_pts = 1000):

with the following arguments:

A Graph object will not be displayed unless you ask for it. This is done with the ‘show’ method:

graph.show()

Examples

Quick plot

Quickly plotting a function (without having to build sampled data):

from graph import *
def myfunc(x): return 3*x**3+2

quickGraph(myfunc,2,10) # show plot of f between xmin = 2 and xmax = 10

This will display the following image. Notice the automatic label.

This function also works with DataArrays defined in module Arrays, using the following syntax:

quickGraph(my_data_array) 

Units of my_data_array will be displayed on the labels of the graph.

Re-using a curve in several graphs

We have a function and a set of data. We want to build two graphs :

def myfunc(x): return 3*x**3+2

curve_1 = Curve(myfunc, color = 'b')

graph_1 = Graph([curve_1], xlabel = "Abscissa")
graph_1.show()

X = np.array([1,2,3,4,5])
Y = X**3
curve_2 = Curve((X,Y), marker = '-*', color='r', label = "some data")

graph_2 = Graph([curve_1, curve_2])
graph_2.show()

This code will display the following graphs:

graph_1

Object-oriented API

When a graph has been built, it is easy to modify one single item of the graph and display it again. For instance, if we want to modify the last graph by adding a title and changing the y-scale:

graph_2.ymax = 200
graph_2.title = "Awesome graph"
graph_2.show()

This results in the following graph:

BACK TO HOME PAGE