Skip to content
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

Behavior of multiple plot_structure_2d() in one plt.figure #56

Closed
sgbaird opened this issue Oct 9, 2022 · 2 comments · Fixed by #57
Closed

Behavior of multiple plot_structure_2d() in one plt.figure #56

sgbaird opened this issue Oct 9, 2022 · 2 comments · Fixed by #57
Labels
question Further information is requested

Comments

@sgbaird
Copy link
Contributor

sgbaird commented Oct 9, 2022

Not really a bug, just something I noticed. Due to (I think) the following line:

ax = ax or plt.gca()

I get the somewhat unintuitive behavior where the plots overlap each other:

from pymatviz.structure_viz import plot_structure_2d
plots = train_structures.apply(plot_structure_2d)

image

plots then seems to be empty objects.

plots
146     AxesSubplot(0.1981,0.125;0.6288x0.755)
925     AxesSubplot(0.1981,0.125;0.6288x0.755)
1282    AxesSubplot(0.1981,0.125;0.6288x0.755)
Name: structure, dtype: object
plots.iloc[0]
<AxesSubplot:>

By itself works fine:

plot_structure_2d(train_structures.iloc[0])

image

@sgbaird
Copy link
Contributor Author

sgbaird commented Oct 9, 2022

Ok, this is more what I should be going for I think:

import matplotlib.pyplot as plt
fig, axes = plt.subplots(1, 3)
[plot_structure_2d(s, ax=ax) for s, ax in zip(train_structures.tolist(), axes)]

image

@janosh janosh changed the title behavior of multiple plot_structure_2d in single Jupyter cell Behavior of multiple plot_structure_2d() in one plt.figure Oct 10, 2022
@janosh janosh added the question Further information is requested label Oct 10, 2022
@janosh
Copy link
Owner

janosh commented Oct 10, 2022

I don't think there's a way for plot_structure_2d() to be smart about this since it can't know how many structures there are to plot in total and what the figure's axes layout should be.

Ok, this is more what I should be going for I think:

Yes, that's the intended usage. There are 2 somewhat hidden examples of this:

fig, axs = plt.subplots(3, 4, figsize=(12, 12))
for struct, ax in zip(df_perov.structure.head(12), axs.flat):
ax = plot_structure_2d(struct, ax=ax)
ax.set_title(struct.composition.reduced_formula, fontsize=14)

df_phonons = load_dataset("matbench_phonons")
n_rows, n_cols = 3, 4
fig, axs = plt.subplots(n_rows, n_cols, figsize=(3 * n_rows, 3 * n_cols))
for struct, ax in zip(df_phonons.structure.head(n_rows * n_cols), axs.flat):
ax = plot_structure_2d(struct, ax=ax)
spg_symbol, _ = struct.get_space_group_info()
formula = struct.composition.reduced_formula
ax.set_title(f"{formula} ({spg_symbol})", fontweight="bold")

Should probably add a multi-axes example to the plot_structure_2d() doc string to make it more discoverable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants