General Plotting routines

The plotting of data is always a common task that needs to be performed. However, there is a lot of variation in how someone might want plots to look or be arranged. Some plots might also need to be interactive to be of a real use.

For these reasons the masci_tools library provides utility for general plotting and template functions for common plots made when working with DFT methods. There are two plotting backends available:

  • matplotlib: Mainly used for non-interactive plots

  • bokeh: Mainly used for interactive plots

Available Routines

For both of these there are a lot of plotting routines available (both general or specific to a problem). All of these routines will return the used Axes object in the case of matplotlib or the figure produced by bokeh for custom modifications.

common (Can be used for both backends):

  • scatter(): Make a scatterplot with varying size and color of the points for multiple sets of data

  • line(): Make a lineplot with multiple sets of data

  • dos(): Plot a general density of states (non-spinpolarized)

  • spinpol_dos(): Plot a general density of states (spinpolarized)

  • bands(): Plot a general bandstructure (non-spinpolarized)

  • spinpol_bands(): Plot a general bandstructure (spinpolarized)

matplotlib:

bokeh:

If you have ideas for new useful and beautiful plotting routines you are welcome to contribute. Refer to the sections Using the Plotter class and Using the PlotData class for a guide on how to get started.

Providing Data

Data can be provided to plotting functions in two main ways:

  1. The first arguments and data arguments are given the keys in a mapping, which should be used. The corresponding mapping is provided via the data keyword argument

  2. The first arguments and data arguments are given the data that should be plotted against each other.

The following two code blocks are equivalent in terms of the provided data.

from masci_tools.vis.plot_methods import multiple_scatterplots
import numpy as np

x = np.linspace(-10,10,100)
y1 = x**2
y2 = 20*np.sin(x)

#The data is split up according to fixed rules that the plot function defines.
#The default behaviour is that a list of lists is interpreted as multiple separate plots
ax = multiple_scatterplots(x, [y1, y2])
from masci_tools.vis.plot_methods import multiple_scatterplots
import numpy as np

x = np.linspace(-10,10,100)
y1 = x**2
y2 = 20*np.sin(x)
data = {'x': x, 'y1': y1, 'y2': y2}

ax = multiple_scatterplots('x', ['y1', 'y2'], data=data)

Customizing Plots

You might want to change the parameters of your plot. From changing the color, linestyle or shape of the markers there are a million options to tweak. These can be set by simply passing the keyword arguments with the desired parameters to the plotting function. The names of these parameters mostly correspond to the same names as in the plotting library that is used in the plotting function. However, there are some deviations and also some special keywords that you can use. We will go over the most important ones in this section accompanied with concrete code examples. For a reference of the defaults defined in the masci_tools library you can refer to matplotlib_plotter.MatplotlibPlotter and bokeh_plotter.BokehPlotter for a complete reference.

The most important special keywords are listed below. If there are deviating names for these in matplotlib and bokeh plotting functions both names are written in the order matplotlib or bokeh:

  • limits: This is used to set the bounds of the axis specifically. Provided in form of a dictionary. For example passing limits={'x': (-5,5)} will set the x-axis limits between -5 and 5 and limits={'x': (-5,5), 'y':(0,10)} will set the y-axis limits in addition

  • scale: Used to set the scaling of the axis in the plots. Also provided in form of a dictionary. For example passing scale={'x': 'log', 'y': 'log'} will set both axis to logarithmic scaling scale={'y': 'log'} will only to it for the y-axis

  • lines or straight_lines: Easy way to draw help lines into the plot. Provided in form of a dictionary. For example passing lines={'vertical': 0} will draw a vertical line at x=0 lines={'horizontal': [1,5,10]} will draw three horizontal lines at y=1, 5 or 10 respectively

  • plot_labels` or legend_labels`: Defines labels for the legend of a plot

  • labels for axis: Normally called xlabel or ylabel, but specialized plot routines might have different names

  • title: Title for the produced plot

  • saving options: show=True call the plotting library specific show routines (default). For matplotlib you can also specify saveas='filename' and save_plots=True to save the plot to file

In the following we will look at examples using the matplotlib plotting functions in plot_methods. The options are the same for the bokeh plotting routines in bokeh_plots.

Single plots

We start from the default result of calling the plot_methods.single_scatterplot() function with an exponential function. Afterwards we go through examples of modifying this call in one particular way. All of these can be combined to customize the plot to your desire

from masci_tools.vis.plot_methods import single_scatterplot
import numpy as np

x = np.linspace(-10, 10, 100)
y = np.exp(x)

ax = single_scatterplot(x,y)
../_images/941cd8287452679ae30846dd6c3b4418cec5975e4390cb652fd900863cd843df.png

Setting limits

from masci_tools.vis.plot_methods import single_scatterplot
import numpy as np

