Plotting funtions

plt.py

class pyvib.plt.PlotUpdater(fig)

Add dynamic switching of graphs by changing data Hold shift and: - right: increase index - left: decrease index - up: last index - down: first index

Parameters

fig (plt figure) – Figure

Methods

addLine(line, ax, lineData)

Add a line to the figure

addLine(line, ax, lineData)

Add a line to the figure

Parameters
  • line (plt line pbject) – The line object to add

  • ax (axis) – Axis object the line belongs to

  • lineData (dictionary or list of dictionaries) – Dictionary must contain ‘x’ for x-data and ‘y’ for y-data

pyvib.plt.alignYLabels(fig, axes)

Align y labels to a single line for the axes The left-most positioned label is used

Parameters fig : figure

Figure which axes are on

axeslist of axes

Axes to align

pyvib.plt.arrow(x, y, angle, length, fig, ax, fs=None, slack=2.0, adjText=(0.0, 0.0), label=None, scale=0.7, hideArrow=False, marker=False, markerSize=2.0, markerColor='r', markerLabel='_', markerType='.', textbbox=None)

Makes an arrow in an axis based on a point, length and angle

Parameters
xfloat

x-axis point the arrow hits

yfloat

y-axis point the arrow hits

anglefloat

Angle of the arrow from horizontal. <0 - 360>

lengthfloat

Length of the arrow

figfigure object

Figure to plot on

axaxis object

Axis to plot arrow on

fsfont size, optional

Font size of a label

slackfloat, optional

Slack between arrowhead and the desired point

adjTexttupple of floats, optional

Allows for adjusting the text if crashing with arrow

labelstring, optional

Label to add at start of arrow Use None if no label should be added

scalefloat, optional

Scale size of arrow

hideArrowboolean, optional

Whether arrow should be hidden

markerboolean, optional

Whether a marker should be shown at the point

markerSizefloat, optional

Size of the added marker

markerColorstring, optional

Color of the marker Example ‘r’

markerLabelstring, optional

Label of the marker, for legends

markerTypestring, optional

Type of marker to use

textbbowbbox

Text bounding box e.g. bboxMe = {‘facecolor’:’white’, ‘edgecolor’:’white’, ‘zorder’:-100, ‘pad’:0}

Returns
None
pyvib.plt.arrowxy(x1, y1, x2, y2, fig, ax, fs=8.0, slack=2.0, adjText=(0.0, 0.0), label=None, scale=0.7, hideArrow=False, marker=False, markerSize=2.0, markerColor='r', markerLabel='_', markerType='.')

Makes an arrow in an axis based on endpoints

Parameters
x1float

x-axis point the arrow hits

y1float

y-axis point the arrow hits

x2: float

x-axis point of tail

y2float

y-axis point of tail

figfigure object

Figure to plot on

axaxis object

Axis to plot arrow on

fsfont size, optional

Font size of a label

slackfloat, optional

Slack between arrowhead and the desired point

adjTexttupple of floats, optional

Allows for adjusting the text if crashing with arrow

labelstring, optional

Label to add at start of arrow Use None if no label should be added

scalefloat, optional

Scale size of arrow

hideArrowboolean, optional

Whether arrow should be hidden

markerboolean, optional

Whether a marker should be shown at the point

markerSizefloat, optional

Size of the added marker

markerColorstring, optional

Color of the marker Example ‘r’

markerLabelstring, optional

Label of the marker, for legends

markerTypestring, optional

Type of marker to use

Returns
None
pyvib.plt.decimatedplot(t, vib, p, ax, direct=False)

Plot a decimated version where peaks are intact

Parameters
tfloat 1D array

Time signal

vibfloat 1D array

Signal

pint

Number of points to keep (roughly)

axaxis

Axis to plot on

directbool

If direct decimation should be performed instead of preserving peaks

