CPlot.Decorator: decoration of CPlot images using Matplotlib

Preamble

CPlot can generate images of meshes or flow fields. Those image can be further enhanced with any additional Matplotlib items using this module.

This module is part of Cassiopee, a free open-source pre- and post-processor for CFD simulations.

To import CPlot.Decorator module:

import CPlot.Decorator as Decorator

List of functions

– Actions

CPlot.Decorator.getInfo2DMode

Toggle 2D mode for CPlot display.

CPlot.Decorator.getImage

Read image from file.

CPlot.Decorator.createSubPlot

Create a subplot figure.

CPlot.Decorator.createColorBar

Create a color bar.

CPlot.Decorator.createArrow

Draw an arrow on figure.

CPlot.Decorator.savefig

Save current figure in a file.

CPlot.Decorator.show

Show current figure.

CPlot.Decorator.closeAll

Close all figures.

Contents

Actions

CPlot.Decorator.getInfo2DMode(xlim, ylim, zplane, ppw)

Get the CPlot camera and export parameters to emulate a 2D projection mode.

Parameters:
  • xlim (list of two floats [xmin, xmax]) – x-axis view limits.

  • ylim (list of two floats [ymin, ymax]) – y-axis view limits.

  • zplane (float) – constant-z position of the 2D plane.

  • ppw (float) – pixels per width.

Returns:

posCam, posEye, dirCam, viewAngle, and exportResolution CPlot.display parameters.

Example of use:

import Converter.PyTree as C
import Generator.PyTree as G
import CPlot.PyTree as CPlot
import CPlot.Decorator as Decorator

offscreen = 2
Decorator.setBatch(True)

a = G.cart((0,0,0), (0.01,0.01,1), (201,101,1))
C._initVars(a, '{F} = {CoordinateX}*{CoordinateY}')

ppw = 1000 # pixels per height
zplane = 0.0 # constant-z value
xlim = [1.0, 2.0] # xmin, xmax
ylim = [0.5, 1.0] # ymin, ymax
posCam, posEye, dirCam, viewAngle, exportResolution = Decorator.getInfo2DMode(xlim, ylim, zplane, ppw)

CPlot.display(a, mode='Scalar', scalarField='F',
    isoScales=['F',25,0.5,1.9], 
    colormap=24, # Jet colorbar
    export=CPlot.decorator, # Plot.decorator = '.decorator.png'
    offscreen=offscreen, 
    viewAngle=viewAngle,
    posCam=posCam, posEye=posEye, dirCam=dirCam,
    exportResolution=exportResolution)

CPlot.finalizeExport(offscreen)

if offscreen == 2: import os; os._exit(0)

CPlot.Decorator.getImage()

Read an image from a file into a numpy array.

Parameters:

fileName (string) – name of the image file to read


CPlot.Decorator.createSubPlot(img='.decorator.png', figsize=None, dpi=None, title=None, box=False, xlim=[], ylim=[], **kwargs)

Create a Matplotlib figure and axes from an image generated by CPlot.display or elsewhere. The returned figure and axes can be further modified with any Matplotlib command.

If xlim and ylim are both specified, then the axes are automatically transformed to be in the real coordinate system, which facilitates further modifications with Matplotlib commands. If box is also set to True, then both axes are fully graduated from xmin,ymin to xmax,ymax.

Parameters:
  • img – filename string or numpy array describing the image.

  • figsize (tuple of two floats (width, height)) – width and height of the figure in inches. If None, figsize is automatically calculated from the image ratio.

  • dpi (float) – the resolution in dots per inch. If None, dpi is automatically calculated from the image size and the figsize parameter.

  • title (string) – if not None, add a title to the figure.

  • box (boolean) – if True, display the axes around the image.

  • xlim (list of two floats [xmin, xmax]) – actual limits of the figure in the x direction.

  • ylim (list of two floats [ymin, ymax]) – actual limits of the figure in the y direction.

Returns:

Matplotlib figure and axes (fig, ax).

Other kwargs parameters:

  • fontsize (float, default=12.)

  • labelsize (float, default=10.)

  • interpolation (string, default=’none’) # ‘none’, ‘auto’, ‘antialiased’

  • xlabel (string, default=’x’)

  • xlabelpad (float, default=11.)

  • ylabel (string, default=’y’)

  • ylabelpad (float, default=11.)

  • boxColor (string, default=’black’)

  • boxLineWidth (float, default=1.0)

  • titlepad (float, default=12.)

