API reference¶
pubplot.axes module¶
-
class
pubplot.axes.
PubAxes
(ax, rc)¶ Bases:
pubplot.helpers.RCParamWrapper
Matplotlib Axes wrapper.
This wraps Axes objects which allows us to use local RCparams.
-
ax
¶ A matplotlib Axes object or function that returns such object
-
rc
¶ Matplotlib RCparams
-
pubplot.document module¶
-
class
pubplot.document.
Document
(document_class, style=None)¶ Bases:
object
Document class used to create plots using the same style.
The Document class serves as a Figure factory. Figures follow the same style and are compatible with a given LaTeX document class. By default it uses the dichromatic style from
pubplot.styles
.Parameters: - document_class – dict with
documentclass
anddocument_options
. It may optionally contain a list of LaTeX packages underpackages
as well as any other argument acceptable bypylatex.document
. - style – dict following matplotlib rcParams convention.
-
style
¶ dict following matplotlib rcParams convention.
-
columnwidth
¶ equivalent size as in the LaTeX document class.
-
textwidth
¶ equivalent size as in the LaTeX document class.
-
tiny
¶ equivalent size as in the LaTeX document class.
-
scriptsize
¶ equivalent size as in the LaTeX document class.
-
footnotesize
¶ equivalent size as in the LaTeX document class.
-
small
¶ equivalent size as in the LaTeX document class.
-
normalsize
¶ equivalent size as in the LaTeX document class.
-
large
¶ equivalent size as in the LaTeX document class.
-
Large
¶ equivalent size as in the LaTeX document class.
-
LARGE
¶ equivalent size as in the LaTeX document class.
-
huge
¶ equivalent size as in the LaTeX document class.
-
Huge
¶ equivalent size as in the LaTeX document class.
Examples
You may use one of the available document_classes.
>>> from pubplot.document_classes import ieee_infocom >>> doc = Document(ieee_infocom)
Or provide your own, using a dict
>>> document_class = { ... 'documentclass': 'IEEEtran', ... 'document_options': ['10pt', 'conference', 'letterpaper'] ... } >>> doc = Document(document_class)
Similarly, a style can be chosen from one of the available
>>> from pubplot.styles import monochromatic >>> doc = Document(ieee_infocom, style=monochromatic())
or personalized to your specific needs.
>>> style_axis_below = { ... 'axes.grid': True, ... 'axes.axisbelow': False, ... ':bar:axes.grid.axis': 'y' ... } >>> doc = Document(ieee_infocom, style=style_axis_below)
Any rcParam can be used to define the style. But it also supports plot-specific styles. In the example above the option
':bar:axes.grid.axis': 'y'
applies only to bar plots. You may prepend:<plot_style>:
to any option in order to apply it only to the specific plot type (e.g., bar).Once you have a document, you can obtain any LaTeX font size related to the
document_class
you specified, e.g.,>>> doc.normalsize 10.0
-
FONT_OVERRIDES
= ['font.size', 'axes.labelsize', 'legend.fontsize', 'xtick.labelsize', 'ytick.labelsize']¶
-
figure
(width=None, height=None, scale=1, xscale=1, yscale=1)¶ Creates a new figure with a single plot.
Parameters: - width – figure width in pt, defaults to columnwidth.
- height – figure height in pt and, by default, it is adjusted automatically based on the width.
- scale – overall figure scale, adjusts both width and height.
- xscale – multiply width by xscale, leaving height intact.
- yscale – multiply height by yscale, leaving width intact.
Returns: a Figure object.
Return type: fig
-
subfigures
(nrows=1, ncols=1, width=None, height=None, scale=1, xscale=1, yscale=1, squeeze=True)¶ Creates a new figure with multiple plots.
Parameters: - nrows – number of plot rows.
- ncols – number of column rows.
- width – figure width in pt, defaults to columnwidth.
- height – figure height in pt and, by default, it is adjusted automatically based on nrows, ncols and width.
- scale – overall figure scale, adjusts both width and height.
- xscale – multiply width by xscale, leaving height intact.
- yscale – multiply height by yscale, leaving width intact.
- squeeze – If True (default) and nrows == 1 and ncols == 1, return a single axis object rather than a list of axes.
Returns: - a Figure and a list of axes, or, if squeeze == True,
a Figure and an axis object.
Return type: fig, axes
-
temporary_style
(new_style)¶ Returns a context manager which updates the current document style on enter and reverts the style on exit.
Examples
>>> import pubplot # doctest: +ELLIPSIS >>> pplt = pubplot.Document(pubplot.document_classes.ieee_jrnl) >>> with pplt.temporary_style({'font.size': 6}): ... # All font sizes now 6 ... fig, ax = pplt.subfigures() ... ax.plot([1, 2, 3], [1, 2, 3]) ... fig.save('test') [...] >>> # Font sizes now reverted >>> fig, ax = pplt.subfigures() >>> ax.plot([1, 2, 3], [1, 2, 3]) [...] >>> fig.save('test2')
-
update_style
(new_style)¶ Updates the current document style.
Used to change the current style. It is notably useful to change sizes, since the document sizes can only be retrieved after the class was initialized.
Examples
An example, updating sizes
>>> from pubplot.document_classes import ieee_infocom >>> doc = Document(ieee_infocom) >>> >>> update_sizes = { ... 'font.size': doc.footnotesize, ... 'axes.labelsize': doc.footnotesize, ... 'legend.fontsize': doc.scriptsize, ... 'xtick.labelsize': doc.footnotesize, ... 'ytick.labelsize': doc.footnotesize ... } >>> doc.update_style(update_sizes)
Parameters: new_style – a dict with rcParams. If the option is not specified in the new dict it remains with the old value.
- document_class – dict with
pubplot.document_classes module¶
pubplot.figure module¶
-
class
pubplot.figure.
PubFigure
(fig, rc)¶ Bases:
pubplot.helpers.RCParamWrapper
Matplotlib Figure wrapper.
This wraps Figure objects which allows us to do some nice stuff. Such as local rcParams and personalized save method.
Parameters: - fig – A matplotlib Figure object or function that returns such object.
- rc – Matplotlib RCparams.
-
fig
¶ A matplotlib Figure object.
-
add_subplot
(*args, **kwargs)¶
-
save
(name, pdf=True, pgf=True)¶ Save figure to pgf and pdf.
By default it saves the figure in both pdf and pgf, but this behavior may be adapted using
pdf
andpgf
keyword arguments.Parameters: - name – file name without extension
- pdf – if True saves figure in pdf format
- pgf – if True saves figure in pgf format
pubplot.helpers module¶
-
class
pubplot.helpers.
RCParamWrapper
(obj, rc)¶ Bases:
object
Matplotlib object wrapper for nonglobal RCParams.
This wraps Matplotlib objects which allows us set RCparams per object. Optionally can use a function that returns an object so that the object is lazy initialized
-
obj
¶ A matplotlib object or a function that returns such object
-
rc
¶ Matplotlib RCparams
-
-
class
pubplot.helpers.
RCParams
(rc_dict)¶ Bases:
object
Handle plot-specific rcParams
Stores rcParams, optionally filtering plot-specific options.
Parameters: rc_dict – rcParams style dict. With plot-specific options prepended with the plot type. -
rc_dict
¶ rcParams style dict. With plot-specific options prepended with the plot type.
-
get_rc_to_function
(func)¶
-
-
pubplot.helpers.
dict_select
(my_dict, term, expect=True)¶
pubplot.latex module¶
-
pubplot.latex.
get_document_sizes
(document_class)¶ Get useful document sizes given a LaTeX document class.
Parameters: document_class – dict with documentclass
anddocument_options
. It may optionally contain a list of LaTeX packages underpackages
as well as any other argument acceptable bypylatex.document
.Examples
You may use one of the available document_classes.
>>> from pprint import pprint >>> from pubplot.document_classes import ieee_infocom >>> sizes_dict = get_document_sizes(ieee_infocom) >>> pprint(sizes_dict) {'Huge': 24.0, 'LARGE': 17.0, 'Large': 14.0, 'caption': 8.0, 'columnwidth': 252.0, 'footnotesize': 8.0, 'huge': 20.0, 'large': 12.0, 'normalsize': 10.0, 'scriptsize': 7.0, 'small': 9.0, 'textwidth': 516.0, 'tiny': 5.0}
Or provide your own, using a dict
>>> document_class = { ... 'documentclass': 'IEEEtran', ... 'document_options': ['10pt', 'conference', 'letterpaper'] ... } >>> sizes_dict = get_document_sizes(document_class) >>> pprint(sizes_dict) {'Huge': 24.0, 'LARGE': 17.0, 'Large': 14.0, 'caption': 8.0, 'columnwidth': 252.0, 'footnotesize': 8.0, 'huge': 20.0, 'large': 12.0, 'normalsize': 10.0, 'scriptsize': 7.0, 'small': 9.0, 'textwidth': 516.0, 'tiny': 5.0}
Returns: A dictionary containing sizes - columnwidth
- textwidth
- tiny
- scriptsize
- footnotesize
- small
- normalsize
- large
- Large
- LARGE
- huge
- Huge
- caption
pubplot.styles module¶
-
pubplot.styles.
dichromatic
(color1='r', color2='k')¶ Dichromatic style.
Classic style using only two colors. It includes a background grid.
Parameters: - color1 – primary color, defaults to red.
- color2 – secondary color, defaults to black.
Returns: rcParams dict with the appropriate style
Return type: dict
-
pubplot.styles.
monochromatic
(color='k')¶ Monochromatic style.
Equivalent to Dichromatic using the same color for both arguments.
Parameters: color – the single color, defaults to black. Returns: rcParams dict with the appropriate style Return type: dict
-
pubplot.styles.
mpl_default
()¶
-
pubplot.styles.
qualitative
(markers=True)¶