pyvib.plt.doublearrow(x1, x2, y, fig, ax, fs=8.0, slack=2.0, adjText=(0.0, 0.0), label=None, scale=0.7)

Make a double-ended horizontal arrow

Parameters
x1float

x-axis point 1

x2float

x-axis point 2

yfloat

y-axis height

figfigure object

Figure to plot on

axaxis object

Axis to plot on

fsfloat, optional

Fontsize of label

slackfloat, optional

Slack between points and arrow head

adjTexttupple of floats, optional

Adjust text if it crashes with arrow

labelstring

Label over the arrow Use None for none

scalefloat, optional

Scale of the arrow

Returns
None
pyvib.plt.figure(nh, nw, width=238.0, height=133.0, axesLoc=None, sharex=False)

Makes a new figure with custom columns and rows, and custom size

Parameters
nhint

Number of ax rows

nwint

Number of ax columns

widthfloat, optional

Width of figure in pixels

heightfloat, optional

Height of figure in pixels

axesLoctupple of tupples

Wrapper for complete custom axes placement and sizes. Each element in the first tupple should contain (position y, position x, size in y, size in x) Best explained with an example:

  • axesLoc = ( (1,1,1,2), (2,1,1,2), (1,3,1,1), (2,3,1,1) )

Returns
figfigure object

Reference to the figure

axesaxes objects in 1D array

All axes in a row-major configuration

pyvib.plt.filterbankvisualization(filters, level, ymax=1.03)

Visualizes the effect of filters in a filterbank.

Can also be used to see the frequency response of a single filter

Parameters
filterslist of 1D arrays

List of filter kernels

levelint

The filterbank level. 0 means one filtering process

ymaxfloat, optional

The maximum y-axis value for plot. Use None to see all.

Returns
figfigure object

The figure

pyvib.plt.get_allLinesAndLabels(axes)

Get all lines and labels in a list of axes

Parameters

axes (list of axis) – Axes to get lines and labels from

Returns

  • lines (list of line) – Identified lines

  • labels (list of label) – Line labels

pyvib.plt.get_decimatedplotvalues(t, vib, r=None, p=None)

Get a decimated version of the input signal where peaks are visibly intact

Parameters
tfloat 1D array

Time signal

vibfloat 1D array

Signal

rint, optional

Denominator of fraction to use. Larger than 1 Either r or p must be supplied

pint, optional

Number of points to keep Either r or p must be supplied

Returns
tdecfloat 1D array

Decimated time signal

vibdecfloat 1D array

Decimated signal

pyvib.plt.get_xy(x, y, line, ax)

Get actual point of a line close to input point

Parameters
xfloat

x-axis point close to desired point

yfloat

y-axis point close to desired point

lineline object

The line to search on Get from line, = ax.plot()

axaxis object

Axis to look on

Returns
xdfloat

Closest x-point

ydfloat

Closest y-point

Iint

Data index of this point

class pyvib.plt.get_xydata(fig, ax, line, snap=True, label=None)

Get closest point on graph by clicking on the figure

Parameters
figfigure object

Figure to listen on

axaxis object

Axis to get values from

lineline object

Line to snap on

snapboolean, optional

Whether closest point on line should be returned

label: string, optional

For figures with multiple axes, the label can help distinguish them.

Returns
None

Methods

onclick

onkeypress

onrelease

printdata

pyvib.plt.plotbearingfaults(Y, df, X, bearing, maintitle='', harmonics=[8, 8, 8, 8], subbands=2)

Plots a 2x2 subplot figure of the same spectrum, but with harmonic lines

and side-bands for each fault type

Parameters
Yfloat 1D array

Spectrum to plot

dffloat

Delta frequency in Hz

Xfloat

Shaft speed in Hz

bearingfloat array_like

Characteristic fault orders of bearing [inner,roller,cage,outer] Should be in orders (per revolution)

maintitlestring, optional

Main title of figure

harmonicsarray_like of ints, optional

Number of harmonics to plot

