'Figure' object has no attribute 'gcf'

Hi,
I am trying to write_image from PdPlotter. Whole code (below) is running okay except the last line.

#########################
from pymatgen.entries.compatibility import MaterialsProjectCompatibility
from pymatgen.analysis.phase_diagram import PhaseDiagram, PDPlotter
drone = VaspToComputedEntryDrone()
queen = BorgQueen(drone, rootpath=".")
entries = queen.get_data()
with MPRester() as m:
mp_entries = m.get_entries_in_chemsys([“Li”, “Fe”, “O”])

entries.extend(mp_entries)

compat = MaterialsProjectCompatibility()
entries = compat.process_entries(entries)
pd = PhaseDiagram(entries)
plotter = PDPlotter(pd)
plotter.write_image(‘test.png’, ‘png’) ### save figure
#############################

I am getting the following error…
Traceback (most recent call last):
File “example_3.py”, line 37, in
plotter.write_image(‘test.png’, ‘png’)
File “python/python-3.10.2/lib/python3.10/site-packages/pymatgen/analysis/phase_diagram.py”, line 2184, in write_image
f = plt.gcf()
AttributeError: ‘Figure’ object has no attribute ‘gcf’

Can someone help me with this error ? I don’t have Jupyter notebook installed on HPC, and only have the option to run pymatgen with python. How can I save the png file? Is there any other way?

Hi @Akansha_Singh,

The write_image method is currently only supported when using the matplotlib backend. If you change the line where you create your plotter object to:

plotter = PDPlotter(pd, backend=“matplotlib”)

Then the code you wrote should work. if you want to save a plotly Figure object instead, you should use their interface as such:

fig = plotter.get_plot()
fig.write_image(“test.png”)

This allows you also to modify the figure by changing various aspects of the layout dictionary (see the Plotly documentation for more). Note that you may need to install the kaleido package for saving Plotly figures.

Hope this helps!

1 Like