Surface slab visualization: Key Error 'O2-'

Hello everyone,
I’m new to pymatgen as well as python which might be already the reason for my moste likely easy preventable error. Nevertheless, I can’t fix it myself.

What i want:
I try to visualize the different 111 surface slabs i found via the slab generator.
Im using this code:

fig = plt.figure()
for n, slab in enumerate(slabs):
ax = fig.add_subplot(1, 2, n+1)
plot_slab(slab, ax, adsorption_sites= False)

and getting this error massage:

KeyError Traceback (most recent call last)
2 for n, slab in enumerate(slabs):
3 ax = fig.add_subplot(1, 2, n+1)
----> 4 plot_slab(slab, ax, adsorption_sites= False)
5 ax.settitle(n+1)
6 ax.set.xticks([])

~\Miniconda3\lib\site-packages\pymatgen\analysis\ in plot_slab(slab, ax, scale, repeat, window, draw_unit_cell, decay, adsorption_sites)
669 ax.add_patch(patches.Circle(coord[:2] - lattsum * (repeat // 2),
670 r, color=‘w’, zorder=2 * n))
–> 671 color = color_dict[sites[n].species_string]
672 ax.add_patch(patches.Circle(coord[:2] - lattsum * (repeat // 2), r,
673 facecolor=color, alpha=alphas[n],

KeyError: ‘O2-’

From what I understand of the error massage system of jupyter the problem is in the line 4 “plot_slab…” and in some kind of subroutine line 671 color. Right?

Does anyone has a hint for me how to fix this problem and give me a hint me how I could have found the solution myself. Is there for instance a documentation which could explain me the “key error : O2-”?
Thank you very much.

Best regards,

I’m not an expert on pymatgen, but the error you have is because there is a dictionary called color_dict that doesn’t have the key ‘O2-’ when it is searched for. You have an object sites that I’m assuming is a list of Sites objects from your defined Structure. I would look into how you defined your structure and see if there is some error how you defined it, and then I would look into how the color_dict dictionary is formed and see if there is some string that you may have typed in wrong.

Hope this helps

Good morning, thank you very much. I will try to dig int that.

Have a nice week.