Download IPython notebook here. Binder badge.

GitHub issues by-label

BandsPlot

[1]:
import sisl
import sisl.viz
# This is just for convenience to retreive files
siesta_files = sisl._environ.get_environ_variable("SISL_FILES_TESTS") / "sisl" / "io" / "siesta"

Let’s get a bands_plot from a .bands file

[2]:
bands_plot = sisl.get_sile( siesta_files / "SrTiO3.bands").plot()
info:0: SislInfo: The plot has been initialized correctly, but the current settings were not enough to generate the figure.
Error: Could not read or generate data for BandsPlot from any of the possible sources.
Here are the errors for each source:
        - bands file: FileNotFoundError.[Errno 2] No such file or directory: '_THIS_DIRECTORY_DOES_NOT_EXIST_/sisl/io/siesta/SrTiO3.bands'
        - aiida bands: AttributeError.'NoneType' object has no attribute '_get_bandplot_data'
        - wfsx file: TypeError.expected str, bytes or os.PathLike object, not NoneType
        - band structure: ValueError.No band structure (k points path) was provided

and see what we’ve got:

[3]:
bands_plot
'<sisl.viz.plots.bands.BandsPlot object at 0x7efce7b55600>'

Getting the bands that you want

By default, BandsPlot gives you the 15 bands below and above 0 eV (which is interpreted as the fermi level).

There are two main ways to specify the bands that you want to display: Erange and bands_range.

As you may have guessed, Erange specifies the energy range that is displayed:

[4]:
bands_plot.update_settings(Erange=[-10, 10])
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [4], in <cell line: 1>()
----> 1 bands_plot.update_settings(Erange=[-10, 10])

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:440, in ConfigurableMeta.__new__.<locals>.update_settings(self, *args, **kwargs)
    439 def update_settings(self, *args, **kwargs):
