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

Breaking: Have plot methods return plt.Axes object, not matplotlib module #3237

Merged
merged 27 commits into from
Aug 17, 2023

Conversation

janosh
Copy link
Member

@janosh janosh commented Aug 13, 2023

Closes #3138.

pymatgen plotting methods using the matplotlib backend return the matplotlib.pyplot module (plt), rather than an object related to the plot itself, such as the Figure or Axes. This is suboptimal API design and inconsistent with the plotly backend which returns go.Figure.

This PR changes all plotting methods to return a plt.Axes object (same as pymatviz) instead of plt.

Reasons:

  • More Intuitive as Axes actually contains the plot attributes (lines, legend, labels, etc.), and is more in line with what users may expect and what they typically interact with when customizing plots.
  • Consistency with pymatviz and pymatgen's own plotly backend.

@janosh janosh added data viz PRs and issues about pymatgen plotting functionality breaking Breaking change api Application programming interface ux User experience labels Aug 13, 2023
E           AttributeError: 'Axes' object has no attribute 'subplot'

/home/runner/work/pymatgen/pymatgen/pymatgen/electronic_structure/plotter.py:1082
E           AttributeError: 'Axes' object has no attribute 'ylim'

/home/runner/work/pymatgen/pymatgen/pymatgen/electronic_structure/plotter.py:3834
E       AttributeError: 'Axes' object has no attribute 'gca'

/home/runner/work/pymatgen/pymatgen/pymatgen/electronic_structure/plotter.py:360:
E           AttributeError: 'Axes' object has no attribute 'ylim'

/home/runner/work/pymatgen/pymatgen/pymatgen/electronic_structure/plotter.py:3838: AttributeError
E           AttributeError: 'Axes' object has no attribute 'xlim'

pymatgen/electronic_structure/plotter.py:3836: AttributeError
E       AttributeError: 'Axes' object has no attribute 'gca'

/home/runner/work/pymatgen/pymatgen/pymatgen/phonon/plotter.py:266: AttributeError
E       AttributeError: 'Axes' object has no attribute 'setp'

pymatgen/electronic_structure/plotter.py:3875: AttributeError
@janosh janosh changed the title Plot methods return plt.Axes object, not plt module Breaking: Have plot methods return plt.Axes object, not matplotlib module Aug 17, 2023
    def test_plot(self):
        # Disabling latex for testing.
        from matplotlib import axes, rc

        rc("text", usetex=False)
        self.plotter.add_dos("Total", self.dos)
        self.plotter.get_plot(units="mev")
        self.plotter_nostack.add_dos("Total", self.dos)
        plt = self.plotter_nostack.get_plot(units="mev")

>       ax = plt.gca()

also attempt fix get_elt_projected_plots
>       assert plt.ylim() == (-4.0, 7.6348), "wrong ylim"

and

>       ax.set_title(ax.get_title(), size=width * 4)
E       AttributeError: 'numpy.ndarray' object has no attribute 'set_title'
@janosh janosh merged commit 5abfb63 into master Aug 17, 2023
19 of 24 checks passed
@janosh janosh deleted the return-plt-axes-not-plt branch August 17, 2023 22:35
@fraricci
Copy link
Contributor

fraricci commented Nov 2, 2023

If I can chime in here, I noticed a bug potentially introduced by this pull request.
As mentioned here, there is a "ax =" missing here

@janosh
Copy link
Member Author

janosh commented Nov 2, 2023

@fraricci Thanks for reporting and troubleshooting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Application programming interface breaking Breaking change data viz PRs and issues about pymatgen plotting functionality ux User experience
Projects
None yet
Development

Successfully merging this pull request may close these issues.

returning matplotlib.pyplot module in plotting methods
2 participants