def plot_deltas(self, ax: 'matplotlib.axes.Axes' = None, show: bool = False, filename: str = None, mplargs: dict = None) -> 'matplotlib.axes.Axes': """Simple plot of sparse DOS data as a set of delta functions Items at the same x-value can overlap and will not be summed together Args: ax: existing Matplotlib axes object. If not provided, a new figure with one set of axes will be created using Pyplot show: show the figure on-screen filename: if a path is given, save the figure to this file mplargs: additional arguments to pass to matplotlib Axes.vlines command (e.g. {'linewidth': 2} for a thicker line). Returns: Plotting axes. If "ax" was set, this is the same object. """ with SimplePlottingAxes(ax=ax, show=show, filename=filename) as ax: if mplargs is None: mplargs = {} ax.vlines(self.get_energies(), 0, self.get_weights(), **mplargs) return ax
def test_plot_manager_no_file(self, xy_data, figure): x, y = xy_data with SimplePlottingAxes(ax=None, show=False, filename=None) as ax: ax.plot(x, y) assert np.allclose(ax.lines[0].get_xydata().transpose(), xy_data)
def plot(self, npts: int = 0, xmin: float = None, xmax: float = None, width: float = None, smearing: str = 'Gauss', ax: 'matplotlib.axes.Axes' = None, show: bool = False, filename: str = None, mplargs: dict = None) -> 'matplotlib.axes.Axes': """Simple plot of collected DOS data, resampled onto a grid If the special key 'label' is present in self.info, this will be set as the label for the plotted line (unless overruled in mplargs). The label is only seen if a legend is added to the plot (i.e. by calling `ax.legend()`). Args: npts: Number of points in resampled x-axis. If set to zero (default), no resampling is performed and the stored data is plotted directly. xmin, xmax: output data range; this limits the resampling range as well as the plotting output width: Width of broadening kernel, passed to self.sample() smearing: selection of broadening kernel, passed to self.sample() ax: existing Matplotlib axes object. If not provided, a new figure with one set of axes will be created using Pyplot show: show the figure on-screen filename: if a path is given, save the figure to this file mplargs: additional arguments to pass to matplotlib plot command (e.g. {'linewidth': 2} for a thicker line). Returns: Plotting axes. If "ax" was set, this is the same object. """ # Apply defaults if necessary npts, width = GridDOSData._interpret_smearing_args(npts, width) if npts: assert isinstance(width, float) dos = self.sample_grid(npts, xmin=xmin, xmax=xmax, width=width, smearing=smearing) else: dos = self energies, all_y = dos._energies, dos._weights all_labels = [DOSData.label_from_info(data.info) for data in self] with SimplePlottingAxes(ax=ax, show=show, filename=filename) as ax: self._plot_broadened(ax, energies, all_y, all_labels, mplargs) return ax
def plot_dos(self, npts: int = 0, xmin: float = None, xmax: float = None, width: float = 0.1, smearing: str = 'Gauss', ax: 'matplotlib.axes.Axes' = None, show: bool = False, filename: str = None, mplargs: dict = None) -> 'matplotlib.axes.Axes': """Simple 1-D plot of DOS data Data will be resampled onto a grid with `npts` points unless `npts` is set to zero, in which case: - no resampling takes place - `width` and `smearing` are ignored - `xmin` and `xmax` affect the axis limits of the plot, not the underlying data. If the special key 'label' is present in self.info, this will be set as the label for the plotted line (unless overruled in mplargs). The label is only seen if a legend is added to the plot (i.e. by calling ``ax.legend()``). Args: npts, xmin, xmax: output data range, as passed to self.sample_grid width: Width of broadening kernel, passed to self.sample() smearing: selection of broadening kernel, passed to self.sample() ax: existing Matplotlib axes object. If not provided, a new figure with one set of axes will be created using Pyplot show: show the figure on-screen filename: if a path is given, save the figure to this file mplargs: additional arguments to pass to matplotlib plot command (e.g. {'linewidth': 2} for a thicker line). Returns: Plotting axes. If "ax" was set, this is the same object. """ with SimplePlottingAxes(ax=ax, show=show, filename=filename) as ax: if mplargs is None: mplargs = {} if 'label' not in mplargs: mplargs.update({'label': self.label_from_info(self.info)}) if npts: energies, intensity = self.sample_grid(npts, xmin=xmin, xmax=xmax, width=width, smearing=smearing) else: energies, intensity = self.get_energies(), self.get_weights() ax.plot(energies, intensity, **mplargs) ax.set_xlim(left=xmin, right=xmax) return ax
def plot(self, npts: int = 1000, xmin: float = None, xmax: float = None, width: float = 0.1, smearing: str = 'Gauss', ax: 'matplotlib.axes.Axes' = None, show: bool = False, filename: str = None, orientation: str = 'horizontal', mplargs: dict = None) -> 'matplotlib.axes.Axes': """Simple plot of collected DOS data, resampled onto a grid If the special key 'label' is present in self.info, this will be set as the label for the plotted line (unless overruled in mplargs). The label is only seen if a legend is added to the plot (i.e. by calling `ax.legend()`). Args: npts, xmin, xmax: output data range, as passed to self.sample_grid width: Width of broadening kernel, passed to self.sample() smearing: selection of broadening kernel, passed to self.sample() ax: existing Matplotlib axes object. If not provided, a new figure with one set of axes will be created using Pyplot show: show the figure on-screen filename: if a path is given, save the figure to this file mplargs: additional arguments to pass to matplotlib plot command (e.g. {'linewidth': 2} for a thicker line). Returns: Plotting axes. If "ax" was set, this is the same object. """ with SimplePlottingAxes(ax=ax, show=show, filename=filename) as ax: if mplargs is None: mplargs = {} energies, all_y = self.sample_grid(npts, xmin=xmin, xmax=xmax, width=width, smearing=smearing) all_labels = [DOSData.label_from_info(data.info) for data in self] if orientation == 'horizontal': all_lines = ax.plot(energies, all_y.T, **mplargs) ax.set_xlim([min(energies), max(energies)]) else: all_lines = ax.plot(all_y.T, energies, **mplargs) ax.set_ylim([min(energies), max(energies)]) for line, label in zip(all_lines, all_labels): line.set_label(label) ax.legend() # ax.set_ylim(bottom=0) return ax
def test_plot_manager_axis_file(self, figure, xy_data): x, y = xy_data ax = figure.add_subplot() with SimplePlottingAxes(ax=ax, show=False, filename=self.filename) as return_ax: assert return_ax is ax ax.plot(x, y) assert os.path.isfile(self.filename)
def test_plot_manager_axis_file(self, testdir, xy_data, figure): filename = 'plot.png' x, y = xy_data ax = figure.add_subplot(111) with SimplePlottingAxes(ax=ax, show=False, filename=filename) as return_ax: assert return_ax is ax ax.plot(x, y) assert os.path.isfile(filename)
def test_plot_manager_error(self, plt): # Boot up a figure to help the oldlibs tests manage without graphics fig = plt.figure() try: with pytest.raises(AssertionError): with SimplePlottingAxes(ax=None, show=False, filename=None) as _: raise AssertionError() finally: plt.close(fig=fig)
def test_plot_manager_no_file(self, plt, xy_data): x, y = xy_data # Boot up a figure to help the oldlibs tests manage without graphics fig = plt.figure() try: with SimplePlottingAxes(ax=None, show=False, filename=None) as ax: ax.plot(x, y) assert np.allclose(ax.lines[0].get_xydata().transpose(), xy_data) assert not os.path.isfile(self.filename) finally: plt.close(fig=fig)
def test_plot_manager_error(self, figure): with pytest.raises(AssertionError): with SimplePlottingAxes(ax=None, show=False, filename=None): raise AssertionError()