Example of use:

import Converter.PyTree as C
import Generator.PyTree as G
import CPlot.PyTree as CPlot
import CPlot.Decorator as Decorator

import numpy

offscreen = 2
Decorator.setBatch(True)

a = G.cart((0,0,0), (0.01,0.01,1), (201,101,1))
C._initVars(a, '{F} = {CoordinateX}*{CoordinateY}')

ppw = 1000 # pixels per height
zplane = 0.0 # constant-z value
xlim = [1.0, 2.0] # xmin, xmax
ylim = [0.5, 1.0] # ymin, ymax
posCam, posEye, dirCam, viewAngle, exportResolution = Decorator.getInfo2DMode(xlim, ylim, zplane, ppw)

CPlot.display(a, mode='Scalar', scalarField='F',
    isoScales=['F',25,0.5,1.9], 
    colormap=24, # Jet colorbar
    export=CPlot.decorator, # Plot.decorator = '.decorator.png'
    offscreen=offscreen, 
    viewAngle=viewAngle,
    posCam=posCam, posEye=posEye, dirCam=dirCam,
    exportResolution=exportResolution)

CPlot.finalizeExport(offscreen)

# create raw image
fig, ax = Decorator.createSubPlot()
Decorator.savefig('out0.png', pad=0.0); Decorator.closeAll()

# create image with title and borders
fig, ax = Decorator.createSubPlot(box=True, title='F(x,y) = x*y')
Decorator.savefig('out1.png', pad=0.1); Decorator.closeAll()

# create image with title and graduated axes
fig, ax = Decorator.createSubPlot(box=True, title='F(x,y) = x*y', xlim=xlim, ylim=ylim)
Decorator.savefig('out2.png', pad=0.1)

# add modifications with Matplotlib commands
cx = numpy.linspace(xlim[0], xlim[1], 100)
ax.plot(cx, 1.20/cx, color='k', linestyle='dashed')
bbox = dict(boxstyle="round, pad=0.4, rounding_size=0.5", ec='k', fc="white", linewidth=1.5)
ax.text(1.5, 0.75, 'iso 1.2', size=12, ha='center', va='center', color='k', bbox=bbox, rotation=-27.)
Decorator.savefig('out3.png', pad=0.1)

if offscreen == 2: import os; os._exit(0)

CPlot.Decorator.createColorBar(fig, ax, levels=None, title=None, cmap=None, discrete=False, location='right', size='3%', pad=0.5, **kwargs)

Create and add a colorbar to an existing subplot figure.

Possible colormap names are ‘Jet’, ‘Magma’, ‘Inferno’, ‘Viridis’, ‘Plasma’, ‘Diverging’, ‘Greys’, and ‘Greens’.

Parameters:
  • fig (Matplotlib figure) – subplot figure.

  • ax (Matplotlib axes) – subplot axes.

  • levels (list [nLevels, minLevel, maxLevel]) – level information to display in the colorbar. If None, levels are taken from CPlot.

  • title (string) – if not None, add a title to the colorbar.

  • cmap (string or list of colors or list of (value, color)) – colormap name (‘Blue2Red’, …) or list. If None, cmap is taken from CPlot.

  • discrete (boolean) – if True, create a discrete colormap with as many colors as the number of levels.

  • location (string ('left', 'right', 'top', 'bottom')) – location of the colorbar on the figure.

  • size (string) – colorbar size in figure percentage.

  • pad (float) – amount of padding in inches between the axes and the colorbar.

Returns:

Matplotlib colorbar (cbar).

Other kwargs parameters:

  • extend (string, ‘neither’) # ‘neither’, ‘both’, ‘min’, ‘max’

  • extendrect (boolean, True)

  • fontsize (float, 12.)

  • labelsize (float, 10.)

  • titlepad (float, 12.)

  • labelFormat (string, ‘%.2f’)

  • nticks (int, 5)

Example of use:

import Converter.PyTree as C
import Generator.PyTree as G
import CPlot.PyTree as CPlot
import CPlot.Decorator as Decorator

import numpy

