contour

This section describes various options available for contour plots in fivecentplots

See the full API

Setup

Import packages:


import fivecentplots as fcp
import pandas as pd
from pathlib import Path

Read some fake contour data:


df = pd.read_csv(Path(fcp.__file__).parent / 'test_data/fake_data_contour.csv')
df.head()

Experiment Batch X Y Value
0 Control 101 1 -4 3.5
1 Control 101 1 -2 2.1
2 Control 101 1 0 3.3
3 Control 101 1 2 3.2
4 Control 101 1 4 4.0

Optionally set the design theme (skipping here and using default):


#fcp.set_theme('gray')
#fcp.set_theme('white')

Basic contour

A basic contour plot without fill can be generated using keyword filled=False. We can disable a colorbar using keyword cbar=False:


fcp.contour(df, x='X', y='Y', z='Value', filled=False, cbar=False)
_images/contour_12_0.png

Contour lines

We can control the style and number of the contour lines as shown below to get a better result:


fcp.contour(df, x='X', y='Y', z='Value', filled=False, cbar=False, levels=40, contour_width=2,
            xmin=-4, xmax=5, ymin=-4, ymax=6)
_images/contour_15_0.png

Contour points

The x and y points used to make the contour can also be overlaid on the contour lines, using keyword show_points=True. Markers can be styled according to typical keywords for the markers Element class:


fcp.contour(df, x='X', y='Y', z='Value', filled=False, cbar=False, levels=40, contour_width=2,
            xmin=-4, xmax=5, ymin=-4, ymax=6, show_points=True, marker_size=26, marker_fill_color='#00FF00')
_images/contour_18_0.png

Grouping

We can also use other columns in the DataFrame to easily group the data into multiple contour subplots:


fcp.contour(df, x='X', y='Y', z='Value', row='Batch', col='Experiment', filled=False,
            cbar=False, xmin=-3, xmax=3, ymin=-3, ymax=3, ax_size=[250,250], contour_width=2,
            label_rc_font_size=12)
_images/contour_21_0.png

Filled contour

Next we can fill the contours using the keyword filled or contour_filled. We can also add a colorbar to show the range of z-values in the plot using the keyword cmap=True:


fcp.contour(df, x='X', y='Y', z='Value', row='Batch', col='Experiment', filled=True,
            cbar=True, xmin=-3, xmax=3, ymin=-3, ymax=3, ax_size=[250,250],
            label_rc_font_size=12, levels=40)
_images/contour_24_0.png

And with a fixed z-range:


fcp.contour(df, x='X', y='Y', z='Value', row='Batch', col='Experiment', filled=True,
            cbar=True, xmin=-3, xmax=3, ymin=-3, ymax=3, ax_size=[250,250],
            label_rc_font_size=12, zmin=1, zmax=3, levels=40)
_images/contour_26_0.png

Data interpolation

The x, y, and z data provided in the DataFrame needs to be transformed into a grid for the contour. This is done using numpy and scipy modules:

xi = np.linspace(min(xx), max(xx))
yi = np.linspace(min(yy), max(yy))
zi = scipy.interpolate.griddata((xx, yy), zz, (xi[None, :], yi[:, None]), method=method)

where the method is supplied using the keyword interp. Any standard scipy argument is allowed but a default of “cubic” is used. Observe the differences between “cubic” and “linear” below:


fcp.contour(df, x='X', y='Y', z='Value', cbar=False, levels=40, contour_width=2,
            xmin=-3, xmax=3, ymin=-3, ymax=3, interp='cubic')
_images/contour_29_0.png

fcp.contour(df, x='X', y='Y', z='Value', cbar=False, levels=40, contour_width=2,
            xmin=-3, xmax=3, ymin=-3, ymax=3, interp='linear')
_images/contour_30_0.png