Description of MatplotlibFigureField class

class django_matplotlib.fields.MatplotlibFigureField(*args, **kwargs)

Matplotlib figure field for Django.

Figures are generated in ‘png’ (default) or ‘svg’ formats. They can be inserted to html documents as inline objects (e.g. using <img src=”data:image/png;base64,…”>) or saved to temporary files.

If files are saved on disk, they are automatically cleaned up when the program exits using atexit module.

MatplotlibFigureField is compatible with standard Django Admin app.

Figures automatically re-render (at any subsequent request) when they code is changed. It uses hashlib.md5() function to check changes in figure’s code. If the figure wasn’t changed, it would be stored in memory and underlying figure view function (which returns matplotlib.Figure instance) not be called for each subsequent request.

Note

Model fields created using this class aren’t stored in the database and no additional columns for such fields are created.

__init__(*args, **kwargs)

MatplotlibFigureField initializer.

This field can take any of standard Django’s field attributes, but forces required argument to False for corresponding form.

Parameters:
  • figure (bool) – The name of callable within figures.py which should return matplotlib.Figure object.
  • silent – Be silent on exceptions or not (default is False).
  • plt_args (tuple) – Positional arguments passed to figure’s view (the function defined by the figure parameter).
  • plt_kwargs (dict) – Keyword arguments passed to figure’s view (the function defined by the figure parameter).
  • fig_width (int) – Output figure width in pixels. Default is 320.
  • fig_height (int) – Output figure height in pixels. Default is 240.
  • output_type (str) – Output type of the figure. Either ‘file’ or ‘string’. Default is ‘string’ (used for inline figure object embedding to html pages).
  • output_format (str) – Output format of the figure. Either ‘svg’ or ‘png’ (default).
  • cleanup (bool) – Defines whether created files be cleaned up at program exit or not. Default is True (created files will be erased at exit). Has sense only if output_type=’file’.

Note

Default parameters for MatplotlibFigureField are defined in the file conf.py.

Note

If output_type=’file’, MEDIA_ROOT should be defined in your project settings file. In this case, the field will save temporary files to the folder MEDIA_ROOT/DJANGO_MATPLOTLIB_TMP. Default value of DJANGO_MATPLOTLIB_TMP is defined in conf.py and can be overridden in your project settings file.