--> 440     return self._update_settings(*args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:561, in Configurable._update_settings(self, run_updates, **kwargs)
    559     #Do things after updating the settings
    560     if len(self.settings_history.last_updated) > 0 and run_updates:
--> 561         self._run_updates(self.settings_history.last_updated)
    563 return self

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:589, in Configurable._run_updates(self, for_keys)
    587 # Execute the functions that we need to execute.
    588 for f_name in func_names:
--> 589     getattr(self, f_name)()
    591 return self

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plotutils.py:829, in repeat_if_children.<locals>.apply_to_all_plots(obj, children_sel, *args, **kwargs)
    825     obj.get_figure()
    827 else:
--> 829     return method(obj, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:891, in vizplotly_settings.<locals>.decorator.<locals>.func(obj, *args, **kwargs)
    888 @wraps(method)
    889 def func(obj, *args, **kwargs):
    890     getattr(obj, method_name)(**kwargs, **extra_kwargs)
--> 891     return method(obj, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:1108, in Plot.set_data(self, update_fig, **kwargs)
   1100 @repeat_if_children
   1101 @vizplotly_settings('before')
   1102 def set_data(self, update_fig = True, **kwargs):
   1103     """ Method to process the data that has been read beforehand by read_data() and prepare the figure
   1104
   1105     If everything is succesful, it calls the next step in plotting (`get_figure`)
   1106     """
-> 1108     self._for_backend = self._set_data()
   1110     if update_fig:
   1111         self.get_figure()

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:972, in _populate_with_settings.<locals>.f_default_setting_args(self, *args, **kwargs)
    969         except KeyError:
    970             pass
--> 972 return f(self, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plots/bands.py:645, in BandsPlot._set_data(self, Erange, E0, bands_range, spin, spin_texture_colorscale, bands_width, bands_color, spindown_color, gap, gap_tol, gap_color, direct_gaps_only, custom_gaps)
    642 def _set_data(self, Erange, E0, bands_range, spin, spin_texture_colorscale, bands_width, bands_color, spindown_color,
    643     gap, gap_tol, gap_color, direct_gaps_only, custom_gaps):
    644     # Calculate all the gaps of this band structure
--> 645     self._calculate_gaps(E0)
    647     # Shift all the bands to the reference
    648     filtered_bands = self.bands - E0

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:972, in _populate_with_settings.<locals>.f_default_setting_args(self, *args, **kwargs)
    969         except KeyError:
    970             pass
--> 972 return f(self, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plots/bands.py:714, in BandsPlot._calculate_gaps(self, E0)
    708 """
    709 Calculates the gap (or gaps) assuming 0 is the fermi level.
    710
    711 It creates the attributes `gap` and `gap_info`
    712 """
    713 # Calculate the band gap to store it
--> 714 shifted_bands = self.bands - E0
    715 above_fermi = self.bands.where(shifted_bands > 0)
    716 below_fermi = self.bands.where(shifted_bands < 0)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:723, in Plot.__getattr__(self, key)
    720     except (KeyError, AttributeError):
    721         pass
--> 723 raise AttributeError(f"The attribute '{key}' was not found either in the plot, its backend, or in shared attributes.")

AttributeError: The attribute 'bands' was not found either in the plot, its backend, or in shared attributes.

while with bands_range you can actually indicate the indices.

However, note that ``Erange`` has preference over ``bands_range``, therefore you need to set Erange to None if you want the change to take effect.

[5]:
bands_plot.update_settings(bands_range=[6, 15], Erange=None)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [5], in <cell line: 1>()
----> 1 bands_plot.update_settings(bands_range=[6, 15], Erange=None)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:440, in ConfigurableMeta.__new__.<locals>.update_settings(self, *args, **kwargs)
    439 def update_settings(self, *args, **kwargs):
--> 440     return self._update_settings(*args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:561, in Configurable._update_settings(self, run_updates, **kwargs)
    559     #Do things after updating the settings
    560     if len(self.settings_history.last_updated) > 0 and run_updates:
--> 561         self._run_updates(self.settings_history.last_updated)
    563 return self

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:589, in Configurable._run_updates(self, for_keys)
    587 # Execute the functions that we need to execute.
    588 for f_name in func_names:
--> 589     getattr(self, f_name)()
    591 return self

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plotutils.py:829, in repeat_if_children.<locals>.apply_to_all_plots(obj, children_sel, *args, **kwargs)
    825     obj.get_figure()
    827 else:
--> 829     return method(obj, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:891, in vizplotly_settings.<locals>.decorator.<locals>.func(obj, *args, **kwargs)
    888 @wraps(method)
    889 def func(obj, *args, **kwargs):
    890     getattr(obj, method_name)(**kwargs, **extra_kwargs)
--> 891     return method(obj, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:1108, in Plot.set_data(self, update_fig, **kwargs)
   1100 @repeat_if_children
   1101 @vizplotly_settings('before')
   1102 def set_data(self, update_fig = True, **kwargs):
   1103     """ Method to process the data that has been read beforehand by read_data() and prepare the figure
   1104
   1105     If everything is succesful, it calls the next step in plotting (`get_figure`)
   1106     """
-> 1108     self._for_backend = self._set_data()
   1110     if update_fig:
   1111         self.get_figure()

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:972, in _populate_with_settings.<locals>.f_default_setting_args(self, *args, **kwargs)
    969         except KeyError:
    970             pass
--> 972 return f(self, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plots/bands.py:645, in BandsPlot._set_data(self, Erange, E0, bands_range, spin, spin_texture_colorscale, bands_width, bands_color, spindown_color, gap, gap_tol, gap_color, direct_gaps_only, custom_gaps)
    642 def _set_data(self, Erange, E0, bands_range, spin, spin_texture_colorscale, bands_width, bands_color, spindown_color,
    643     gap, gap_tol, gap_color, direct_gaps_only, custom_gaps):
    644     # Calculate all the gaps of this band structure
--> 645     self._calculate_gaps(E0)
    647     # Shift all the bands to the reference
    648     filtered_bands = self.bands - E0

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:972, in _populate_with_settings.<locals>.f_default_setting_args(self, *args, **kwargs)
    969         except KeyError:
    970             pass
--> 972 return f(self, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plots/bands.py:714, in BandsPlot._calculate_gaps(self, E0)
    708 """
    709 Calculates the gap (or gaps) assuming 0 is the fermi level.
    710
    711 It creates the attributes `gap` and `gap_info`
    712 """
    713 # Calculate the band gap to store it
--> 714 shifted_bands = self.bands - E0
    715 above_fermi = self.bands.where(shifted_bands > 0)
    716 below_fermi = self.bands.where(shifted_bands < 0)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:723, in Plot.__getattr__(self, key)
    720     except (KeyError, AttributeError):
    721         pass
--> 723 raise AttributeError(f"The attribute '{key}' was not found either in the plot, its backend, or in shared attributes.")

AttributeError: The attribute 'bands' was not found either in the plot, its backend, or in shared attributes.

If your fermi level is not correctly set or you want a different energy reference, you can provide a value for E0 to specify where your 0 should be and the bands to display will be automatically calculated from that.

However, if you want to update E0 after the plot has been build and you want BandsPlot to recalculate the bands for you you will need to set Erange and bands_range to None again.

[6]:
bands_plot.update_settings(E0=-10, bands_range=None, Erange=None)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [6], in <cell line: 1>()
----> 1 bands_plot.update_settings(E0=-10, bands_range=None, Erange=None)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:440, in ConfigurableMeta.__new__.<locals>.update_settings(self, *args, **kwargs)
    439 def update_settings(self, *args, **kwargs):
--> 440     return self._update_settings(*args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:561, in Configurable._update_settings(self, run_updates, **kwargs)
    559     #Do things after updating the settings
    560     if len(self.settings_history.last_updated) > 0 and run_updates:
--> 561         self._run_updates(self.settings_history.last_updated)
    563 return self

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:589, in Configurable._run_updates(self, for_keys)
    587 # Execute the functions that we need to execute.
    588 for f_name in func_names:
--> 589     getattr(self, f_name)()
    591 return self

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plotutils.py:829, in repeat_if_children.<locals>.apply_to_all_plots(obj, children_sel, *args, **kwargs)
    825     obj.get_figure()
    827 else:
--> 829     return method(obj, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:891, in vizplotly_settings.<locals>.decorator.<locals>.func(obj, *args, **kwargs)
    888 @wraps(method)
    889 def func(obj, *args, **kwargs):
    890     getattr(obj, method_name)(**kwargs, **extra_kwargs)
--> 891     return method(obj, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:1108, in Plot.set_data(self, update_fig, **kwargs)
   1100 @repeat_if_children
   1101 @vizplotly_settings('before')
   1102 def set_data(self, update_fig = True, **kwargs):
   1103     """ Method to process the data that has been read beforehand by read_data() and prepare the figure
   1104
   1105     If everything is succesful, it calls the next step in plotting (`get_figure`)
   1106     """
-> 1108     self._for_backend = self._set_data()
   1110     if update_fig:
   1111         self.get_figure()

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:972, in _populate_with_settings.<locals>.f_default_setting_args(self, *args, **kwargs)
    969         except KeyError:
    970             pass
--> 972 return f(self, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plots/bands.py:645, in BandsPlot._set_data(self, Erange, E0, bands_range, spin, spin_texture_colorscale, bands_width, bands_color, spindown_color, gap, gap_tol, gap_color, direct_gaps_only, custom_gaps)
    642 def _set_data(self, Erange, E0, bands_range, spin, spin_texture_colorscale, bands_width, bands_color, spindown_color,
    643     gap, gap_tol, gap_color, direct_gaps_only, custom_gaps):
    644     # Calculate all the gaps of this band structure
--> 645     self._calculate_gaps(E0)
    647     # Shift all the bands to the reference
    648     filtered_bands = self.bands - E0

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:972, in _populate_with_settings.<locals>.f_default_setting_args(self, *args, **kwargs)
    969         except KeyError:
    970             pass
--> 972 return f(self, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plots/bands.py:714, in BandsPlot._calculate_gaps(self, E0)
    708 """
    709 Calculates the gap (or gaps) assuming 0 is the fermi level.
    710
    711 It creates the attributes `gap` and `gap_info`
    712 """
    713 # Calculate the band gap to store it
--> 714 shifted_bands = self.bands - E0
    715 above_fermi = self.bands.where(shifted_bands > 0)
    716 below_fermi = self.bands.where(shifted_bands < 0)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:723, in Plot.__getattr__(self, key)
    720     except (KeyError, AttributeError):
    721         pass
--> 723 raise AttributeError(f"The attribute '{key}' was not found either in the plot, its backend, or in shared attributes.")

AttributeError: The attribute 'bands' was not found either in the plot, its backend, or in shared attributes.

Notice how only 25 bands are displayed now: the only 10 that are below 0 eV (there are no lower states) and 15 above 0 eV.

[7]:
# Set them back to "normal"
bands_plot = bands_plot.update_settings(E0=0, bands_range=None, Erange=None)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [7], in <cell line: 2>()
      1 # Set them back to "normal"
----> 2 bands_plot = bands_plot.update_settings(E0=0, bands_range=None, Erange=None)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:440, in ConfigurableMeta.__new__.<locals>.update_settings(self, *args, **kwargs)
    439 def update_settings(self, *args, **kwargs):
--> 440     return self._update_settings(*args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:561, in Configurable._update_settings(self, run_updates, **kwargs)
    559     #Do things after updating the settings
    560     if len(self.settings_history.last_updated) > 0 and run_updates:
--> 561         self._run_updates(self.settings_history.last_updated)
    563 return self

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:589, in Configurable._run_updates(self, for_keys)
    587 # Execute the functions that we need to execute.
    588 for f_name in func_names:
--> 589     getattr(self, f_name)()
    591 return self

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plotutils.py:829, in repeat_if_children.<locals>.apply_to_all_plots(obj, children_sel, *args, **kwargs)
    825     obj.get_figure()
    827 else:
--> 829     return method(obj, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:891, in vizplotly_settings.<locals>.decorator.<locals>.func(obj, *args, **kwargs)
    888 @wraps(method)
    889 def func(obj, *args, **kwargs):
    890     getattr(obj, method_name)(**kwargs, **extra_kwargs)
--> 891     return method(obj, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:1108, in Plot.set_data(self, update_fig, **kwargs)
   1100 @repeat_if_children
   1101 @vizplotly_settings('before')
   1102 def set_data(self, update_fig = True, **kwargs):
   1103     """ Method to process the data that has been read beforehand by read_data() and prepare the figure
   1104
   1105     If everything is succesful, it calls the next step in plotting (`get_figure`)
   1106     """
-> 1108     self._for_backend = self._set_data()
   1110     if update_fig:
   1111         self.get_figure()

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:972, in _populate_with_settings.<locals>.f_default_setting_args(self, *args, **kwargs)
    969         except KeyError:
    970             pass
--> 972 return f(self, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plots/bands.py:645, in BandsPlot._set_data(self, Erange, E0, bands_range, spin, spin_texture_colorscale, bands_width, bands_color, spindown_color, gap, gap_tol, gap_color, direct_gaps_only, custom_gaps)
    642 def _set_data(self, Erange, E0, bands_range, spin, spin_texture_colorscale, bands_width, bands_color, spindown_color,
    643     gap, gap_tol, gap_color, direct_gaps_only, custom_gaps):
    644     # Calculate all the gaps of this band structure
--> 645     self._calculate_gaps(E0)
    647     # Shift all the bands to the reference
    648     filtered_bands = self.bands - E0

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:972, in _populate_with_settings.<locals>.f_default_setting_args(self, *args, **kwargs)
    969         except KeyError:
    970             pass
--> 972 return f(self, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plots/bands.py:714, in BandsPlot._calculate_gaps(self, E0)
    708 """
    709 Calculates the gap (or gaps) assuming 0 is the fermi level.
    710
    711 It creates the attributes `gap` and `gap_info`
    712 """
    713 # Calculate the band gap to store it
--> 714 shifted_bands = self.bands - E0
    715 above_fermi = self.bands.where(shifted_bands > 0)
    716 below_fermi = self.bands.where(shifted_bands < 0)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:723, in Plot.__getattr__(self, key)
    720     except (KeyError, AttributeError):
    721         pass
--> 723 raise AttributeError(f"The attribute '{key}' was not found either in the plot, its backend, or in shared attributes.")

AttributeError: The attribute 'bands' was not found either in the plot, its backend, or in shared attributes.

Notice that in spin polarized bands, you can select the spins to display using the ``spin`` setting, just pass a list of spin components (e.g. spin=[0]).

Quick styling

If all you want is to change the color and width of the bands, there’s one simple solution: use the bands_color and bands_width settings.

Let’s show them in red:

[8]:
bands_plot.update_settings(bands_color="red")
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [8], in <cell line: 1>()
----> 1 bands_plot.update_settings(bands_color="red")

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:440, in ConfigurableMeta.__new__.<locals>.update_settings(self, *args, **kwargs)
    439 def update_settings(self, *args, **kwargs):
--> 440     return self._update_settings(*args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:561, in Configurable._update_settings(self, run_updates, **kwargs)
    559     #Do things after updating the settings
    560     if len(self.settings_history.last_updated) > 0 and run_updates:
--> 561         self._run_updates(self.settings_history.last_updated)
    563 return self

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:589, in Configurable._run_updates(self, for_keys)
    587 # Execute the functions that we need to execute.
    588 for f_name in func_names:
--> 589     getattr(self, f_name)()
    591 return self

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plotutils.py:829, in repeat_if_children.<locals>.apply_to_all_plots(obj, children_sel, *args, **kwargs)
    825     obj.get_figure()
    827 else:
--> 829     return method(obj, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:891, in vizplotly_settings.<locals>.decorator.<locals>.func(obj, *args, **kwargs)
    888 @wraps(method)
    889 def func(obj, *args, **kwargs):
    890     getattr(obj, method_name)(**kwargs, **extra_kwargs)
--> 891     return method(obj, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:1108, in Plot.set_data(self, update_fig, **kwargs)
   1100 @repeat_if_children
   1101 @vizplotly_settings('before')
   1102 def set_data(self, update_fig = True, **kwargs):
   1103     """ Method to process the data that has been read beforehand by read_data() and prepare the figure
   1104
   1105     If everything is succesful, it calls the next step in plotting (`get_figure`)
   1106     """
-> 1108     self._for_backend = self._set_data()
   1110     if update_fig:
   1111         self.get_figure()

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:972, in _populate_with_settings.<locals>.f_default_setting_args(self, *args, **kwargs)
    969         except KeyError:
    970             pass
--> 972 return f(self, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plots/bands.py:645, in BandsPlot._set_data(self, Erange, E0, bands_range, spin, spin_texture_colorscale, bands_width, bands_color, spindown_color, gap, gap_tol, gap_color, direct_gaps_only, custom_gaps)
    642 def _set_data(self, Erange, E0, bands_range, spin, spin_texture_colorscale, bands_width, bands_color, spindown_color,
    643     gap, gap_tol, gap_color, direct_gaps_only, custom_gaps):
    644     # Calculate all the gaps of this band structure
--> 645     self._calculate_gaps(E0)
    647     # Shift all the bands to the reference
    648     filtered_bands = self.bands - E0

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:972, in _populate_with_settings.<locals>.f_default_setting_args(self, *args, **kwargs)
    969         except KeyError:
    970             pass
--> 972 return f(self, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plots/bands.py:714, in BandsPlot._calculate_gaps(self, E0)
    708 """
    709 Calculates the gap (or gaps) assuming 0 is the fermi level.
    710
    711 It creates the attributes `gap` and `gap_info`
    712 """
    713 # Calculate the band gap to store it
--> 714 shifted_bands = self.bands - E0
    715 above_fermi = self.bands.where(shifted_bands > 0)
    716 below_fermi = self.bands.where(shifted_bands < 0)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:723, in Plot.__getattr__(self, key)
    720     except (KeyError, AttributeError):
    721         pass
--> 723 raise AttributeError(f"The attribute '{key}' was not found either in the plot, its backend, or in shared attributes.")

AttributeError: The attribute 'bands' was not found either in the plot, its backend, or in shared attributes.

And now in green but also make them wider:

[9]:
bands_plot.update_settings(bands_color="green", bands_width=3)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [9], in <cell line: 1>()
----> 1 bands_plot.update_settings(bands_color="green", bands_width=3)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:440, in ConfigurableMeta.__new__.<locals>.update_settings(self, *args, **kwargs)
    439 def update_settings(self, *args, **kwargs):
--> 440     return self._update_settings(*args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:561, in Configurable._update_settings(self, run_updates, **kwargs)
    559     #Do things after updating the settings
    560     if len(self.settings_history.last_updated) > 0 and run_updates:
--> 561         self._run_updates(self.settings_history.last_updated)
    563 return self

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:589, in Configurable._run_updates(self, for_keys)
    587 # Execute the functions that we need to execute.
    588 for f_name in func_names:
--> 589     getattr(self, f_name)()
    591 return self

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plotutils.py:829, in repeat_if_children.<locals>.apply_to_all_plots(obj, children_sel, *args, **kwargs)
    825     obj.get_figure()
    827 else:
--> 829     return method(obj, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:891, in vizplotly_settings.<locals>.decorator.<locals>.func(obj, *args, **kwargs)
    888 @wraps(method)
    889 def func(obj, *args, **kwargs):
    890     getattr(obj, method_name)(**kwargs, **extra_kwargs)
--> 891     return method(obj, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:1108, in Plot.set_data(self, update_fig, **kwargs)
   1100 @repeat_if_children
   1101 @vizplotly_settings('before')
   1102 def set_data(self, update_fig = True, **kwargs):
   1103     """ Method to process the data that has been read beforehand by read_data() and prepare the figure
   1104
   1105     If everything is succesful, it calls the next step in plotting (`get_figure`)
   1106     """
-> 1108     self._for_backend = self._set_data()
   1110     if update_fig:
   1111         self.get_figure()

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:972, in _populate_with_settings.<locals>.f_default_setting_args(self, *args, **kwargs)
    969         except KeyError:
    970             pass
--> 972 return f(self, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plots/bands.py:645, in BandsPlot._set_data(self, Erange, E0, bands_range, spin, spin_texture_colorscale, bands_width, bands_color, spindown_color, gap, gap_tol, gap_color, direct_gaps_only, custom_gaps)
    642 def _set_data(self, Erange, E0, bands_range, spin, spin_texture_colorscale, bands_width, bands_color, spindown_color,
    643     gap, gap_tol, gap_color, direct_gaps_only, custom_gaps):
    644     # Calculate all the gaps of this band structure
--> 645     self._calculate_gaps(E0)
    647     # Shift all the bands to the reference
    648     filtered_bands = self.bands - E0

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:972, in _populate_with_settings.<locals>.f_default_setting_args(self, *args, **kwargs)
    969         except KeyError:
    970             pass
--> 972 return f(self, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plots/bands.py:714, in BandsPlot._calculate_gaps(self, E0)
    708 """
    709 Calculates the gap (or gaps) assuming 0 is the fermi level.
    710
    711 It creates the attributes `gap` and `gap_info`
    712 """
    713 # Calculate the band gap to store it
--> 714 shifted_bands = self.bands - E0
    715 above_fermi = self.bands.where(shifted_bands > 0)
    716 below_fermi = self.bands.where(shifted_bands < 0)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:723, in Plot.__getattr__(self, key)
    720     except (KeyError, AttributeError):
    721         pass
--> 723 raise AttributeError(f"The attribute '{key}' was not found either in the plot, its backend, or in shared attributes.")

AttributeError: The attribute 'bands' was not found either in the plot, its backend, or in shared attributes.

If you have spin polarized bands, bands_color will modify the color of the first spin channel, while the second one can be tuned with spindown_color.

[10]:
bands_plot = bands_plot.update_settings(bands_color="black", bands_width=1)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [10], in <cell line: 1>()
----> 1 bands_plot = bands_plot.update_settings(bands_color="black", bands_width=1)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:440, in ConfigurableMeta.__new__.<locals>.update_settings(self, *args, **kwargs)
    439 def update_settings(self, *args, **kwargs):
--> 440     return self._update_settings(*args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:561, in Configurable._update_settings(self, run_updates, **kwargs)
    559     #Do things after updating the settings
    560     if len(self.settings_history.last_updated) > 0 and run_updates:
--> 561         self._run_updates(self.settings_history.last_updated)
    563 return self

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:589, in Configurable._run_updates(self, for_keys)
    587 # Execute the functions that we need to execute.
    588 for f_name in func_names:
--> 589     getattr(self, f_name)()
    591 return self

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plotutils.py:829, in repeat_if_children.<locals>.apply_to_all_plots(obj, children_sel, *args, **kwargs)
    825     obj.get_figure()
    827 else:
--> 829     return method(obj, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:891, in vizplotly_settings.<locals>.decorator.<locals>.func(obj, *args, **kwargs)
    888 @wraps(method)
    889 def func(obj, *args, **kwargs):
    890     getattr(obj, method_name)(**kwargs, **extra_kwargs)
--> 891     return method(obj, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:1108, in Plot.set_data(self, update_fig, **kwargs)
   1100 @repeat_if_children
   1101 @vizplotly_settings('before')
   1102 def set_data(self, update_fig = True, **kwargs):
   1103     """ Method to process the data that has been read beforehand by read_data() and prepare the figure
   1104
   1105     If everything is succesful, it calls the next step in plotting (`get_figure`)
   1106     """
-> 1108     self._for_backend = self._set_data()
   1110     if update_fig:
   1111         self.get_figure()

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:972, in _populate_with_settings.<locals>.f_default_setting_args(self, *args, **kwargs)
    969         except KeyError:
    970             pass
--> 972 return f(self, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plots/bands.py:645, in BandsPlot._set_data(self, Erange, E0, bands_range, spin, spin_texture_colorscale, bands_width, bands_color, spindown_color, gap, gap_tol, gap_color, direct_gaps_only, custom_gaps)
    642 def _set_data(self, Erange, E0, bands_range, spin, spin_texture_colorscale, bands_width, bands_color, spindown_color,
    643     gap, gap_tol, gap_color, direct_gaps_only, custom_gaps):
    644     # Calculate all the gaps of this band structure
--> 645     self._calculate_gaps(E0)
    647     # Shift all the bands to the reference
    648     filtered_bands = self.bands - E0

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:972, in _populate_with_settings.<locals>.f_default_setting_args(self, *args, **kwargs)
    969         except KeyError:
    970             pass
--> 972 return f(self, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plots/bands.py:714, in BandsPlot._calculate_gaps(self, E0)
    708 """
    709 Calculates the gap (or gaps) assuming 0 is the fermi level.
    710
    711 It creates the attributes `gap` and `gap_info`
    712 """
    713 # Calculate the band gap to store it
--> 714 shifted_bands = self.bands - E0
    715 above_fermi = self.bands.where(shifted_bands > 0)
    716 below_fermi = self.bands.where(shifted_bands < 0)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:723, in Plot.__getattr__(self, key)
    720     except (KeyError, AttributeError):
    721         pass
--> 723 raise AttributeError(f"The attribute '{key}' was not found either in the plot, its backend, or in shared attributes.")

AttributeError: The attribute 'bands' was not found either in the plot, its backend, or in shared attributes.

Displaying the smallest gaps

The easiest thing to do is to let BandsPlot discover where the (minimum) gaps are.

This is indicated by setting the gap parameter to True. One can also use gap_color if a particular color is desired.

[11]:
bands_plot.update_settings(gap=True, gap_color="green", Erange=[-10,10]) # We reduce Erange just to see it better
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [11], in <cell line: 1>()
----> 1 bands_plot.update_settings(gap=True, gap_color="green", Erange=[-10,10])

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:440, in ConfigurableMeta.__new__.<locals>.update_settings(self, *args, **kwargs)
    439 def update_settings(self, *args, **kwargs):
--> 440     return self._update_settings(*args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:561, in Configurable._update_settings(self, run_updates, **kwargs)
    559     #Do things after updating the settings
    560     if len(self.settings_history.last_updated) > 0 and run_updates:
--> 561         self._run_updates(self.settings_history.last_updated)
    563 return self

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:589, in Configurable._run_updates(self, for_keys)
    587 # Execute the functions that we need to execute.
    588 for f_name in func_names:
--> 589     getattr(self, f_name)()
    591 return self

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plotutils.py:829, in repeat_if_children.<locals>.apply_to_all_plots(obj, children_sel, *args, **kwargs)
    825     obj.get_figure()
    827 else:
--> 829     return method(obj, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:891, in vizplotly_settings.<locals>.decorator.<locals>.func(obj, *args, **kwargs)
    888 @wraps(method)
    889 def func(obj, *args, **kwargs):
    890     getattr(obj, method_name)(**kwargs, **extra_kwargs)
--> 891     return method(obj, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:1108, in Plot.set_data(self, update_fig, **kwargs)
   1100 @repeat_if_children
   1101 @vizplotly_settings('before')
   1102 def set_data(self, update_fig = True, **kwargs):
   1103     """ Method to process the data that has been read beforehand by read_data() and prepare the figure
   1104
   1105     If everything is succesful, it calls the next step in plotting (`get_figure`)
   1106     """
-> 1108     self._for_backend = self._set_data()
   1110     if update_fig:
   1111         self.get_figure()

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:972, in _populate_with_settings.<locals>.f_default_setting_args(self, *args, **kwargs)
    969         except KeyError:
    970             pass
--> 972 return f(self, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plots/bands.py:645, in BandsPlot._set_data(self, Erange, E0, bands_range, spin, spin_texture_colorscale, bands_width, bands_color, spindown_color, gap, gap_tol, gap_color, direct_gaps_only, custom_gaps)
    642 def _set_data(self, Erange, E0, bands_range, spin, spin_texture_colorscale, bands_width, bands_color, spindown_color,
    643     gap, gap_tol, gap_color, direct_gaps_only, custom_gaps):
    644     # Calculate all the gaps of this band structure
--> 645     self._calculate_gaps(E0)
    647     # Shift all the bands to the reference
    648     filtered_bands = self.bands - E0

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:972, in _populate_with_settings.<locals>.f_default_setting_args(self, *args, **kwargs)
    969         except KeyError:
    970             pass
--> 972 return f(self, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plots/bands.py:714, in BandsPlot._calculate_gaps(self, E0)
    708 """
    709 Calculates the gap (or gaps) assuming 0 is the fermi level.
    710
    711 It creates the attributes `gap` and `gap_info`
    712 """
    713 # Calculate the band gap to store it
--> 714 shifted_bands = self.bands - E0
    715 above_fermi = self.bands.where(shifted_bands > 0)
    716 below_fermi = self.bands.where(shifted_bands < 0)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:723, in Plot.__getattr__(self, key)
    720     except (KeyError, AttributeError):
    721         pass
--> 723 raise AttributeError(f"The attribute '{key}' was not found either in the plot, its backend, or in shared attributes.")

AttributeError: The attribute 'bands' was not found either in the plot, its backend, or in shared attributes.

This displays the minimum gaps. However there may be some issues with it: it will show all gaps with the minimum value. That is, if you have repeated points in the brillouin zone it will display multiple gaps that are equivalent.

What’s worse, if the region where your gap is is very flat, two consecutive points might have the same energy. Multiple gaps will be displayed one glued to another.

To help cope with this issues, you have the direct_gaps_only and gap_tol.

In this case, since we have no direct gaps, setting direct_gaps_only will hide them all:

[12]:
bands_plot.update_settings(direct_gaps_only=True)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [12], in <cell line: 1>()
----> 1 bands_plot.update_settings(direct_gaps_only=True)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:440, in ConfigurableMeta.__new__.<locals>.update_settings(self, *args, **kwargs)
    439 def update_settings(self, *args, **kwargs):
--> 440     return self._update_settings(*args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:561, in Configurable._update_settings(self, run_updates, **kwargs)
    559     #Do things after updating the settings
    560     if len(self.settings_history.last_updated) > 0 and run_updates:
--> 561         self._run_updates(self.settings_history.last_updated)
    563 return self

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:589, in Configurable._run_updates(self, for_keys)
    587 # Execute the functions that we need to execute.
    588 for f_name in func_names:
--> 589     getattr(self, f_name)()
    591 return self

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plotutils.py:829, in repeat_if_children.<locals>.apply_to_all_plots(obj, children_sel, *args, **kwargs)
    825     obj.get_figure()
    827 else:
--> 829     return method(obj, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:891, in vizplotly_settings.<locals>.decorator.<locals>.func(obj, *args, **kwargs)
    888 @wraps(method)
    889 def func(obj, *args, **kwargs):
    890     getattr(obj, method_name)(**kwargs, **extra_kwargs)
--> 891     return method(obj, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:1108, in Plot.set_data(self, update_fig, **kwargs)
   1100 @repeat_if_children
   1101 @vizplotly_settings('before')
   1102 def set_data(self, update_fig = True, **kwargs):
   1103     """ Method to process the data that has been read beforehand by read_data() and prepare the figure
   1104
   1105     If everything is succesful, it calls the next step in plotting (`get_figure`)
   1106     """
-> 1108     self._for_backend = self._set_data()
   1110     if update_fig:
   1111         self.get_figure()

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:972, in _populate_with_settings.<locals>.f_default_setting_args(self, *args, **kwargs)
    969         except KeyError:
    970             pass
--> 972 return f(self, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plots/bands.py:645, in BandsPlot._set_data(self, Erange, E0, bands_range, spin, spin_texture_colorscale, bands_width, bands_color, spindown_color, gap, gap_tol, gap_color, direct_gaps_only, custom_gaps)
    642 def _set_data(self, Erange, E0, bands_range, spin, spin_texture_colorscale, bands_width, bands_color, spindown_color,
    643     gap, gap_tol, gap_color, direct_gaps_only, custom_gaps):
    644     # Calculate all the gaps of this band structure
--> 645     self._calculate_gaps(E0)
    647     # Shift all the bands to the reference
    648     filtered_bands = self.bands - E0

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:972, in _populate_with_settings.<locals>.f_default_setting_args(self, *args, **kwargs)
    969         except KeyError:
    970             pass
--> 972 return f(self, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plots/bands.py:714, in BandsPlot._calculate_gaps(self, E0)
    708 """
    709 Calculates the gap (or gaps) assuming 0 is the fermi level.
    710
    711 It creates the attributes `gap` and `gap_info`
    712 """
    713 # Calculate the band gap to store it
--> 714 shifted_bands = self.bands - E0
    715 above_fermi = self.bands.where(shifted_bands > 0)
    716 below_fermi = self.bands.where(shifted_bands < 0)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:723, in Plot.__getattr__(self, key)
    720     except (KeyError, AttributeError):
    721         pass
--> 723 raise AttributeError(f"The attribute '{key}' was not found either in the plot, its backend, or in shared attributes.")

AttributeError: The attribute 'bands' was not found either in the plot, its backend, or in shared attributes.

This example is not meaningful for gap_tol, but it is illustrative of what gap_tol does. It is the minimum k-distance between two points to consider them “the same point” in the sense that only one of them will be used to show the gap. In this case, if we set gap_tol all the way up to 3, the plot will consider the two gamma points to be part of the same “point” and therefore it will only show the gap once.

[13]:
bands_plot.update_settings(direct_gaps_only=False, gap_tol=3)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [13], in <cell line: 1>()
----> 1 bands_plot.update_settings(direct_gaps_only=False, gap_tol=3)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:440, in ConfigurableMeta.__new__.<locals>.update_settings(self, *args, **kwargs)
    439 def update_settings(self, *args, **kwargs):
--> 440     return self._update_settings(*args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:561, in Configurable._update_settings(self, run_updates, **kwargs)
    559     #Do things after updating the settings
    560     if len(self.settings_history.last_updated) > 0 and run_updates:
--> 561         self._run_updates(self.settings_history.last_updated)
    563 return self

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:589, in Configurable._run_updates(self, for_keys)
    587 # Execute the functions that we need to execute.
    588 for f_name in func_names:
--> 589     getattr(self, f_name)()
    591 return self

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plotutils.py:829, in repeat_if_children.<locals>.apply_to_all_plots(obj, children_sel, *args, **kwargs)
    825     obj.get_figure()
    827 else:
--> 829     return method(obj, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:891, in vizplotly_settings.<locals>.decorator.<locals>.func(obj, *args, **kwargs)
    888 @wraps(method)
    889 def func(obj, *args, **kwargs):
    890     getattr(obj, method_name)(**kwargs, **extra_kwargs)
--> 891     return method(obj, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:1108, in Plot.set_data(self, update_fig, **kwargs)
   1100 @repeat_if_children
   1101 @vizplotly_settings('before')
   1102 def set_data(self, update_fig = True, **kwargs):
   1103     """ Method to process the data that has been read beforehand by read_data() and prepare the figure
   1104
   1105     If everything is succesful, it calls the next step in plotting (`get_figure`)
   1106     """
-> 1108     self._for_backend = self._set_data()
   1110     if update_fig:
   1111         self.get_figure()

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:972, in _populate_with_settings.<locals>.f_default_setting_args(self, *args, **kwargs)
    969         except KeyError:
    970             pass
--> 972 return f(self, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plots/bands.py:645, in BandsPlot._set_data(self, Erange, E0, bands_range, spin, spin_texture_colorscale, bands_width, bands_color, spindown_color, gap, gap_tol, gap_color, direct_gaps_only, custom_gaps)
    642 def _set_data(self, Erange, E0, bands_range, spin, spin_texture_colorscale, bands_width, bands_color, spindown_color,
    643     gap, gap_tol, gap_color, direct_gaps_only, custom_gaps):
    644     # Calculate all the gaps of this band structure
--> 645     self._calculate_gaps(E0)
    647     # Shift all the bands to the reference
    648     filtered_bands = self.bands - E0

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:972, in _populate_with_settings.<locals>.f_default_setting_args(self, *args, **kwargs)
    969         except KeyError:
    970             pass
--> 972 return f(self, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plots/bands.py:714, in BandsPlot._calculate_gaps(self, E0)
    708 """
    709 Calculates the gap (or gaps) assuming 0 is the fermi level.
    710
    711 It creates the attributes `gap` and `gap_info`
    712 """
    713 # Calculate the band gap to store it
--> 714 shifted_bands = self.bands - E0
    715 above_fermi = self.bands.where(shifted_bands > 0)
    716 below_fermi = self.bands.where(shifted_bands < 0)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:723, in Plot.__getattr__(self, key)
    720     except (KeyError, AttributeError):
    721         pass
--> 723 raise AttributeError(f"The attribute '{key}' was not found either in the plot, its backend, or in shared attributes.")

AttributeError: The attribute 'bands' was not found either in the plot, its backend, or in shared attributes.

This is not what gap_tol is meant for, since it is thought to remediate the effect of locally flat bands, but still you can get the idea of what it does.

[14]:
bands_plot = bands_plot.update_settings(gap=False, gap_tol=0.01)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [14], in <cell line: 1>()
----> 1 bands_plot = bands_plot.update_settings(gap=False, gap_tol=0.01)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:440, in ConfigurableMeta.__new__.<locals>.update_settings(self, *args, **kwargs)
    439 def update_settings(self, *args, **kwargs):
--> 440     return self._update_settings(*args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:561, in Configurable._update_settings(self, run_updates, **kwargs)
    559     #Do things after updating the settings
    560     if len(self.settings_history.last_updated) > 0 and run_updates:
--> 561         self._run_updates(self.settings_history.last_updated)
    563 return self

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:589, in Configurable._run_updates(self, for_keys)
    587 # Execute the functions that we need to execute.
    588 for f_name in func_names:
--> 589     getattr(self, f_name)()
    591 return self

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plotutils.py:829, in repeat_if_children.<locals>.apply_to_all_plots(obj, children_sel, *args, **kwargs)
    825     obj.get_figure()
    827 else:
--> 829     return method(obj, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:891, in vizplotly_settings.<locals>.decorator.<locals>.func(obj, *args, **kwargs)
    888 @wraps(method)
    889 def func(obj, *args, **kwargs):
    890     getattr(obj, method_name)(**kwargs, **extra_kwargs)
--> 891     return method(obj, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:1108, in Plot.set_data(self, update_fig, **kwargs)
   1100 @repeat_if_children
   1101 @vizplotly_settings('before')
   1102 def set_data(self, update_fig = True, **kwargs):
   1103     """ Method to process the data that has been read beforehand by read_data() and prepare the figure
   1104
   1105     If everything is succesful, it calls the next step in plotting (`get_figure`)
   1106     """
-> 1108     self._for_backend = self._set_data()
   1110     if update_fig:
   1111         self.get_figure()

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:972, in _populate_with_settings.<locals>.f_default_setting_args(self, *args, **kwargs)
    969         except KeyError:
    970             pass
--> 972 return f(self, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plots/bands.py:645, in BandsPlot._set_data(self, Erange, E0, bands_range, spin, spin_texture_colorscale, bands_width, bands_color, spindown_color, gap, gap_tol, gap_color, direct_gaps_only, custom_gaps)
    642 def _set_data(self, Erange, E0, bands_range, spin, spin_texture_colorscale, bands_width, bands_color, spindown_color,
    643     gap, gap_tol, gap_color, direct_gaps_only, custom_gaps):
    644     # Calculate all the gaps of this band structure
--> 645     self._calculate_gaps(E0)
    647     # Shift all the bands to the reference
    648     filtered_bands = self.bands - E0

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:972, in _populate_with_settings.<locals>.f_default_setting_args(self, *args, **kwargs)
    969         except KeyError:
    970             pass
--> 972 return f(self, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plots/bands.py:714, in BandsPlot._calculate_gaps(self, E0)
    708 """
    709 Calculates the gap (or gaps) assuming 0 is the fermi level.
    710
    711 It creates the attributes `gap` and `gap_info`
    712 """
    713 # Calculate the band gap to store it
--> 714 shifted_bands = self.bands - E0
    715 above_fermi = self.bands.where(shifted_bands > 0)
    716 below_fermi = self.bands.where(shifted_bands < 0)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:723, in Plot.__getattr__(self, key)
    720     except (KeyError, AttributeError):
    721         pass
--> 723 raise AttributeError(f"The attribute '{key}' was not found either in the plot, its backend, or in shared attributes.")

AttributeError: The attribute 'bands' was not found either in the plot, its backend, or in shared attributes.

Displaying custom gaps

If you are not happy with the gaps that the plot is displaying for you or you simply want gaps that are not the smallest ones, you can always use custom_gaps.

Custom gaps should be a list where each item specifies how to draw that given gap. See the setting’s help message:

[15]:
print(bands_plot.get_param("custom_gaps").help)
List of all the gaps that you want to display.

 Each item is a dict.

 Structure of the dict: {
        'from': K value where to start measuring the gap.
                    It can be either the label of the k-point or the numeric value in the plot.
        'to': K value where to end measuring the gap.
                    It can be either the label of the k-point or the numeric value in the plot.
        'color': The color with which the gap should be displayed
        'spin': The spin components where the gap should be calculated.
}

So, for example, if we want to plot the gamma-gamma gap:

[16]:
bands_plot.update_settings(custom_gaps=[{"from": "Gamma", "to": "Gamma", "color": "red"}])
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [16], in <cell line: 1>()
----> 1 bands_plot.update_settings(custom_gaps=[{"from": "Gamma", "to": "Gamma", "color": "red"}])

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:440, in ConfigurableMeta.__new__.<locals>.update_settings(self, *args, **kwargs)
    439 def update_settings(self, *args, **kwargs):
--> 440     return self._update_settings(*args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:561, in Configurable._update_settings(self, run_updates, **kwargs)
    559     #Do things after updating the settings
    560     if len(self.settings_history.last_updated) > 0 and run_updates:
--> 561         self._run_updates(self.settings_history.last_updated)
    563 return self

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:589, in Configurable._run_updates(self, for_keys)
    587 # Execute the functions that we need to execute.
    588 for f_name in func_names:
--> 589     getattr(self, f_name)()
    591 return self

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plotutils.py:829, in repeat_if_children.<locals>.apply_to_all_plots(obj, children_sel, *args, **kwargs)
    825     obj.get_figure()
    827 else:
--> 829     return method(obj, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:891, in vizplotly_settings.<locals>.decorator.<locals>.func(obj, *args, **kwargs)
    888 @wraps(method)
    889 def func(obj, *args, **kwargs):
    890     getattr(obj, method_name)(**kwargs, **extra_kwargs)
--> 891     return method(obj, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:1108, in Plot.set_data(self, update_fig, **kwargs)
   1100 @repeat_if_children
   1101 @vizplotly_settings('before')
   1102 def set_data(self, update_fig = True, **kwargs):
   1103     """ Method to process the data that has been read beforehand by read_data() and prepare the figure
   1104
   1105     If everything is succesful, it calls the next step in plotting (`get_figure`)
   1106     """
-> 1108     self._for_backend = self._set_data()
   1110     if update_fig:
   1111         self.get_figure()

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:972, in _populate_with_settings.<locals>.f_default_setting_args(self, *args, **kwargs)
    969         except KeyError:
    970             pass
--> 972 return f(self, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plots/bands.py:645, in BandsPlot._set_data(self, Erange, E0, bands_range, spin, spin_texture_colorscale, bands_width, bands_color, spindown_color, gap, gap_tol, gap_color, direct_gaps_only, custom_gaps)
    642 def _set_data(self, Erange, E0, bands_range, spin, spin_texture_colorscale, bands_width, bands_color, spindown_color,
    643     gap, gap_tol, gap_color, direct_gaps_only, custom_gaps):
    644     # Calculate all the gaps of this band structure
--> 645     self._calculate_gaps(E0)
    647     # Shift all the bands to the reference
    648     filtered_bands = self.bands - E0

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:972, in _populate_with_settings.<locals>.f_default_setting_args(self, *args, **kwargs)
    969         except KeyError:
    970             pass
--> 972 return f(self, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plots/bands.py:714, in BandsPlot._calculate_gaps(self, E0)
    708 """
    709 Calculates the gap (or gaps) assuming 0 is the fermi level.
    710
    711 It creates the attributes `gap` and `gap_info`
    712 """
    713 # Calculate the band gap to store it
--> 714 shifted_bands = self.bands - E0
    715 above_fermi = self.bands.where(shifted_bands > 0)
    716 below_fermi = self.bands.where(shifted_bands < 0)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:723, in Plot.__getattr__(self, key)
    720     except (KeyError, AttributeError):
    721         pass
--> 723 raise AttributeError(f"The attribute '{key}' was not found either in the plot, its backend, or in shared attributes.")

AttributeError: The attribute 'bands' was not found either in the plot, its backend, or in shared attributes.

Notice how we got the gap probably not where we wanted, since it would be better to have it in the middle Gamma point, which is more visible. As the help message of custom_gaps states, you can also pass the K value instead of a label.

Now, you’ll be happy to know that you can easily access the k values of all labels, as they are stored as attributes in the bands dataarray, which you can find in bands_plot.bands:

[17]:
bands_plot.bands.attrs
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [17], in <cell line: 1>()
----> 1 bands_plot.bands.attrs

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:723, in Plot.__getattr__(self, key)
    720     except (KeyError, AttributeError):
    721         pass
--> 723 raise AttributeError(f"The attribute '{key}' was not found either in the plot, its backend, or in shared attributes.")

AttributeError: The attribute 'bands' was not found either in the plot, its backend, or in shared attributes.

Now all we need to do is to grab the value for the second gamma point:

[18]:
gap_k = None
for val, label in zip(bands_plot.bands.attrs["ticks"], bands_plot.bands.attrs["ticklabels"]):
    if label == "Gamma":
        gap_k = val
gap_k
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [18], in <cell line: 2>()
      1 gap_k = None
----> 2 for val, label in zip(bands_plot.bands.attrs["ticks"], bands_plot.bands.attrs["ticklabels"]):
      3     if label == "Gamma":
      4         gap_k = val

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:723, in Plot.__getattr__(self, key)
    720     except (KeyError, AttributeError):
    721         pass
--> 723 raise AttributeError(f"The attribute '{key}' was not found either in the plot, its backend, or in shared attributes.")

AttributeError: The attribute 'bands' was not found either in the plot, its backend, or in shared attributes.

And use it to build a custom gap:

[19]:
bands_plot.update_settings(custom_gaps=[{"from": gap_k, "to": gap_k, "color": "orange"}])
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [19], in <cell line: 1>()
----> 1 bands_plot.update_settings(custom_gaps=[{"from": gap_k, "to": gap_k, "color": "orange"}])

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:440, in ConfigurableMeta.__new__.<locals>.update_settings(self, *args, **kwargs)
    439 def update_settings(self, *args, **kwargs):
--> 440     return self._update_settings(*args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:561, in Configurable._update_settings(self, run_updates, **kwargs)
    559     #Do things after updating the settings
    560     if len(self.settings_history.last_updated) > 0 and run_updates:
--> 561         self._run_updates(self.settings_history.last_updated)
    563 return self

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:589, in Configurable._run_updates(self, for_keys)
    587 # Execute the functions that we need to execute.
    588 for f_name in func_names:
--> 589     getattr(self, f_name)()
    591 return self

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plotutils.py:829, in repeat_if_children.<locals>.apply_to_all_plots(obj, children_sel, *args, **kwargs)
    825     obj.get_figure()
    827 else:
--> 829     return method(obj, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:891, in vizplotly_settings.<locals>.decorator.<locals>.func(obj, *args, **kwargs)
    888 @wraps(method)
    889 def func(obj, *args, **kwargs):
    890     getattr(obj, method_name)(**kwargs, **extra_kwargs)
--> 891     return method(obj, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:1108, in Plot.set_data(self, update_fig, **kwargs)
   1100 @repeat_if_children
   1101 @vizplotly_settings('before')
   1102 def set_data(self, update_fig = True, **kwargs):
   1103     """ Method to process the data that has been read beforehand by read_data() and prepare the figure
   1104
   1105     If everything is succesful, it calls the next step in plotting (`get_figure`)
   1106     """
-> 1108     self._for_backend = self._set_data()
   1110     if update_fig:
   1111         self.get_figure()

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:972, in _populate_with_settings.<locals>.f_default_setting_args(self, *args, **kwargs)
    969         except KeyError:
    970             pass
--> 972 return f(self, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plots/bands.py:645, in BandsPlot._set_data(self, Erange, E0, bands_range, spin, spin_texture_colorscale, bands_width, bands_color, spindown_color, gap, gap_tol, gap_color, direct_gaps_only, custom_gaps)
    642 def _set_data(self, Erange, E0, bands_range, spin, spin_texture_colorscale, bands_width, bands_color, spindown_color,
    643     gap, gap_tol, gap_color, direct_gaps_only, custom_gaps):
    644     # Calculate all the gaps of this band structure
--> 645     self._calculate_gaps(E0)
    647     # Shift all the bands to the reference
    648     filtered_bands = self.bands - E0

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:972, in _populate_with_settings.<locals>.f_default_setting_args(self, *args, **kwargs)
    969         except KeyError:
    970             pass
--> 972 return f(self, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plots/bands.py:714, in BandsPlot._calculate_gaps(self, E0)
    708 """
    709 Calculates the gap (or gaps) assuming 0 is the fermi level.
    710
    711 It creates the attributes `gap` and `gap_info`
    712 """
    713 # Calculate the band gap to store it
--> 714 shifted_bands = self.bands - E0
    715 above_fermi = self.bands.where(shifted_bands > 0)
    716 below_fermi = self.bands.where(shifted_bands < 0)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:723, in Plot.__getattr__(self, key)
    720     except (KeyError, AttributeError):
    721         pass
--> 723 raise AttributeError(f"The attribute '{key}' was not found either in the plot, its backend, or in shared attributes.")

AttributeError: The attribute 'bands' was not found either in the plot, its backend, or in shared attributes.

Individual band styling

The bands_color and bands_width should be enough for most uses. However, you may want to style each band differently. Since we can not support every possible case, you can pass a function to the add_band_data. Here’s the help message:

[20]:
print(bands_plot.get_param("add_band_data").help)
This function receives each band and should return a dictionary with additional arguments
            that are passed to the band drawing routine. It also receives the plot as the second argument.
            See the docs of `sisl.viz.backends.templates.Backend.draw_line` to understand what are the supported arguments
            to be returned. Notice that the arguments that the backend is able to process can be very framework dependant.

You can build a dummy function to print the band and see how it looks like. Notice that you only get those bands that are inside the range specified for the plot, therefore the first band here is band 11!

[21]:
def add_band_data(band, self):
    """Dummy function to see the band DataArray"""
    if band.band == 11:
        print(band)

    return {}

bands_plot.update_settings(add_band_data=add_band_data)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [21], in <cell line: 8>()
      4         print(band)
      6     return {}
----> 8 bands_plot.update_settings(add_band_data=add_band_data)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:440, in ConfigurableMeta.__new__.<locals>.update_settings(self, *args, **kwargs)
    439 def update_settings(self, *args, **kwargs):
--> 440     return self._update_settings(*args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:561, in Configurable._update_settings(self, run_updates, **kwargs)
    559     #Do things after updating the settings
    560     if len(self.settings_history.last_updated) > 0 and run_updates:
--> 561         self._run_updates(self.settings_history.last_updated)
    563 return self

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:589, in Configurable._run_updates(self, for_keys)
    587 # Execute the functions that we need to execute.
    588 for f_name in func_names:
--> 589     getattr(self, f_name)()
    591 return self

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:972, in _populate_with_settings.<locals>.f_default_setting_args(self, *args, **kwargs)
    969         except KeyError:
    970             pass
--> 972 return f(self, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plots/bands.py:704, in BandsPlot.get_figure(self, backend, add_band_data, **kwargs)
    703 def get_figure(self, backend, add_band_data, **kwargs):
--> 704     self._for_backend["draw_bands"]["add_band_data"] = add_band_data
    705     return super().get_figure(backend, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:723, in Plot.__getattr__(self, key)
    720     except (KeyError, AttributeError):
    721         pass
--> 723 raise AttributeError(f"The attribute '{key}' was not found either in the plot, its backend, or in shared attributes.")

AttributeError: The attribute '_for_backend' was not found either in the plot, its backend, or in shared attributes.

Just as an educational example, we are going to style the bands according to this conditions: - If the band is +- 5 eV within the fermi level, we are going to draw markers whose size is proportional to the gradient of the band at each point. - Otherwise, we will just display the bands as purple dotted lines that fade as we get far from the fermi level (just because we can!)

Note: Of course, to modify traces, one must have some notion of how plotly traces work. Just hit plotly’s visual reference page https://plotly.com/python/ for inspiration.

[22]:
import numpy as np
def draw_gradient(band, self):
    """
    Takes a band and styles it according to its energy dispersion.

    NOTE: If it's to far from the fermi level, it fades it in purple for additional coolness.
    """
    dist_from_Ef = np.max(abs(band))
    if dist_from_Ef < 5:
        return {
            "mode": "lines+markers",
            "marker_size": np.abs(np.gradient(band))*40,
        }
    else:
        return {
            "line_color": "purple",
            "line_dash": "dot",
            "opacity": 1-float(dist_from_Ef/10)
        }

bands_plot.update_settings(add_band_data=draw_gradient)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [22], in <cell line: 21>()
     14     else:
     15         return {
     16             "line_color": "purple",
     17             "line_dash": "dot",
     18             "opacity": 1-float(dist_from_Ef/10)
     19         }
---> 21 bands_plot.update_settings(add_band_data=draw_gradient)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:440, in ConfigurableMeta.__new__.<locals>.update_settings(self, *args, **kwargs)
    439 def update_settings(self, *args, **kwargs):
--> 440     return self._update_settings(*args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:561, in Configurable._update_settings(self, run_updates, **kwargs)
    559     #Do things after updating the settings
    560     if len(self.settings_history.last_updated) > 0 and run_updates:
--> 561         self._run_updates(self.settings_history.last_updated)
    563 return self

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:589, in Configurable._run_updates(self, for_keys)
    587 # Execute the functions that we need to execute.
    588 for f_name in func_names:
--> 589     getattr(self, f_name)()
    591 return self

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/configurable.py:972, in _populate_with_settings.<locals>.f_default_setting_args(self, *args, **kwargs)
    969         except KeyError:
    970             pass
--> 972 return f(self, *args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plots/bands.py:704, in BandsPlot.get_figure(self, backend, add_band_data, **kwargs)
    703 def get_figure(self, backend, add_band_data, **kwargs):
--> 704     self._for_backend["draw_bands"]["add_band_data"] = add_band_data
    705     return super().get_figure(backend, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/viz/plot.py:723, in Plot.__getattr__(self, key)
    720     except (KeyError, AttributeError):
    721         pass
--> 723 raise AttributeError(f"The attribute '{key}' was not found either in the plot, its backend, or in shared attributes.")

AttributeError: The attribute '_for_backend' was not found either in the plot, its backend, or in shared attributes.

Displaying spin texture

If your bands plot comes from a non-colinear spin calculation (or is using a Hamiltonian with non-colinear spin), you can pass "x", "y" or "z" to the spin setting in order to get a display of the spin texture.

Let’s read in a hamiltonian coming from a spin orbit SIESTA calculation, which is obtained from this fantastic spin texture tutorial:

[23]:
H = sisl.get_sile(siesta_files / "Bi2D_BHex.TSHS").read_hamiltonian()
H.spin.is_spinorbit
---------------------------------------------------------------------------
SileError                                 Traceback (most recent call last)
Input In [23], in <cell line: 1>()
----> 1 H = sisl.get_sile(siesta_files / "Bi2D_BHex.TSHS").read_hamiltonian()
      2 H.spin.is_spinorbit

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/io/siesta/binaries.py:315, in tshsSileSiesta.read_hamiltonian(self, geometry, **kwargs)
    284 def read_hamiltonian(self, geometry=None, **kwargs):
    285     """ Electronic structure from the siesta.TSHS file
    286
    287     The TSHS file format does *not* contain exact orbital information.
   (...)
    313     >>> H = sisl.get_sile("siesta.TSHS").read_hamiltonian(geometry=geom)
    314     """
--> 315     tshs_g = self.read_geometry()
    316     if geometry is None:
    317         geometry = tshs_g

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/io/siesta/binaries.py:180, in onlysSileSiesta.read_geometry(self, geometry)
    177 """ Returns Geometry object from a TranSiesta file """
    179 # Read supercell
--> 180 sc = self.read_supercell()
    182 na = _siesta.read_tshs_sizes(self.file)[1]
    183 self._fortran_check("read_geometry", "could not read sizes.")

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/io/siesta/binaries.py:163, in onlysSileSiesta.read_supercell(self)
    161 """ Returns a SuperCell object from a TranSiesta file """
    162 n_s = _siesta.read_tshs_sizes(self.file)[3]
--> 163 self._fortran_check("read_supercell", "could not read sizes.")
    164 arr = _siesta.read_tshs_cell(self.file, n_s)
    165 self._fortran_check("read_supercell", "could not read cell.")

File ~/checkouts/readthedocs.org/user_builds/sisl/checkouts/v0.12.2/sisl/io/siesta/sile.py:50, in SileBinSiesta._fortran_check(self, method, message, ret_msg)
     48     msg = f'{self!s}.{method} {message} (ierr={ierr})'
     49     if not ret_msg:
---> 50         raise SileError(msg)
     51 if ret_msg:
     52     return msg

SileError: tshsSileSiesta(Bi2D_BHex.TSHS, base=_THIS_DIRECTORY_DOES_NOT_EXIST_/sisl/io/siesta).read_supercell could not read sizes. (ierr=2)

Generate the path for our band structure:

[24]:
band_struct = sisl.BandStructure(H, point=[[1./2, 0., 0.], [0., 0., 0.],
                                     [1./3, 1./3, 0.], [1./2, 0., 0.]],
                           division=301,
                           name=['M', r'$\Gamma$', 'K', 'M'])
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [24], in <cell line: 1>()
----> 1 band_struct = sisl.BandStructure(H, point=[[1./2, 0., 0.], [0., 0., 0.],
      2                                      [1./3, 1./3, 0.], [1./2, 0., 0.]],
      3                            division=301,
      4                            name=['M', r'$\Gamma$', 'K', 'M'])

NameError: name 'H' is not defined

And finally generate the plot:

[25]:
spin_texture_plot = band_struct.plot(Erange=[-2,2])
spin_texture_plot
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [25], in <cell line: 1>()
----> 1 spin_texture_plot = band_struct.plot(Erange=[-2,2])
      2 spin_texture_plot

NameError: name 'band_struct' is not defined

These are the bands, now let’s ask for a particular spin texture:

[26]:
spin_texture_plot.update_settings(spin="x", bands_width=3)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [26], in <cell line: 1>()
----> 1 spin_texture_plot.update_settings(spin="x", bands_width=3)

NameError: name 'spin_texture_plot' is not defined

And let’s change the colorscale for the spin texture:

[27]:
spin_texture_plot.update_settings(backend="plotly", spin_texture_colorscale="temps")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [27], in <cell line: 1>()
----> 1 spin_texture_plot.update_settings(backend="plotly", spin_texture_colorscale="temps")

NameError: name 'spin_texture_plot' is not defined

We hope you enjoyed what you learned!


This next cell is just to create the thumbnail for the notebook in the docs

[28]:
thumbnail_plot = spin_texture_plot

if thumbnail_plot:
    thumbnail_plot.show("png")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [28], in <cell line: 1>()
----> 1 thumbnail_plot = spin_texture_plot
      3 if thumbnail_plot:
      4     thumbnail_plot.show("png")

NameError: name 'spin_texture_plot' is not defined