Package: pympljstyle
pympljstyleA Python library for specifying and applying journal-specific styles to matplotlib figures.InstallationThe library can be installed usingpip:pipinstallpympljstyleUsageListing the available journalsThe journals that have registered styles can be accessed usingget_registered_journals:importpympljstyleregistered_journals=pympljstyle.get_registered_journals()print(*registered_journals,sep="\n")This shows the journal identifier followed by its complete title and any registered custom size units."cortex": Cortex (custom units: 1 column, 1.5 columns, 2 columns)"j_neurophys": Journal of Neurophysiology (custom units: single, double, full)Using a built-in styleTo use a built-in style, first identify the relevant journal identifier as shown above.
Then, the width of the figure needs to be specified.
This can be in typical units like centimetres or inches, but many journal style definitions also support more journal-specific units like 'columns' or identifiers such as 'single' (the custom units that are supported by a particular journal style are shown in the output ofget_registered_journals, as shown in the previous section).
The height of the figure can be specified using the same set of units, with the additional option of using a 'width' (or 'w') unit to allow for the height to be a multiple of the width (e.g., '0.5 widths').
Finally, the sort of content that will be shown in the figure can (optionally) be specified.
This is typically used by journal styles to identify the appropriate DPI to use when exporting the figure.
The available options arehalftone,combination(the default), andline.With the above information, the style for a particular journal can be applied using theapply_stylecontext manager:importmatplotlib.pyplotaspltimportpympljstylewithpympljstyle.apply_style(journal_name="cortex",width="1.5 cols",height="0.5 widths",content_type="line",):# the journal-specific styles are applied# the settings are returned to their previous valuesIf you want to access the settings directly, rather than applying them to a context manager, you can use theget_stylefunction.
This function has the same call signature ofapply_style.Note that these functions also set a few 'opinionated' settings that are not journal specific.
These can be disabled by setting the parameterwith_opinionated_defaultstoFalse.Specifying a new styleIf the desired journal style is not present in the package, you can add your own specification of the journal style to the package registry.
For example, we can re-create the style for the journal Cortex that is present in the package.
To do so, first create a class that inherits frompympljstyle.BaseJournaland is decorated by@pympljstyle.add_journal:@pympljstyle.add_journalclassCortex(pympljstyle.BaseJournal):...The key attributes that are inherited fromBaseJournalare:_rc_params: the dictionary of custom matplotlibrcParamssettings._content_type: the user-specified type of content in the figure (halftone,combination, orline)._ureg: apintUnitRegistryinstance.This class is required to have three class attributes:name: a journal identifier, typically in snake case.journal_name: the complete journal title.custom_units: a tuple of strings that describes the custom size units that are available for the journal.For Cortex, this looks like:@pympljstyle.add_journalclassCortex(pympljstyle.BaseJournal):name="cortex"journal_name="Cortex"custom_units=("1 column","1.5 columns","2 columns")The class is also required to have two methods:add_custom_settings: where custom entries intoself._rc_paramsare made.add_custom_units: where custom units are defined.For Cortex, the custom setting that we will use is their specification for the DPI of saved figures.
Because that is dependent on the content type, we will use the_content_typevariable to set the appropriate DPI value in_rc_params:@pympljstyle.add_journalclassCortex(pympljstyle.BaseJournal):name="cortex"journal_name="Cortex"custom_units=("1 column","1.5 columns","2 columns")defadd_custom_settings(self)->None:dpi={"halftone":300,"combination":500,"line":1000,}self._rc_params["savefig.dpi"]=dpi[self._content_type]We can also specify some custom units - in particular, we can specify figure widths in 'columns'.
As per theirwebsite(actually publisher rather than journal-specific), 1 column is 90 mm, 1.5 columns is 140 mm, and 2 columns is 190 mm.
To specify this relationship inpint, we need to convert the points into a slope and a y-intercept.
Here, the slope is 100 and the y-intercept is -10.
Inthe way of defining custom units, the y-intercept is referred to as theoffset.
For Cortex, the 'columns' unit (which can also be referred to as 'column', 'col', or 'cols') is set via:@pympljstyle.add_journalclassCortex(pympljstyle.BaseJournal):name="cortex"journal_name="Cortex"custom_units=("1 column","1.5 columns","2 columns")defadd_custom_settings(self)->None:dpi={"halftone":300,"combination":500,"line":1000,}self._rc_params["savefig.dpi"]=dpi[self._content_type]defadd_custom_units(self)->None:self._ureg.define("column = 100 mm; offset: -10 = col")Once complete, consider contributing your new journal styles to the package via a pull request.DevelopmentBuild and release processFirst, build the package (after having previously runpip install -e .[build]):python-mbuildThis will produce files in thedist/sub-directory.Then, test the ability to upload to the test PyPI server:python-mtwineupload--repositorytestpypidist/*If that all looks OK, then upload to PyPI:python-mtwinetwineuploaddist/*