# Plotting functions or data sets with units¶

NOTES:

This module defines wrappers for some Sage plotting functions that allow quantities with dimensions to be plotted:

Basically, the wrapper checks dimensional consistency (e.g. when plotting several functions on the same graph or when plotting lists of data), finds a unit for each axis if the user hasn’t given one, and then scales the data and functions and hands on the dimensionless values to the corresponding Sage functions.

AUTHOR:

• Miriam Backens (2009): initial version

EXAMPLES:

Plot the intensity of black body radiation according to Planck’s law at temperatures 6000 K, 5000 K and 3500 K:

sage: from sage.dimpy import *
sage: I6000 = lambda l: 2*h_*c**2*l**-5 / (exp(h_*c/(l*k*6000*kelvin)) - 1)
sage: I5000 = lambda l: 2*h_*c**2*l**-5 / (exp(h_*c/(l*k*5500*kelvin)) - 1)
sage: I3500 = lambda l: 2*h_*c**2*l**-5 / (exp(h_*c/(l*k*3500*kelvin)) - 1)
sage: unit_plot([I6000, I5000, I3500], (10*nano*meter, 2000*nano*meter), xscale='nm', yscale='TW/m^3', xlabel='$\lambda$', ylabel='$I$')


Given a set of experimental data, you can create a scatter plot or a simple list plot, or you can create a list plot where the points are joined by lines.

A scatter plot of the masses and orbital periods of some extrasolar planets discovered by the satellite CoRoT:

sage: data = [(1.03*m_J, 1.5089557*day), (3.31*m_J, 1.7429964*day), (0.72*m_J, 9.20205*day), (0.467*m_J, 4.0378962*day), (3.3*m_J, 8.89*day), (0.035*m_J, 0.853585*day)]
sage: unit_scatter_plot(data, xlabel='mass', ylabel='period', xscale=m_J, yscale=day)


A list plot of measurements of the position of an accelerating body:

sage: data_acc = [(0*second, 0*meter), (1/4*second, 0.3*meter),  (1/2*second, 1.3*meter), (3/4*second, 2.8*meter),  (1*second, 4.5*meter)]
sage: unit_list_plot(data_acc, True, xlabel='time', ylabel='position')

sage.dimpy.plot.unit_bar_chart(data, *args, **kwargs)

Create a bar chart out of quantities.

INPUT:

• data - a list of quantities
• keyword argument yscale to set the units in which the quantities should be displayed, these can either be Quantities or parseable strings, (default: combination of SI units)
• keyword arguments xlabel and ylabel set the description on the labels (the units are added automatically to the y label, default: ‘’)
• any other input that is legal for sage.plot.bar_chart.bar_chart

OUTPUT: a Sage Graphics object

EXAMPLE:

sage: from sage.dimpy import *
sage: unit_bar_chart([0*kilogram, 60*kilogram, 75*kilogram, 53*kilogram, 81*kilogram])

sage.dimpy.plot.unit_list_plot(data, plotjoined=False, **kwargs)

Plot a list of quantities.

INPUT:

• data - a list of tuples of quantities or a list of quantities (in the latter case, the x components are set to 1, 2, 3, ...)
• plotjoined - a bool, whether the data points shoul be joined by lines
• keyword arguments xscale and yscale to set the units in which the quantities should be displayed, these can either be Quantities or parseable strings, (default: combination of SI units)
• keyword arguments xlabel and ylabel set the description on the labels (the units are added automatically, default: ‘’)
• any other input that is legal for sage.plot.plot.list_plot

OUTPUT: a Sage Graphics object

EXAMPLES:

sage: from sage.dimpy import *
sage: unit_list_plot([1*meter, 2*meter, 3*meter, 4*meter, 5*meter])

sage: unit_list_plot([(0*second, 1*meter), (0.5*second, 2*meter), (0.9*second, 3*meter), (1.6*second, 4*meter), (2.3*second, 5*meter)], True)

sage.dimpy.plot.unit_parametric_plot(func, *args, **kwargs)

Plot functions of quantities parametrically.

INPUT:

• func - two functions, the first one giving the x coordinates, the second the y coordinates depending on a parameter t
• a tuple giving the minimum and maximum values for t, or the two values as separate arguments
• keyword arguments xscale and yscale to set the units in which the quantities should be displayed, these can either be Quantities or parseable strings, (default: combination of SI units)
• keyword arguments xlabel and ylabel to set the description on the labels (the units are added automatically, default: ‘’)
• any other input that is legal for sage.plot.plot.parametric_plot

OUTPUT: a Sage Graphics object

EXAMPLE:

sage: from sage.dimpy import *
sage: unit_parametric_plot([lambda t: abs(t), lambda t: sqrt(meter**2 - t**2)], (0*meter, 1*meter))

sage.dimpy.plot.unit_plot(func, *args, **kwargs)

Plot function(s) where input and output are Quantities.

INPUT:

• func - a function or a list of functions (these should actually be callables, i.e. either lambda functions or proper Python functions)
• a tuple giving the minimum and maximum values for the independent variable (can also be given as two separate values)
• keyword arguments xscale and yscale to set the units in which the quantities should be displayed, these can either be Quantities or parseable strings, (default: combination of SI units)
• keyword arguments xlabel and ylabel to set the description on the labels (the units are added automatically, default: ‘’)
• any other input that is legal for sage.plot.plot.plot

OUTPUT: a Sage Graphics object

NOTES:

Use the scale factors wisely - the plotting program dislikes values that are too big or too small.

EXAMPLES:

sage: from sage.dimpy import *
sage: s = lambda t: g_n/2*t**2
sage: unit_plot(s, (0*second, 2*second))


Dimensional consistency is checked:

sage: unit_plot(s, (0*second, 2*meter))
...
DimensionMismatchError: Cannot plot values, dimensions were (s, ) (m, )


We can include text for the axes labels:

sage: unit_plot(s, (0*second, 2*second), xlabel='time', ylabel='distance')


It is also possible to rescale the axes:

sage: unit_plot(s, (0*second, 2*second), xscale=minute, yscale=foot)


Plotting multiple functions:

sage: a = lambda x: x**2
sage: b = lambda x: x**2/2
sage: c = lambda x: x**2*2
sage: unit_plot([a, b, c], (0*meter, 3*meter))

sage.dimpy.plot.unit_scatter_plot(data, *args, **kwargs)

Create a scatter plot of data with units.

INPUT:

• data - a list of tuples or lists of data
• keyword arguments xscale and yscale to set the units in which the quantities should be displayed, these can either be Quantities or parseable strings, (default: combination of SI units)
• keyword arguments xlabel and ylabel set the description on the labels (the units are added automatically, default: ‘’)
• any other input that is legal for sage.plot.scatter_plot.scatter_plot

OUTPUT: a Sage Graphics object

EXAMPLE:

sage: from sage.dimpy import *
sage: set = [(1*second, 3*meter), (1.5*second, 5*meter), (2*second, 9*meter)]
sage: unit_scatter_plot(set)


All elements of the data list must have the same dimensions:

sage: unit_scatter_plot([(0*meter, 0*second), (1*meter, 1*second), (2*meter, 4*second), (0.5*meter, 0.25*kilogram)])
...
DimensionMismatchError: Invalid data point (number 3, y coordinate): expected (s, ), dimensions were (kg, )


#### Previous topic

Interface between Sage and the QuantityParser