-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add keyword hide_f_block: bool = None
(La and Ac series) to ptable
plotters
#140
Merged
+107
−51
Merged
Changes from 10 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f7d16f3
force some arg to be keyword
DanielYang59 355753c
allow hiding f-block
DanielYang59 92e9f68
add hide_f_block for scatters and lines
DanielYang59 3e6f1ce
update example
DanielYang59 ffc2340
add hide_f_block: bool = False to ptable_heatmap
janosh 7734edf
breaking rename `rare_earth_voffset` to `f_block_voffset`
DanielYang59 004aaab
Merge branch 'ptable_hide_la-ac' of github.com:DanielYang59/pymatviz …
DanielYang59 ef34ab7
Merge branch 'main' into ptable_hide_la-ac
DanielYang59 a149203
remove hidden axes
DanielYang59 05395ef
add tests for hide f-block
DanielYang59 cc59370
fix typo in comment
DanielYang59 4482478
parametrize hide_f_block=True|False to avoid code duplication in test…
janosh 3840d24
auto-enable hide_f_block if no data is provided for f-block elements
janosh 8683a6b
enable `hide_f_block` tests for lines and scatters plotter
DanielYang59 f886b57
Merge branch 'ptable_hide_la-ac' of github.com:DanielYang59/pymatviz …
DanielYang59 69ea949
fix IndexError: single positional indexer is out-of-bounds
janosh d006e67
avoid hardcode data col names
DanielYang59 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
|
@@ -318,9 +318,11 @@ class PTableProjector: | |
|
||
def __init__( | ||
self, | ||
*, | ||
data: SupportedDataType, | ||
colormap: str | Colormap | None, | ||
plot_kwargs: dict[str, Any] | None = None, | ||
hide_f_block: bool = False, | ||
) -> None: | ||
"""Initialize a ptable projector. | ||
|
||
|
@@ -332,17 +334,23 @@ def __init__( | |
colormap (str | Colormap | None): The colormap to use. | ||
plot_kwargs (dict): Additional keyword arguments to | ||
pass to the plt.subplots function call. | ||
hide_f_block: Hide f-block (Lanthanum and Actinium series). | ||
""" | ||
# Get colormap | ||
self.cmap: Colormap = colormap | ||
|
||
self.hide_f_block = hide_f_block | ||
|
||
# Preprocess data | ||
self.data: pd.DataFrame = data | ||
|
||
# Initialize periodic table canvas | ||
n_periods = df_ptable.row.max() | ||
n_groups = df_ptable.column.max() | ||
|
||
if self.hide_f_block: | ||
n_periods -= 3 | ||
DanielYang59 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Set figure size | ||
plot_kwargs = plot_kwargs or {} | ||
plot_kwargs.setdefault("figsize", (0.75 * n_groups, 0.75 * n_periods)) | ||
|
@@ -404,6 +412,7 @@ def add_child_plots( | |
self, | ||
child_plotter: Callable[[plt.axes, Any], None], | ||
child_args: dict[str, Any], | ||
*, | ||
ax_kwargs: dict[str, Any], | ||
on_empty: Literal["hide", "show"] = "hide", | ||
) -> None: | ||
|
@@ -416,6 +425,10 @@ def add_child_plots( | |
on_empty: Whether to "show" or "hide" tiles for elements without data. | ||
""" | ||
for element in Element: | ||
# Hide f-block | ||
if self.hide_f_block and (element.is_lanthanoid or element.is_actinoid): | ||
DanielYang59 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
continue | ||
|
||
# Get axis index by element symbol | ||
symbol: str = element.symbol | ||
row, column = df_ptable.loc[symbol, ["row", "column"]] | ||
|
@@ -459,6 +472,10 @@ def add_ele_symbols( | |
|
||
# Add symbol for each element | ||
for element in Element: | ||
# Hide f-block | ||
if self.hide_f_block and (element.is_lanthanoid or element.is_actinoid): | ||
continue | ||
|
||
# Get axis index by element symbol | ||
symbol: str = element.symbol | ||
row, column = df_ptable.loc[symbol, ["row", "column"]] | ||
|
@@ -473,6 +490,7 @@ def add_colorbar( | |
self, | ||
title: str, | ||
coords: tuple[float, float, float, float] = (0.18, 0.8, 0.42, 0.02), | ||
*, | ||
cbar_kwargs: dict[str, Any] | None = None, | ||
title_kwargs: dict[str, Any] | None = None, | ||
) -> None: | ||
|
@@ -635,7 +653,8 @@ def ptable_heatmap( | |
label_font_size: int = 16, | ||
value_font_size: int = 12, | ||
tile_size: float | tuple[float, float] = 0.9, | ||
rare_earth_voffset: float = 0.5, | ||
f_block_voffset: float = 0.5, | ||
hide_f_block: bool = False, | ||
**kwargs: Any, | ||
) -> plt.Axes: | ||
"""Plot a heatmap across the periodic table of elements. | ||
|
@@ -702,8 +721,10 @@ def ptable_heatmap( | |
cbar_coords (tuple[float, float, float, float]): Color bar position and size: | ||
[x, y, width, height] anchored at lower left corner of the bar. Defaults to | ||
(0.18, 0.8, 0.42, 0.05). | ||
rare_earth_voffset (float): Vertical offset for lanthanides and actinides | ||
f_block_voffset (float): Vertical offset for lanthanides and actinides | ||
(row 6 and 7) from the rest of the periodic table. Defaults to 0.5. | ||
hide_f_block (bool): Whether to hide the f-block (lanthanides and actinides). | ||
Defaults to False. | ||
**kwargs: Additional keyword arguments passed to plt.figure(). | ||
|
||
Returns: | ||
|
@@ -778,6 +799,9 @@ def tick_fmt(val: float, _pos: int) -> str: | |
) | (text_style or {}) | ||
|
||
for symbol, row, column, *_ in df_ptable.itertuples(): | ||
if hide_f_block and (row in (6, 7)): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought I would do this after refactor all other plotters with the new |
||
continue | ||
|
||
period = n_rows - row # invert row count to make periodic table right side up | ||
tile_value = values.get(symbol) | ||
|
||
|
@@ -814,7 +838,7 @@ def tick_fmt(val: float, _pos: int) -> str: | |
# replace shortens scientific notation 1e+01 to 1e1 so it fits inside cells | ||
label = label.replace("e+0", "e") | ||
if period < 3: # vertical offset for lanthanides + actinides | ||
period += rare_earth_voffset | ||
period += f_block_voffset | ||
rect = Rectangle( | ||
(column, period), tile_width, tile_height, edgecolor="gray", facecolor=color | ||
) | ||
|
@@ -900,6 +924,7 @@ def ptable_heatmap_splits( | |
cbar_coords: tuple[float, float, float, float] = (0.18, 0.8, 0.42, 0.02), | ||
cbar_title: str = "Values", | ||
on_empty: Literal["hide", "show"] = "hide", | ||
hide_f_block: bool = False, | ||
ax_kwargs: dict[str, Any] | None = None, | ||
symbol_kwargs: dict[str, Any] | None = None, | ||
plot_kwargs: dict[str, Any] | ||
|
@@ -943,6 +968,7 @@ def ptable_heatmap_splits( | |
cbar_kwargs (dict): Keyword arguments passed to fig.colorbar(). | ||
on_empty ('hide' | 'show'): Whether to show or hide tiles for elements without | ||
data. Defaults to "hide". | ||
hide_f_block (bool): Hide f-block (Lanthanum and Actinium series). | ||
plot_kwargs (dict): Additional keyword arguments to | ||
pass to the plt.subplots function call. | ||
|
||
|
@@ -964,6 +990,7 @@ def ptable_heatmap_splits( | |
data=data, | ||
colormap=colormap, | ||
plot_kwargs=plot_kwargs, # type: ignore[arg-type] | ||
hide_f_block=hide_f_block, | ||
) | ||
|
||
# Call child plotter: evenly split rectangle | ||
|
@@ -1548,6 +1575,7 @@ def ptable_scatters( | |
symbol_text: str | Callable[[Element], str] = lambda elem: elem.symbol, | ||
symbol_pos: tuple[float, float] = (0.5, 0.8), | ||
on_empty: Literal["hide", "show"] = "hide", | ||
hide_f_block: bool = False, | ||
plot_kwargs: dict[str, Any] | ||
| Callable[[Sequence[float]], dict[str, Any]] | ||
| None = None, | ||
|
@@ -1579,6 +1607,7 @@ def ptable_scatters( | |
Defaults to (0.5, 0.5). (1, 1) is the upper right corner. | ||
on_empty ('hide' | 'show'): Whether to show or hide tiles for elements without | ||
data. Defaults to "hide". | ||
hide_f_block (bool): Hide f-block (Lanthanum and Actinium series). | ||
child_args: Arguments to pass to the child plotter call. | ||
plot_kwargs (dict): Additional keyword arguments to | ||
pass to the plt.subplots function call. | ||
|
@@ -1595,7 +1624,12 @@ def ptable_scatters( | |
symbol_kwargs.setdefault("fontsize", 12) | ||
|
||
# Initialize periodic table plotter | ||
plotter = PTableProjector(data=data, colormap=None, plot_kwargs=plot_kwargs) # type: ignore[arg-type] | ||
plotter = PTableProjector( | ||
data=data, | ||
colormap=None, | ||
plot_kwargs=plot_kwargs, # type: ignore[arg-type] | ||
hide_f_block=hide_f_block, | ||
) | ||
|
||
# Call child plotter: Scatter | ||
plotter.add_child_plots( | ||
|
@@ -1620,6 +1654,7 @@ def ptable_lines( | |
symbol_text: str | Callable[[Element], str] = lambda elem: elem.symbol, | ||
symbol_pos: tuple[float, float] = (0.5, 0.8), | ||
on_empty: Literal["hide", "show"] = "hide", | ||
hide_f_block: bool = False, | ||
plot_kwargs: dict[str, Any] | ||
| Callable[[Sequence[float]], dict[str, Any]] | ||
| None = None, | ||
|
@@ -1651,6 +1686,7 @@ def ptable_lines( | |
Defaults to (0.5, 0.5). (1, 1) is the upper right corner. | ||
on_empty ('hide' | 'show'): Whether to show or hide tiles for elements without | ||
data. Defaults to "hide". | ||
hide_f_block (bool): Hide f-block (Lanthanum and Actinium series). | ||
child_args: Arguments to pass to the child plotter call. | ||
plot_kwargs (dict): Additional keyword arguments to | ||
pass to the plt.subplots function call. | ||
|
@@ -1669,6 +1705,7 @@ def ptable_lines( | |
data=data, | ||
colormap=None, | ||
plot_kwargs=plot_kwargs, # type: ignore[arg-type] | ||
hide_f_block=hide_f_block, | ||
) | ||
|
||
# Call child plotter: line | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice, much better now. didn't actually notice all the white space before