offscreen = 2
Decorator.setBatch(True)

a = G.cart((0,0,0), (0.01,0.01,1), (201,101,1))
C._initVars(a, '{F} = {CoordinateX}*{CoordinateY}')

ppw = 1000 # pixels per height
zplane = 0.0 # constant-z value
xlim = [1.0, 2.0] # xmin, xmax
ylim = [0.5, 1.0] # ymin, ymax
posCam, posEye, dirCam, viewAngle, exportResolution = Decorator.getInfo2DMode(xlim, ylim, zplane, ppw)

CPlot.display(a, mode='Scalar', scalarField='F',
    isoScales=['F',25,0.5,1.9], 
    colormap=0, # Default Blue2Red colorbar
    export=CPlot.decorator, # Plot.decorator = '.decorator.png'
    offscreen=offscreen, 
    viewAngle=viewAngle,
    posCam=posCam, posEye=posEye, dirCam=dirCam,
    exportResolution=exportResolution)

CPlot.finalizeExport(offscreen)

# create image with title and graduated axes
fig, ax = Decorator.createSubPlot(box=True, title='F(x,y) = x*y', xlim=xlim, ylim=ylim)

# add multicolor colorbar using CPlot information
cbar = Decorator.createColorBar(fig, ax, title='F')
Decorator.savefig('out4.png', pad=0.1); cbar.remove()

# add multicolor colorbar using cmap name
cmap = 'Blue2Red'
cbar = Decorator.createColorBar(fig, ax, title='F', levels=[25,0.5,1.9], cmap=cmap)
Decorator.savefig('out5.png', pad=0.1); cbar.remove()

# add multicolor colorbar using cmap list
cmap = [(0.00,[0,0,1]), (0.25,[0,1,1]), (0.50,[0,1,0]), (0.75,[1,1,0]), (1.00,[1,0,0])]
cbar = Decorator.createColorBar(fig, ax, title='F', levels=[25,0.5,1.9], cmap=cmap)
Decorator.savefig('out6.png', pad=0.1)

if offscreen == 2: import os; os._exit(0)

CPlot.Decorator.createArrow(ax, X1, X2, width=0.001, text=None, textSize=10, shiftText=(0, 0))

Create an arrow on figure.

Parameters:
  • X1 (tuple of 3 floats) – 3D position of arrow tail

  • X2 (tuple of 3 floats) – 3D position of arrow head

  • width (float) – width of arrow

  • color (string) – arrow color

  • text (string or None) – text to be displayed at arrow tail

  • textSize (int) – size of text

  • shiftText (tuple of 2 floats) – text offset in pixel units

Example of use:

# - Decorator.createArraow (pyTree) -
import CPlot.PyTree as CPlot
import CPlot.Decorator as Decorator
import Generator.PyTree as G
import Converter.PyTree as C

Decorator.setBatch(True)

a = G.cart((0,0,0), (1,1,1), (10,10,10))
CPlot.display(a, mode='mesh',
              export=CPlot.decorator,
              offscreen=2,
              bgColor=1,
              posCam=(16.493430100289256, 23.422964886666588, -13.69258647223435),
              posEye=(4.5, 4.5, 4.5),
              dirCam=(0.7444990345982689, 0.1529242342484992, 0.6498733461696639),)
CPlot.finalizeExport()

fig, ax = Decorator.createSubPlot()

Decorator.createArrow(ax, (11,11,11), (9,9,9), width=2., text="126. N", textSize=15, shiftText=(1.,0))
Decorator.createArrow(ax, (-2,11,11), (0,9,9), width=2., color='blue', text="127. N", textSize=15, shiftText=(1.,0))

Decorator.savefig('out.png')
import os; os._exit(0)

CPlot.Decorator.savefig(fileName, pad=0., dpi='figure')

Save figure in fileName. All file formats handled by Matplotlib can be used for export.

Parameters:
  • fileName (string) – name of the file to export to

  • pad (float) – amount of padding in inches around the figure, starting from a tight layout.

  • dpi (float or 'figure') – the resolution in dots per inch. If ‘figure’, use the figure’s dpi value.


CPlot.Decorator.show()

Display figure.


CPlot.Decorator.closeAll()

Close all figures previously created with Decorator.createSubPlot or directly via Matplotlib function calls.


Index