snpl package

Submodules

Module contents

A thin wrapper to matplotlib.pyplot and some utility functions

Examples

Here are particularly useful functions.

Basic plotting

>>> import snpl           # this will set the style
>>> import numpy as np
>>> snpl.limx(0.0, 100.0) # unlike pyplot.xlim(), a margin will be added automatically
>>> snpl.limy(0.0, 1.0)
>>> snpl.labelx("$x$") # latex math style is available
>>> snpl.labelx("$y$")
>>> x = np.linspace(10.0, 90.0, 100)
>>> y = np.random.rand(100)
>>> snpl.plot(x, y, ls="", marker="o", color="g", label="data")
>>> snpl.legend(fontsize=8, loc="upper right")
>>> snpl.savefig("test.pdf")
>>> snpl.delete_lines() # this erases all the lines and texts on the current Axes while keeping other things intact

Data cropping - snpl.crop()

>>> import numpy as np
>>> import snpl
>>> np.random.seed(0)
>>> x = np.linspace(0.0, 100.0, 5)
>>> y = np.random.rand(5)
>>> z = np.random.rand(5)
>>> print(x, y, z)
[  0.  25.  50.  75. 100.] [0.5488135  0.71518937 0.60276338 0.54488318 0.4236548 ] [0.64589411 0.43758721 0.891773   0.96366276 0.38344152]
>>> xc, yc, zc = snpl.crop(x, [20.0, 50.0], y, z) # x, y, z will be cropped based on the range of x (20.0 ~ 50.0)
>>> print(xc, yc, zc)
[25. 50.] [0.71518937 0.60276338] [0.43758721 0.891773  ]

Gradient color generator - snpl.get_colors()

>>> import numpy as np
>>> import snpl
>>> x = np.linspace(0.0, 1.0, 10)
>>> n = 10
>>> cs = snpl.get_colors(n)
>>> for i in range(n):
>>>     snpl.plot(x, np.random.rand(len(x))+i, ls="-", marker="o", color=cs[i], label=str(i))
>>> snpl.legend()
>>> snpl.show()

Color manipulation - snpl.tint()

>>> import numpy as np
>>> import snpl
>>> x = np.linspace(0.0, 1.0, 10)
>>> c = "g"
>>> snpl.plot(x, np.random.rand(len(x))+0.0, ls="-", marker="o", color=snpl.tint(c, 0.0), label="0.0")
>>> snpl.plot(x, np.random.rand(len(x))+1.0, ls="-", marker="o", color=snpl.tint(c, 0.2), label="0.2")
>>> snpl.plot(x, np.random.rand(len(x))+2.0, ls="-", marker="o", color=snpl.tint(c, 0.5), label="0.5")
>>> snpl.plot(x, np.random.rand(len(x))+3.0, ls="-", marker="o", color=snpl.tint(c, 0.8), label="0.8")
>>> snpl.legend()
>>> snpl.show()

Access the pyplot instance

>>> import snpl
>>> x = [0.0, 1.0, 2.0]
>>> y = [3.0, 4.0, 5.0]
>>> snpl.pyplot.bar(x, y) # you have full access to pyplot functionality
>>> snpl.show()
snpl.arrow_edge(height, side='left', len_rel=0.1, ax=None, text='', **arrowprops)[source]
snpl.axhline(*args, **kwargs)[source]

Shortcut to pyplot.axhline()

snpl.axvline(*args, **kwargs)[source]

Shortcut to pyplot.axvline()

snpl.cla()[source]

Clears the current Axes.

Shortcut for pyplot.cla().

snpl.clf()[source]

Clears the current Figure.

Shortcut for pyplot.clf().

snpl.crop(xarr, rangex, *arrays, remove_nans=True, remove_infs=True)[source]

Crop arrays to a given range.

Parameters:
  • xarr – Key array.

  • rangex – Range along the key array.

  • *arrays – other arrays to be cropped.

  • remove_nans – If True, nan`s in any of the given arrays are removed. Defaults to `True.

  • remove_infs – If True, inf`s in any of the given arrays are removed. Defaults to `True.

Returns:

Cropped arrays.

Example

