pandas, matplotlib and seaborn packages to compare material
properties using 2D, 3D, and 4D scatter plots.
This example uses data from the MaterialUniverse table in the MI Training database.
Get data from Granta MI
Import thegranta libraries and connect to Granta MI via the Service Layer using Windows authentication,
replacing my.server.name with the name of your Granta MI server. Specify a database and table.
search_criterion()).
Process the data using pandas
Build a DataFrame
Apandas DataFrame can be built from a List of Dicts, where each dictionary in the list corresponds to a
separate row in the DataFrame. Each key-value pair in the Dict corresponds to a column and the cell value in that
column, respectively.
First generate the list of dictionaries, and then use this list to create a DataFrame. Here, we are using the
Record.record_guid property as the DataFrame index, but any column with a unique value for each record can be used.
We also sort by the Base column to guarantee a repeatable row order.
.head() method to output the first few rows of the DataFrame and check the structure.
| Designation | Base | Transparency | Young’s modulus | Density | Tensile strength | Electrical resistivity | |
|---|---|---|---|---|---|---|---|
| guid | |||||||
| 00000e41-000e-4fff-8fff-dd92ffff0000 | 7075 | Al (Aluminum) | Opaque | {‘low’: 69000000000.0, ‘high’: 72538459777.83203} | {‘low’: 2781.691551208496, ‘high’: 2809.648036… | {‘low’: 386000000.0, ‘high’: 510000000.0} | {‘low’: 4.199999809265137e-08, ‘high’: 4.40000… |
| 00000e38-000e-4fff-8fff-dd92ffff0000 | 7075 | Al (Aluminum) | Opaque | {‘low’: 69000000000.0, ‘high’: 76000000000.0} | {‘low’: 2769.9999809265137, ‘high’: 2829.99992… | {‘low’: 434000000.0, ‘high’: 580000000.0} | {‘low’: 5.0999999046325684e-08, ‘high’: 5.3000… |
| 02401a77-c3e7-4b68-875c-7be3b735b703 | Tungsten Carbide | Carbide | Opaque | {‘low’: 599999999999.9996, ‘high’: 67000000000… | {‘low’: 15299.99999999999, ‘high’: 15900.00000… | {‘low’: 372999999.9999999, ‘high’: 529999999.9… | {‘low’: 6.309999847412099e-07, ‘high’: 1.00000… |
| 000009cc-000e-4fff-8fff-dd92ffff0000 | AISI 4130 | Fe (Iron) | Opaque | {‘low’: 200000000000.0, ‘high’: 210256408691.4… | {‘low’: 7790.849685668945, ‘high’: 7869.149684… | {‘low’: 862000000.0, ‘high’: 1241000000.0} | {‘low’: 2e-07, ‘high’: 2.5e-07} |
| 000009cb-000e-4fff-8fff-dd92ffff0000 | AISI 4130 | Fe (Iron) | Opaque | {‘low’: 200000000000.0, ‘high’: 210256408691.4… | {‘low’: 7794.244766235352, ‘high’: 7872.578620… | {‘low’: 621000000.0, ‘high’: 686368408.203125} | {‘low’: 2e-07, ‘high’: 2.5e-07} |
Process any range data
A DataFrame cell can contain any data type we choose to store in it. However, plotting packages such asmatplotlib
require numerical values to be in a standard format. Some MI Scripting Toolkit data types, such as
ranges represented as dictionaries, are not in that format and have to be processed before plotting.
Below, we define a granta_mean function which takes a single dict as input and calculates either the arithmetic or
geometric mean (depending on the signs of the input values).
granta_mean function to each cell in the Density, Young’s modulus, Tensile strength, and Electrical
resistivity columns. Store the results in a new DataFrame, and check the output to ensure the data processing worked
as expected.
| Density | Young’s modulus | Tensile strength | Electrical resistivity | |
|---|---|---|---|---|
| guid | ||||
| 0000132e-000e-4fff-8fff-dd92ffff0000 | 922.225549 | 2.236068e+08 | 3.162278e+07 | 1.000000e+01 |
| 0000157d-000e-4fff-8fff-dd92ffff0000 | 1049.571352 | 2.408319e+09 | 3.872983e+07 | 9.949874e+13 |
| 00001584-000e-4fff-8fff-dd92ffff0000 | 1079.953746 | 1.999375e+09 | 4.289522e+07 | 9.949874e+13 |
| 00001ca7-000e-4fff-8fff-dd92ffff0000 | 1095.415362 | 7.509658e+05 | 8.289964e+06 | 3.160000e+10 |
| 00001570-000e-4fff-8fff-dd92ffff0000 | 1134.988977 | 1.308128e+10 | 1.064425e+08 | 3.162278e-02 |
.describe() method to provide some initial insights into
the distribution of values in the table. (This also confirms that the granta_mean function worked as expected.)
| Density | Young’s modulus | Tensile strength | Electrical resistivity | |
|---|---|---|---|---|
| count | 29.000000 | 2.900000e+01 | 2.900000e+01 | 2.900000e+01 |
| mean | 3056.118133 | 7.441616e+10 | 3.107848e+08 | 2.184037e+15 |
| std | 3201.106148 | 1.256091e+11 | 4.631297e+08 | 4.056391e+15 |
| min | 922.225549 | 7.509658e+05 | 8.289964e+06 | 4.298837e-08 |
| 25% | 1184.905061 | 2.408319e+09 | 3.863832e+07 | 1.689970e-06 |
| 50% | 1369.963508 | 1.308128e+10 | 6.816744e+07 | 4.000000e+13 |
| 75% | 2799.839234 | 8.692341e+10 | 4.446234e+08 | 1.000000e+15 |
| max | 15597.115118 | 6.340347e+11 | 1.775671e+09 | 1.000000e+16 |
Process any discrete or short text data
The Designation column is based on a short-text attribute, which is represented as a string and can be used as is. The columns Transparency and Base are based on discrete attributes. Since they are single-valued, they contain strings and so can also be used as is.| Density | Young’s modulus | Tensile strength | Electrical resistivity | Transparency | Base | Designation | |
|---|---|---|---|---|---|---|---|
| guid | |||||||
| 0000132e-000e-4fff-8fff-dd92ffff0000 | 922.225549 | 2.236068e+08 | 3.162278e+07 | 1.000000e+01 | Opaque | Other | Leather |
| 0000157d-000e-4fff-8fff-dd92ffff0000 | 1049.571352 | 2.408319e+09 | 3.872983e+07 | 9.949874e+13 | Opaque | Polymer | Acrylonitrile Butadiene Styrene (Extrusion) |
| 00001584-000e-4fff-8fff-dd92ffff0000 | 1079.953746 | 1.999375e+09 | 4.289522e+07 | 9.949874e+13 | Transparent | Polymer | Acrylonitrile Butadiene Styrene (Transparent, … |
| 00001ca7-000e-4fff-8fff-dd92ffff0000 | 1095.415362 | 7.509658e+05 | 8.289964e+06 | 3.160000e+10 | Translucent | Polymer | Polyvinyl chloride elastomer (ultra high molec… |
| 00001570-000e-4fff-8fff-dd92ffff0000 | 1134.988977 | 1.308128e+10 | 1.064425e+08 | 3.162278e-02 | Opaque | Polymer | Acrylonitrile Butadiene Styrene/20% PAN Carbon… |
Specify column units
DataFrame doesn’t have a built-in method for specifying the units of a column. For this script, we will create a
separate dict that provides a simple mapping between column headings and units.
Create scatter plots using pandas
This snippet of code is optional, and is included to increase the size of all plots generated in this
notebook:
.plot() method to plot the data in your DataFrame.
By default, .plot() uses matplotlib to generate plots. Text wrapped in dollar signs is processed as LaTeX-style
math text.

Create scatter plots with matplotlib
Alternatively, you can use matplotlib directly with the DataFrame as an input. For comparison, here is the same plot
using matplotlib. The result is the same, but the figure and axes are first created with plt.subplots(), the axis
scales and labels modified, and then the ax.scatter() method is used to create the plot itself. Finally, the
top-level plt object is used to add a title.

Plotting data by category (discrete values)
Usingmatplotlib directly allows us to use the full power and flexibility of the package. For example, you can
create a plot color-coded by the value of a discrete attribute by iteratively adding scatter plots to the same set of
axes.
The simplest way to implement this is to use the .groupby() DataFrame method, which generates a set of groupby
objects. These behave like individual DataFrames, and can be plotted similarly to the previous example. matplotlib
will automatically increment the marker color for each new plot.
In the example below, the full DataFrame is separated into separate groupby objects that share the same Base
value. These DataFrame rows are each plotted on their own scatter plot with a separate color. The marker is
incremented manually through a pre-defined list, which ensures accessibility for users with color vision deficiencies.

Creating a 3D scatter plot
The set-up of a 3D scatter plot is slightly different to the 2D examples above, because the z-axis must be added as a subplot. Usefigure() and add_subplot(projection="3d") to create a 3D figure, then use an alternative syntax to
plot the data (in previous plots, the data was added using DataFrame column names, whereas this example plots the
arrays themselves).

Adding a continuous color axis to a 2D scatter plot
Add a continuous color axis using thec and cmap arguments to the plot.scatter() constructor.
This example uses the optional norm argument to specify an alternative method of mapping numeric values to a color
in the colormap. The color_norm variable contains a logarithmic normalization which ensures color variation is
used across all values of electrical resistivity.
The viridis colormap is perceptually uniform (monotonically increases in luminance) so is accessible to users with
color vision deficiencies.

Plotting data by category (binned point or range values)
For this plot, we first usenumpy.geomspace() to define 5 logarithmically-spaced bins and DataFrame.cut() to map
the continuous Electrical resistivity values to those categories.
| Density | Young’s modulus | Tensile strength | Electrical resistivity | Transparency | Base | Designation | Electrical resistivity (binned) | |
|---|---|---|---|---|---|---|---|---|
| guid | ||||||||
| 00000e41-000e-4fff-8fff-dd92ffff0000 | 2795.634849 | 7.074711e+10 | 4.436891e+08 | 4.298837e-08 | Opaque | Al (Aluminum) | 7075 | NaN |
| 00000e38-000e-4fff-8fff-dd92ffff0000 | 2799.839234 | 7.241547e+10 | 5.017171e+08 | 5.199038e-08 | Opaque | Al (Aluminum) | 7075 | Very low |
| 02401a77-c3e7-4b68-875c-7be3b735b703 | 15597.115118 | 6.340347e+11 | 4.446234e+08 | 7.943551e-07 | Opaque | Carbide | Tungsten Carbide | Very low |
| 000009cc-000e-4fff-8fff-dd92ffff0000 | 7829.901810 | 2.050641e+11 | 1.034283e+09 | 2.236068e-07 | Opaque | Fe (Iron) | AISI 4130 | Very low |
| 000009cb-000e-4fff-8fff-dd92ffff0000 | 7833.313776 | 2.050641e+11 | 6.528666e+08 | 2.236068e-07 | Opaque | Fe (Iron) | AISI 4130 | Very low |
.groupby() to plot each bin with a different color and shape of marker. A cycler.cycler object
specifies which colors will be used for each separate scatter plot.

Create scatter plots with seaborn
An alternative to using matplotlib directly is to use seaborn, a wrapper of matplotlib that aims to make it
easier to create professional-looking plots.
For example, here is the E vs rho plot grouped by ‘Base’ in seaborn. Note that the plot itself is defined in one
line, and we can specify DataFrame columns to use for the marker hue and style with no additional steps. The
legend is created automatically.

Plotting in n dimensions (pair plots)
Theseaborn package doesn’t expose matplotlib’s 3D scatter plotting functionality. However, it can produce stacked
(paired) plots of any dimensionality as required. The example below shows a comparison across 4 continuous dimensions.
To avoid having to set the label of each axis, we’ve created a new DataFrame with the desired axis labels as column
names. The labelling is then done automatically.
Although sns.pairplot doesn’t have a style argument, you can provide a list of markers instead. This must be the
same length as the number of categories. Here, we use itertools to cycle through the marker style list to get the
correct number.