x = np.linspace(-10, 10, 100)
y = np.exp(x)

ax = single_scatterplot(x,y, limits={'x': (-1,1), 'y': (0,4)})
../_images/9407d8092f4eb19b2e07d8f17ed7e2b5cbe1f364ba703d587f9b3e5bace1db65.png

Modifying the scale of the axis

from masci_tools.vis.plot_methods import single_scatterplot
import numpy as np

x = np.linspace(-10, 10, 100)
y = np.exp(x)

ax = single_scatterplot(x,y, scale={'y': 'log'})
../_images/a13c408e5eff42a4e321f48567c6095bf3a9e58efc48769cb05f9f1148a8f571.png

Setting labels on the axis and a title

from masci_tools.vis.plot_methods import single_scatterplot
import numpy as np

x = np.linspace(-10, 10, 100)
y = np.exp(x)

ax = single_scatterplot(x,y, xlabel='X', ylabel='Y', title='Exponential Growth')
../_images/620e2fc639ee5a9f200bc8be48e4d2c6b4019ec98af19fe1509e77c0d72ac704.png

Modifying plot parameters

See the matplotlib documentation for complete references of possible options

from masci_tools.vis.plot_methods import single_scatterplot
import numpy as np

x = np.linspace(-10, 10, 100)
y = np.exp(x)

ax = single_scatterplot(x,y, color='darkblue', linestyle='--', marker=None)
../_images/e930967d26b20e19292457df6513751f6ece164928646af0a0bcd66303ba37e1.png

Setting user defaults

If you wish to change some parameters for all the plots you want to do, you can use the functions plot_methods.set_mpl_plot_defaults() or bokeh_plots.set_bokeh_plot_defaults() for the matplotlib and bokeh plotting library respectively. These functions accept the same keyword arguments as above and they will be applied to all the next plots that you do.

You can reset the changes to the defaults with plot_methods.reset_mpl_plot_defaults() or bokeh_plots.reset_bokeh_plot_defaults()

Note

You can still override these defaults by simply passing in another value for the parameter you wish to overwrite in the call to a plotting function

from masci_tools.vis.plot_methods import single_scatterplot, set_mpl_plot_defaults
import numpy as np

x = np.linspace(-10, 10, 100)
y = np.exp(x)

set_mpl_plot_defaults(color='darkblue', linestyle='--', marker=None)

ax = single_scatterplot(x,y, scale={'y': 'log'})
../_images/ed2f5e5eb0709e2beb5eef3ea459cd32411537e34215e85bdc0d00d95c14be56.png

Resetting defaults:

from masci_tools.vis.plot_methods import reset_mpl_plot_defaults
reset_mpl_plot_defaults()

Multiple plots

Many plotting routines accept multiple sets of data to plot. An example of this is the plot_methods.multiple_scatterplots() function. The usage of these is essentially the same. However, some parameters can be changed for each data set to plot. These include but are not limited to linestyle, linewidth, marker, markersize and color. These parameters can either be set to a single value applying it to all data sets, or can be specified for some/all data sets with unspecified values being replaced with the current defaults. This second way can be done in two ways (Both of the below examples have the same effect):

  1. List of values (None for unspecified values) Example: linestyle=['-', None, '--']

  2. Dictionary with integer indices Example: linestyle={0:'-', 2:'--'}

Warning

Specifying parameters for multiple data sets is only valid for the parameters passed into the function. Setting defaults with values for multiple data sets is not supported

Default plot

from masci_tools.vis.plot_methods import multiple_scatterplots
import numpy as np

x = np.linspace(-1,1,100)
y = np.exp(x)
y2 = x**2
y3 = np.sin(x)

ax = multiple_scatterplots([x, x, x], [y, y2, y3])
../_images/cd2732ddec000c580c4c2c105fa9ff297a13809261eaf20694abb7baae03e85d.png

Changing parameters on all plots

from masci_tools.vis.plot_methods import multiple_scatterplots
import numpy as np

x = np.linspace(-1,1,100)
y = np.exp(x)
y2 = x**2
y3 = np.sin(x)

ax = multiple_scatterplots([x, x, x], [y, y2, y3], linestyle='--', marker=None)
../_images/76938881a2fa8b3a0e58c8f353e1b30f40aa21433498a9c2a65983be8ef11803.png

Changing parameters on specific plots

from masci_tools.vis.plot_methods import multiple_scatterplots
import numpy as np

x = np.linspace(-1,1,100)
y = np.exp(x)
y2 = x**2
y3 = np.sin(x)

ax = multiple_scatterplots([x, x, x], [y, y2, y3],
                           linestyle='--',
                           marker=None,
                           color=['darkgreen', None, 'darkred'],
                           linewidth={0: 10})
../_images/33ae5f732ae3e30d1abfa7e09b2fd468e4668a00685a3904cbd465be8efe5d31.png