>>> import numpy as np
>>> import snpl
>>>
>>> x = np.arange(10).astype(float)
>>> y = x + 0.1
>>> z = x + 0.2
>>> print(x)
>>> [0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
>>> print(y)
>>> [0.1 1.1 2.1 3.1 4.1 5.1 6.1 7.1 8.1 9.1]
>>> print(z)
>>> [0.2 1.2 2.2 3.2 4.2 5.2 6.2 7.2 8.2 9.2]
>>>
>>> xc, yc, zc = snpl.crop(x, [2.5, 6.0], y, z)
>>> print(xc)
>>> [3. 4. 5. 6.]
>>> print(yc)
>>> [3.1 4.1 5.1 6.1]
>>> print(zc)
>>> [3.2 4.2 5.2 6.2]
snpl.delete_lines(*axes_objects)[source]
snpl.draw_powerlaw(xi=1.0, xf=10.0, yi=1.0, exponent=1.0, ax=None, **lineprops)[source]

Draw a power-law guide line in the log-log plot.

This function uses Axes.plot() to draw a guide line of a power law with a given exponent.

Parameters:
  • xi – x coordinate of the start point of the guide line.

  • xf – x coordinate of the end point of the guide line.

  • yi – y coordinate of the start point of the guide line.

  • exponent – Exponent of the power law, i.e., slope of the line.

  • ax – Axes object to which the function operates. If None, the current Axes in pyplot.

  • linepropskwargs passed to Axes.plot().

Returns:

The created Line2D object.

snpl.errorbar(*args, **kwargs)[source]

Shortcut for pyplot.errorbar()

snpl.expandx(ratio=0.03, ax=None)[source]

Expands the xlim based on the canvas size.

Parameters:
  • ratio – Expand ratio. 0.0 for no expansion.

  • ax – Axes object to which the function operates. If None, the current Axes in pyplot.

snpl.expandy(ratio=0.03, ax=None)[source]

Expands the ylim based on the canvas size.

Parameters:
  • ratio – Expand ratio. 0.0 for no expansion.

  • ax – Axes object to which the function operates. If None, the current Axes in pyplot.

snpl.figsize(wid_in=None, hei_in=None, ax=None)[source]

Sets the figure size

Parameters:
  • wid_in – width in inches. If None, inherit the current value. Defaults to None.

  • hei_in – height in inches. If None, inherit the current value. Defaults to None.

  • axAxes object. The size of the figure containing this Axes object will be modified. If None, the current figure in pyplot will be modified. Defaults to None.

Returns:

Current figure width & height in tuple (width, height).

snpl.formatx(empty=False, scientific=True, ax=None)[source]

Modifies the axis tick label formatting for x axis.

Parameters:
  • empty – If True, no tick labels.

  • scientific – If False, turn off the scientific notation (e.g. 10^3)

  • ax – Axes object to which the label will be set. If None, the current Axes in pyplot.

snpl.formaty(empty=False, scientific=True, ax=None)[source]

Modifies the axis tick label formatting for y axis.

Parameters:
  • empty – If True, no tick labels.

  • scientific – If False, turn off the scientific notation (e.g. 10^3)

  • ax – Axes object to which the label will be set. If None, the current Axes in pyplot.

snpl.gca()[source]

Gets the current Axes object.

Shortcut for pyplot.gca().

snpl.get_colors(num, name='inferno')[source]

Make a color series from a colormap.

Parameters:
  • num – Number of colors to generate.

  • name – Color map name registered in matplotlib.

Returns:

A list of (r,g,b,a) arrays.

Example

>>> cs = snpl.get_colors(5)
>>> x = np.array([0.0, 1.0])
>>> y = np.array([1.0, 1.0])
>>> for i, c in enumerate(cs):
>>>     snpl.plot(x, y + i, color=c, marker="", ls="-")
>>> snpl.show()
snpl.get_markers(num, symbols='osD^>v<XP')[source]

Make a marker style series.

This function makes a list of marker style characters of a given length. The marker styles are cyclic. For example, with the default symbols, >>> print(snpl.get_markers(11)) >>> [“o”, “s”, “D”, “^”, “>”, “v”, “<”, “X”, “P”, “o”, “s”] Note that the pattern repeats after 10th item.

Parameters:
  • num – Number of marker styles to generate.

  • symbols – Repeat pattern of the symbols.

Returns:

A list of strings.

Example

>>> cs = snpl.get_markers(5)
>>> x = np.array([0.0, 1.0])
>>> y = np.array([1.0, 1.0])
>>> for i, m in enumerate(ms):
>>>     snpl.plot(x, y + i, marker=m)
>>> snpl.show()
snpl.labelx(label, ax=None, **kwargs)[source]

Sets the label for x axis for the current axes.

Parameters:
  • ax – Axes object to which the label will be set. If None, the current Axes in pyplot.

  • kwargs – Arguments to set_xlabel().

snpl.labely(label, ax=None, **kwargs)[source]

Sets the label for y axis for the current axes.

Parameters:
  • ax – Axes object to which the label will be set. If None, the current Axes in pyplot.

  • kwargs – Arguments to set_ylabel().

snpl.legend(*axes_objects, **legendprops)[source]
snpl.limx(left=None, right=None, expandratio=0.03, ax=None)[source]

Sets left and right limits of the x axis.

If either of two limits is not given, return currently-set limits instead.

Parameters:
  • left – left limit.

  • right – right limit.

  • ax – Axes object to which the function operates. If None, the current Axes in pyplot.

  • expandratio – expansion ratio. 0.0 for no expansion.

Returns:

current limits in two-tuple.

snpl.limy(lower=None, upper=None, expandratio=0.03, ax=None)[source]

Sets lower and upper limits of the x axis.

If either of two limits is not given, return currently-set limits instead.

Parameters:
  • lower – lower limit.

  • upper – upper limit.

  • ax – Axes object to which the function operates. If None, the current Axes in pyplot.

  • expandratio – expansion ratio. 0.0 for no expansion.

Returns:

current limits in two-tuple.

snpl.linewidth(lw)[source]

Sets the default line width through matplotlib.rc

Default values of - plot line width - axes frame line width, - tick mark line width - tick mark length (major ticks: lw*4, minor ticks: lw*2) will be changed.

Parameters:

lw – line width in points.

snpl.log(axis='xy', b=True, ax=None)[source]

Sets/unsets log scale to x and/or y axes.

This also sets the ticks nicely.

Parameters:
  • axis – Axis (x and/or y) to be modified. “x”, “y”, or “xy”.

  • b – If True, the axis is changed to log scale. If False, the axis is changed to linear scale.

  • ax – Axes object to which the function operates. If None, the current Axes in pyplot.

snpl.markersize(ms=None, mew=None)[source]

Sets the default marker size through matplotlib.rc

Parameters:
  • ms – Markersize in points. If None, no change will be made. It also sets the errorbar cap size (half of the marker size)

  • mew – Marker edge width in points. If None, no change will be made.

Note

This just sets the default value. Styles can be modified for individual plots in snpl.plot() etc.

snpl.plot(*args, **kwargs)[source]

Shortcut for pyplot.plot()

snpl.remove_borders(*args, ax=None)[source]

Removes axes frame on the specified sides.

Parameters:
  • args – One or more strings from (“top”, “left”, “right”, “bottom”) that specifies the border(s) to be removed.

  • ax – Axes object to which the function operates. If None, the current Axes in pyplot.

snpl.savefig(*args, set_title_with_script_path=True, **kwargs)[source]

Shortcut for pyplot.savefig() with additional utility argument

Shortcut for pyplot.savefig().

Parameters:
  • set_title_with_script_path – If True, the title entry in the saved image file (.e.g, PDF) is set to Generated by <top level Python module that generated the file>. Note that it wouldn’t work properly if metadata arg is set in args or kwargs.

  • args – Arguments for pyplot.savefig().

  • kwargs – Keyword arguments for pyplot.savefig().

snpl.show(*args, **kwargs)[source]
snpl.text_corner(s, x=0.03, y=0.9, ax=None)[source]

Add a text in the canvas.

Parameters:
  • s – Text.

  • x – x position. Fraction of the plot area width.

  • y – y position. Fraction of the plot area height.

  • ax – Axes object to which the function operates. If None, the current Axes in pyplot.

Returns:

The created Text object.

snpl.ticklabelpadding(val)[source]

Sets the default tick label padding through matplotlib.rc

The tick label padding is the space between the tick and the tick label (numbers on the axis). This method applies for major and minor ticks on both x and y axes.

Parameters:

val – padding in points.

snpl.ticx(step='auto', minor=False, ax=None)[source]

Sets the ticks nicely on linear-scaled x axis.

Parameters:
  • step – Tick spacing value. If “auto”, automatic.

  • minor – If False, set the major ticks. If True, set the minor ticks. Defaults to False (major ticks).

  • ax – Axes object to which the function operates. If None, the current Axes in pyplot.

snpl.ticxlog(numticks='every', ax=None)[source]

Sets the ticks nicely on log-scaled x axis.

Parameters:
  • numticks – If an int value, sets the number of ticks to that value (as much as possible). If “every”, the major ticks will be placed every decade. Defaults to “every”.

  • ax – Axes object to which the function operates. If None, the current Axes in pyplot.

snpl.ticy(step='auto', minor=False, ax=None)[source]

Sets the ticks nicely on linear-scaled y axis.

Parameters:
  • step – Tick spacing value. If “auto”, automatic.

  • minor – If False, set the major ticks. If True, set the minor ticks. Defaults to False (major ticks).

  • ax – Axes object to which the function operates. If None, the current Axes in pyplot.

snpl.ticylog(numticks='every', ax=None)[source]

Sets the ticks nicely on log-scaled y axis.

Parameters:
  • numticks – If an int value, sets the number of ticks to that value (as much as possible). If “every”, the major ticks will be placed every decade. Defaults to “every”.

  • ax – Axes object to which the function operates. If None, the current Axes in pyplot.

snpl.tint(c, f)[source]

Tint the given color.

Parameters:
  • c – The color to be tinted. Can be any color-like object that matplotlib accepts.

  • f – Tinting degree. 0.0 = original color, 1.0 = white.

Returns:

(r,g,b) tuple.

Example

>>> snpl.plot([0], [0], marker="o", color=snpl.tint("b", 0.0)) # blue
>>> snpl.plot([1], [1], marker="o", color=snpl.tint("b", 0.3)) # thin blue
>>> snpl.plot([2], [2], marker="o", color=snpl.tint("b", 0.6)) # very thin blue
>>> snpl.show()
snpl.title(text, ax=None, **kwargs)[source]

Sets the label for y axis for the current axes.

Parameters:
  • ax – Axes object to which the label will be set. If None, the current Axes in pyplot.

  • kwargs – Arguments to set_title().