pyrocko.plot¶
Utility functions and defintions for a common plot style throughout Pyrocko.
Functions with name prefix mpl_
are Matplotlib specific. All others should
be toolkit-agnostic.
The following skeleton can be used to produce nice PDF figures, with absolute
sizes derived from paper and font sizes
(file /../../examples/plot_skeleton.py
in the Pyrocko source directory):
from matplotlib import pyplot as plt
from pyrocko.plot import mpl_init, mpl_margins, mpl_papersize
# from pyrocko.plot import mpl_labelspace
fontsize = 9. # in points
# set some Pyrocko style defaults
mpl_init(fontsize=fontsize)
fig = plt.figure(figsize=mpl_papersize('a4', 'landscape'))
# let margins be proportional to selected font size, e.g. top and bottom
# margin are set to be 5*fontsize = 45 [points]
labelpos = mpl_margins(fig, w=7., h=5., units=fontsize)
axes = fig.add_subplot(1, 1, 1)
# positioning of axis labels
# mpl_labelspace(axes) # either: relative to axis tick labels
labelpos(axes, 2., 1.5) # or: relative to left/bottom paper edge
axes.plot([0, 1], [0, 9])
axes.set_xlabel('Time [s]')
axes.set_ylabel('Amplitude [m]')
fig.savefig('plot_skeleton.pdf')
plt.show()
Failed to get README.md: [Errno 2] No such file or directory: ‘/usr/local/lib/python3.11/dist-packages/pyrocko/plot/README.md’
Modules
A Python interface to GMT. |
|
This module contains functions to plot instrument response transfer functions in Bode plot style using Matplotlib. |
|
Matplotlib plotting with some fancy extras. |
|
Functions
|
Convert string into color float tuple ranged 0-1 for use with Matplotlib. |
|
Compatibility function to get named MPL colormap. |
Compatibility function to get named MPL colormap names. |
|
|
Initialize Matplotlib rc parameters Pyrocko style. |
|
Add some extra padding between label and ax annotations. |
|
Adjust Matplotlib subplot params with absolute values in user units. |
|
Get paper size in inch from string. |
|
Configure x axis of a matplotlib axes object for interactive time display. |
|
Round x to nice value. |
|
Get paper size from string. |
Classes
|
Mode of operation for auto-scaling. |
|
Tunable 1D autoscaling based on data range. |
Exceptions
Raised for invalid color definitions. |
- papersize(paper, orientation='landscape', units='point')[source]¶
Get paper size from string.
- Parameters:
paper – string selecting paper size. Choices:
'a0'
,'a1'
,'a2'
,'a3'
,'a4'
,'a5'
,'a6'
,'a7'
,'a8'
,'a9'
,'a10'
,'b0'
,'b1'
,'b2'
,'b3'
,'b4'
,'b5'
,'archa'
,'archb'
,'archc'
,'archd'
,'arche'
,'flsa'
,'halfletter'
,'note'
,'letter'
,'legal'
,'11x17'
,'ledger'
orientation –
'landscape'
, or'portrait'
units – Units to be returned. Choices:
'point'
,'inch'
, or'cm'
- Returns:
(width, height)
- class AutoScaleMode(...) str [source]¶
Bases:
StringChoice
Mode of operation for auto-scaling.
mode
description
'auto'
:Look at data range and choose one of the choices below.
'min-max'
:Output range is selected to include data range.
'0-max'
:Output range shall start at zero and end at data max.
'min-0'
:Output range shall start at data min and end at zero.
'symmetric'
:Output range shall by symmetric by zero.
'off'
:Similar to
'min-max'
, but snap and space are disabled, such that the output range always exactly matches the data range.
- class AutoScaler(approx_ticks=7.0, mode='auto', exp=None, snap=False, inc=None, space=0.0, exp_factor=3, no_exp_interval=(-3, 5))[source]¶
Bases:
Object
Tunable 1D autoscaling based on data range.
Instances of this class may be used to determine nice minima, maxima and increments for ax annotations, as well as suitable common exponents for notation.
The autoscaling process is guided by the following public attributes:
- ♦ approx_ticks¶
float
, default:7.0
Approximate number of increment steps (tickmarks) to generate.
- ♦ mode¶
str
(AutoScaleMode
), default:'auto'
Mode of operation for auto-scaling.
- ♦ exp¶
int
, optionalIf defined, override automatically determined exponent for notation by the given value.
- ♦ snap¶
bool
, default:False
If set to True, snap output range to multiples of increment. This parameter has no effect, if mode is set to
'off'
.
- ♦ inc¶
float
, optionalIf defined, override automatically determined tick increment by the given value.
- ♦ space¶
float
, default:0.0
Add some padding to the range. The value given, is the fraction by which the output range is increased on each side. If mode is
'0-max'
or'min-0'
, the end at zero is kept fixed at zero. This parameter has no effect if mode is set to'off'
.
- ♦ exp_factor¶
int
, default:3
Exponent of notation is chosen to be a multiple of this value.
- ♦ no_exp_interval¶
tuple
of 2int
objects, default:(-3, 5)
Range of exponent, for which no exponential notation is aallowed.
- make_scale(data_range, override_mode=None)[source]¶
Get nice minimum, maximum and increment for given data range.
Returns
(minimum, maximum, increment)
or(maximum, minimum, -increment)
, depending on whether data_range is(data_min, data_max)
or(data_max, data_min)
. Ifoverride_mode
is defined, the mode attribute is temporarily overridden by the given value.
- mpl_init(fontsize=10)[source]¶
Initialize Matplotlib rc parameters Pyrocko style.
Returns the matplotlib.pyplot module for convenience.
- mpl_get_cmap(name)[source]¶
Compatibility function to get named MPL colormap.
The function matplotlib.cm.get_cmap has been removed in MPL 3.8 but the suggested replacement is not available in slightly older versions of MPL, e.g. 3.3 (default on Debian 11).
- mpl_margins(fig, left=1.0, top=1.0, right=1.0, bottom=1.0, wspace=None, hspace=None, w=None, h=None, nw=None, nh=None, all=None, units='inch')[source]¶
Adjust Matplotlib subplot params with absolute values in user units.
Calls
matplotlib.figure.Figure.subplots_adjust()
onfig
with absolute margin widths/heights rather than relative values. Ifwspace
orhspace
are given, the number of subplots must be given innw
andnh
becausesubplots_adjust()
treats the spacing parameters relative to the subplot width and height.- Parameters:
units – Unit multiplier or unit as string:
'point'
,'inch'
, or'cm'
left,right,top,bottom – margin space
w – set
left
andright
at onceh – set
top
andbottom
at onceall – set
left
,top
,right
, andbottom
at oncenw – number of subplots horizontally
nh – number of subplots vertically
wspace – horizontal spacing between subplots
hspace – vertical spacing between subplots
- mpl_papersize(paper, orientation='landscape')[source]¶
Get paper size in inch from string.
Returns argument suitable to be passed to the
figsize
argument ofmatplotlib.pyplot.figure()
.- Parameters:
paper – string selecting paper size. Choices:
'a0'
,'a1'
,'a2'
,'a3'
,'a4'
,'a5'
,'a6'
,'a7'
,'a8'
,'a9'
,'a10'
,'b0'
,'b1'
,'b2'
,'b3'
,'b4'
,'b5'
,'archa'
,'archb'
,'archc'
,'archd'
,'arche'
,'flsa'
,'halfletter'
,'note'
,'letter'
,'legal'
,'11x17'
,'ledger'
orientation –
'landscape'
, or'portrait'
- Returns:
(width, height)
- exception InvalidColorDef[source]¶
Bases:
ValueError
Raised for invalid color definitions.
- mpl_color(x)[source]¶
Convert string into color float tuple ranged 0-1 for use with Matplotlib.
Accepts tango color names, matplotlib color names, and slash-separated strings. In the latter case, if values are larger than 1., the color is interpreted as 0-255 ranged. Single-valued (grayscale), three-valued (color) and four-valued (color with alpha) are accepted. An
InvalidColorDef
exception is raised when the convertion fails.
- mpl_time_axis(axes, approx_ticks=5.0)[source]¶
Configure x axis of a matplotlib axes object for interactive time display.
- Parameters:
axes (
matplotlib.axes.Axes
) – Axes to be configured.approx_ticks (float) – Approximate number of ticks to create.
This function tries to use nice tick increments and tick labels for time ranges from microseconds to years, similar to how this is handled in Snuffler.