subbandsint, optional

Number of side-bands to plot per harmonic

Returns
figfigure object

The figure

pyvib.plt.plotcepstrum(cep, dt, ax, xlim=None, line='-k', width=None, label=None, zorder=3)

Plot a cepstrum with certain limit in x and y direction

Parameters
cepfloat 1D array

Cepstrum values

dtfloat

Delta time

axaxis object

Axis to plot on

xlimNone or array_like, optional

X-axis limits:

  • None for showing the entire spectrum

  • [xmin, xmax] for a limit

linestring, optional

Design of the line, including color and linetype

widthstring, optional

Linewidth

labelstring, optional

Label for legends

Returns
lineline object

The spectrum line object

pyvib.plt.plotfft(Y, df, ax, xlim=None, linetype='-k', width=None, label='', zorder=3, color=None)

Plot a spectrum with certain limit in x and y direction

Parameters
Yfloat 1D array

Spectrum values

dffloat

Delta frequency

axaxis object

Axis to plot on

xlimNone or array_like, optional

X-axis limits:

  • None for showing the entire spectrum

  • [xmin, xmax] for a limit

linestring, optional

Design of the line, including color and linetype

widthstring, optional

Linewidth

labelstring, optional

Label for legends

zorderint, optional

The plotting depth order

colorstring, or RGB/RGBA tupple

The line color Will override line color

Returns
lineline object

The spectrum line object

pyvib.plt.plotforcingfreqs(Y, df, X, forcingfreqs, forcingfreqtitles=None, maintitle='', harmonics=8)

Plots a 2x2 subplot figure of the same spectrum, but with harmonic lines

and side-bands for each fault type

Parameters
Yfloat 1D array

Spectrum to plot

dffloat

Delta frequency in Hz

Xfloat

Shaft speed in Hz

forcingfreqsfloat array_like

Forcing frequencies

forcingfreqtitleslist of strings, optional

Title for each forcing frequency Must be as long as len(forcingfreqs)

maintitlestring, optional

Main title of figure

harmonicsint, optional

Number of harmonics to plot

subbandsint, optional

Number of side-bands to plot per harmonic

Returns
figfigure object

The figure

pyvib.plt.plotharmonics(fhar, fsub, ax, **kwParameters)

Plot harmonics and sidebands

Parameters
fharfloat

Harmonic frequency

fsubfloat

Sideband frequency

axaxis object

Axis to plot on

nharint or list of ints, optional

How many harmonics to plot:

  • If integer, it defines how many harmonics to plot

  • If list, define which harmonics to plot, i.e. [1,2,4]

  • Else the axis xlim defines how many to plot

nsub_pint or list of ints, optional

How many positive sidebands to plot:

  • If integer, it defines how many to plot for each harmonic

  • If list, it defines how many plot at each harmonic. i.e. [1,1,2]

  • Else 1 is plotted per harmonic

nsub_nint or list of ints, optional

See description of nsub_p

harcolorstring or tupple of floats, optional

Color of each harmonic:

  • If string, normal colors like ‘r’ or ‘k’

  • If tupple, RGBA is used. All numbers <0.0, 1.0>

harstylestring, optional

Style of harmonic

harwidthfloat, optional

Linewidth of harmonic

harlabelstring, optional

Legend label of harmonic

subcolorstring or tupple of floats, optional

Subband color:

  • If string, normal colors like ‘r’ or ‘k’

  • If tupple, RGBA is used. All numbers <0.0, 1.0>

substylestring, optional

Style of subbands

subwidthfloat, optional

Linewidth of subbands

sublabelstring, optional

Legend label of subbands

Returns
None
pyvib.plt.plotsquare(corner1, corner2, ax, line='-r', label='', linewidth=None)

Plot a square

Parameters
corner1tupple or array_like of floats

(x,y) position of lower left corner

corner2tupple or array_like of floats

(x,y) position of upper right corner

