Tweaking figures#
Most of the customization typically needed when exploring and visualizing your data is covered by the keyword arguments illustated in the line-plot and image-plot notebooks. However, getting figures ready for publication often requires more fine-grained tuning.
[1]:
%matplotlib inline
import scipp as sc
import plopp as pp
import numpy as np
import matplotlib.pyplot as plt
da = pp.data.data1d()
Basic modifications#
Changing axes labels#
By default, Plopp will add labels on the horizontal and vertical axes to the best of its knowledge.
[2]:
pp.plot(da)
[2]:
To change the label of the horizontal or vertical axes, use the xlabel/ylabel
arguments:
[3]:
pp.plot(da, xlabel="Time (milliseconds)", ylabel="Phase of sound wave")
[3]:
Adding a figure title#
To add a title to the figure, use the title
argument:
[4]:
pp.plot(da, title='This is my figure title')
[4]:
Setting the axes ranges#
Changing the range of the horizontal or vertical axes is done using the xmin/xmax
and ymax/ymax
arguments, respectively. If only the min
or max
is given, then the other will be automatically determined from the data plotted.
[5]:
pp.plot(
da,
xmin=sc.scalar(10, unit='m'),
ymin=sc.scalar(-0.5, unit='m/s'),
ymax=sc.scalar(1.5, unit='m/s'),
)
[5]:
Note that if the unit in the supplied limits is not identical to the data units, an on-the-fly conversion is attempted. It is also possible to omit the units altogether, in which case it is assumed the unit is the same as the data unit:
[6]:
pp.plot(da, xmin=10, xmax=45)
[6]:
Further modifications#
Instead of providing keyword arguments for tweaking every aspect of the figures, we provide accessors to the underlying Matplotlib Figure
and Axes
objects, that can then directly be used to make the required modifications.
Tick locations and labels#
To change the location of the ticks, as well as their labels, we directly access the Matplotlib axes via the .ax
property of the figure:
[7]:
p = da.plot()
p.ax.set_yticks([-1.0, -0.5, 0, 0.5, 1.0])
p.ax.set_yticklabels([r'$-\pi$', r'$-\pi / 2$', '0', r'$\pi / 2$', r'$\pi$'])
p
[7]:
Axes placement#
To control the exact placement of the axes, it is best to first create the axes manually with Matplotlib and then attaching the Plopp figure to them via the ax
argument.
[8]:
a = pp.data.data1d() * 5.0
b = a + sc.array(dims=a.dims, values=np.random.random(a.shape[0]) - 0.5, unit=a.unit)
fig = plt.figure(figsize=(5, 4))
ax0 = fig.add_axes([0.0, 0.2, 1.0, 0.8])
ax1 = fig.add_axes([0.0, 0.0, 1.0, 0.2])
ax0.xaxis.tick_top()
ax0.xaxis.set_label_position('top')
ax1.set_ylabel('Residuals')
p1 = pp.plot({'a': a, 'b': b}, ax=ax0)
p2 = pp.plot(a - b, ax=ax1)

It is also possible to control the placement of the colorbar for image plots using the cax
argument:
[9]:
da = pp.data.data3d()
fig, ax = plt.subplots(2, 1, figsize=(5, 8))
cax = fig.add_axes([1.0, 0.3, 0.03, 0.5])
pz = pp.plot(da['z', 0], ax=ax[0], cax=cax)
py = pp.plot(da['y', -1], ax=ax[1], cbar=False)
