-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from wiseodd/context-manager
Context manager
- Loading branch information
Showing
10 changed files
with
743 additions
and
255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import pub_ready_plots | ||
|
||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
|
||
rc_params, fig_width_in, fig_height_in = pub_ready_plots.get_mpl_rcParams( | ||
width_frac=1, # between 0 and 1 | ||
height_frac=0.15, # between 0 and 1 | ||
layout="poster-portrait", # or "iclr", "neurips", "poster-portrait", "poster-landscape" | ||
single_col=False, # only works for the "icml" layout | ||
) | ||
|
||
# You can update `rc_params` further before feeding it to `plt`, e.g. | ||
rc_params.update({"axes.linewidth": 1}) | ||
|
||
# Use the styles globally. | ||
# To make it local, use `with plt.rc_context(rc_params):` | ||
plt.rcParams.update(rc_params) | ||
|
||
fig, axs = plt.subplots(1, 2, constrained_layout=True) | ||
fig.set_size_inches(fig_width_in, fig_height_in) | ||
|
||
x = np.linspace(-1, 1, 100) | ||
|
||
axs[0].plot(x, np.sin(x)) | ||
axs[0].set_title("Sine") | ||
axs[0].set_xlabel(r"$x$") | ||
axs[0].set_ylabel(r"$\mathrm{sin}(x)$") | ||
|
||
axs[1].plot(x, np.cos(x)) | ||
axs[1].set_title("Cosine") | ||
axs[1].set_xlabel(r"$x$") | ||
axs[1].set_ylabel(r"$\mathrm{cos}(x)$") | ||
|
||
fig.savefig("advanced_usage.pdf") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import pub_ready_plots | ||
import numpy as np | ||
|
||
with pub_ready_plots.get_context( | ||
width_frac=1, # between 0 and 1 | ||
height_frac=0.15, # between 0 and 1 | ||
nrows=1, # depending on your subplots | ||
ncols=2, # depending on your subplots | ||
layout="iclr", # or "iclr", "neurips", "poster-portrait", "poster-landscape" | ||
single_col=False, # only works for the "icml" layout | ||
sharey=True, # Additional keyword args for `plt.subplots` | ||
) as (fig, axs): | ||
x = np.linspace(-1, 1, 100) | ||
|
||
axs[0].plot(x, np.sin(x)) | ||
axs[0].set_title("Sine") | ||
axs[0].set_xlabel(r"$x$") | ||
axs[0].set_ylabel(r"$\mathrm{sin}(x)$") | ||
|
||
axs[1].plot(x, np.cos(x)) | ||
axs[1].set_title("Cosine") | ||
axs[1].set_xlabel(r"$x$") | ||
axs[1].set_ylabel(r"$\mathrm{cos}(x)$") | ||
|
||
fig.savefig("simple_plot.pdf") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from pub_ready_plots.pub_ready_plots import get_mpl_rcParams, get_context | ||
|
||
__all__ = ["get_mpl_rcParams", "get_context"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import matplotlib as mpl | ||
import matplotlib.font_manager as font_manager | ||
|
||
cmfont = font_manager.FontProperties(fname=mpl.get_data_path() + "/fonts/ttf/cmr10.ttf") | ||
FONT_NAME_CM = cmfont.get_name() | ||
FONT_NAME_TNR = "Times New Roman" | ||
FONT_NAME_AVENIR = "Avenir Next Condensed" | ||
|
||
PAPER_FORMATS = { | ||
"icml": { | ||
"text_width": 6.00117, | ||
"col_width": 3.25063, | ||
"text_height": 8.50166, | ||
"font_name": FONT_NAME_TNR, | ||
"footnote_size": 8, | ||
"script_size": 7, | ||
"linewidth": 1.25, | ||
"tick_size": 1.5, | ||
"tick_width": 0.5, | ||
}, | ||
"neurips": { | ||
"text_width": 5.50107, | ||
"col_width": 5.50107, | ||
"text_height": 9.00177, | ||
"font_name": FONT_NAME_TNR, | ||
"footnote_size": 8, | ||
"script_size": 7, | ||
"linewidth": 1.25, | ||
"tick_size": 1.5, | ||
"tick_width": 0.5, | ||
}, | ||
"iclr": { | ||
"text_width": 5.50107, | ||
"col_width": 5.50107, | ||
"text_height": 9.00177, | ||
"font_name": FONT_NAME_TNR, | ||
"footnote_size": 8, | ||
"script_size": 7, | ||
"linewidth": 1.25, | ||
"tick_size": 1.5, | ||
"tick_width": 0.5, | ||
}, | ||
"jmlr": { | ||
"text_width": 6.00117, | ||
"col_width": 6.00117, | ||
"text_height": 8.50166, | ||
"font_name": FONT_NAME_CM, | ||
"footnote_size": 8, | ||
"script_size": 7, | ||
"linewidth": 1.25, | ||
"tick_size": 1.5, | ||
"tick_width": 0.5, | ||
}, | ||
"poster-landscape": { | ||
"text_width": 6.00117, | ||
"col_width": 6.00117, | ||
"text_height": 8.50166, | ||
"font_name": FONT_NAME_AVENIR, | ||
"footnote_size": 30, | ||
"script_size": 23, | ||
"linewidth": 3, | ||
"tick_size": 4, | ||
"tick_width": 2, | ||
}, | ||
"poster-portrait": { | ||
"text_width": 6.00117, | ||
"col_width": 6.00117, | ||
"text_height": 8.50166, | ||
"font_name": FONT_NAME_AVENIR, | ||
"footnote_size": 10, | ||
"script_size": 8, | ||
"linewidth": 1, | ||
"tick_size": 1.5, | ||
"tick_width": 0.5, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,29 @@ | ||
[project] | ||
name = "pub-ready-plots" | ||
version = "0.1.0" | ||
version = "0.2.0" | ||
description = "Easy publication-ready matplotlib plots for ML papers and posters." | ||
authors = [{ name = "Agustinus Kristiadi", email = "[email protected]" }] | ||
classifiers = [ | ||
"Development Status :: 3 - Alpha", | ||
"License :: OSI Approved :: MIT License", | ||
"Operating System :: OS Independent", | ||
"Programming Language :: Python :: 3.9", | ||
] | ||
requires-python = ">=3.9" | ||
dependencies = ["matplotlib>=3.9.0"] | ||
requires-python = ">=3.10" | ||
readme = "README.md" | ||
license = { text = "MIT" } | ||
|
||
[build-system] | ||
requires = ["pdm-backend"] | ||
build-backend = "pdm.backend" | ||
|
||
|
||
[tool.pdm] | ||
distribution = true | ||
|
||
[tool.pdm.dev-dependencies] | ||
test = ["pytest>=8.2.2", "pytest-cov>=5.0.0"] | ||
lint = ["ruff>=0.5.0"] | ||
|
||
[tool.pytest.ini_options] | ||
pythonpath = ["src"] | ||
testpaths = ["tests"] | ||
testpaths = "tests" |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from pub_ready_plots.pub_ready_plots import get_context, get_mpl_rcParams | ||
import numpy as np | ||
|
||
|
||
def test_correct_func(): | ||
nrows, ncols = 3, 2 | ||
with get_context(0.5, 0.15, nrows, ncols, "iclr") as (fig, axs): | ||
real_rc_params, fig_width_in, fig_height_in = get_mpl_rcParams( | ||
0.5, 0.15, "iclr" | ||
) | ||
|
||
assert np.allclose( | ||
fig.get_size_inches(), (fig_width_in, fig_height_in), atol=0.001 | ||
) | ||
assert isinstance(axs, np.ndarray) | ||
assert axs.shape == (nrows, ncols) |