axaxis object

Axis to plot on

linestring, optional

Design of line with color and linetype

labelstring, optional

Legend label

Returns
None
pyvib.plt.rgb2hex(r, g, b)

Convert RGB to a hex string

Parameters
  • r (int) – Red <0, 255>

  • g (int) – Green <0, 255>

  • b (int) – Blue <0, 255>

Returns

hex – Hex string

Return type

string

pyvib.plt.savefig(fig, path, name, pad=0.03, dpi=200.0)

Saves the figure to file

Parameters
figfigure object

The figure to save

pathstring

Path to image folder. Dont add at the end

namestring

Name of the image. Default is .eps ending. Use whatever ending is appropriate .png, .jpg, .eps, .pdf etc.

padfloat, optional

Padding between frame and axis

dpifloat, optional

resolution of lossy formats

Returns
None
pyvib.plt.set_tickformat(ax, axes, fmat='%.1f')

Set certain format on numbers on axes

Parameters
  • ax (axis object) – Axis

  • axes (string) – both, x or y

  • fmat (string) – Number format to use

pyvib.plt.set_xticks(ax, majorspacing, minorspacing=None)

Set the major and minor spacing of the x-axis

Parameters
  • ax (figure axis) – The axis

  • majorspacing (float or array_like) – The desired major spacing. If single value: Spaces at multiples of majorspacing If array_like: Makes spacing at these points

  • minorspacing (float or array_like, optional) – Sets the minor spacing. Same rules as majorspacing

pyvib.plt.set_xylim(xlim, ax, ylim_mult=[1.0, 1.1], lines=None)

Set the desired x-lim, and adjustts the y-lim to fit the new x-lim

Parameters
  • xlim (float 1D array_lime) – The desired x-lim

  • ax (plt axis) – The axis

  • ylim_mul (float 1D array_like, optional) – Margins for the calculated y-limit in gain

  • lines (1D array_like with lines, optional) – Choose to get y-lim of certain lines instad of all available in ax. None if all should be used.

pyvib.plt.set_yticks(ax, majorspacing, minorspacing=None)

Set the major and minor spacing of the y-axis

Parameters
  • ax (figure axis) – The axis

  • majorspacing (float or array_like) – The desired major spacing. If single value: Spaces at multiples of majorspacing If array_like: Makes spacing at these points

  • minorspacing (float or array_like, optional) – Sets the minor spacing. Same rules as majorspacing

pyvib.plt.subfiglabels(fig, fs, usetex, position='upperrightoutside', move=3.0, skipaxes=None, transpose=False, axes=None)

Adds index to all subfigures in a placement of choice

Should run fig.tight_layout() first

Parameters
figfigure object

Figure to add indices on

fsfloat

Fontsize

usetexboolean

Whether latex rendring should be used

positionstring, optional

Position of indices:

  • upperleft

  • upperleftoutside

  • upperrightoutside

  • undercaption

movefloat, optional

Move slightly if crashing with edges etc.

transposebool, optional

If true, subfiglabels are column-major instad

Returns
None
pyvib.plt.textCorner(fig, ax, label, textbbox=None)

Add text in upper left corner of axis

Parameters
  • fig (figure object) –

  • ax (axis object) –

  • label (string) – Label in the corner

  • textbbow (bbox) – Text bounding box e.g. bboxMe = {‘facecolor’:’white’, ‘edgecolor’:’white’, ‘zorder’:-100, ‘pad’:0}

pyvib.plt.useexp(ax, exp=True, thr=(- 1, 1))

Use exponential term on y-axis on an axis

Parameters
axaxis object

Axis to make exponential

expboolean, optional

Turn on or off exp.

thrtupple, optional

Lower and upper 10^X threshold for using scientific notation

Returns
None
pyvib.plt.usetex(latex=False)

Use latex font in figures

Parameters
latexboolean, optional

Turn off or on latex rendring

Returns
None