Open In App

Create a pseudocolor plot of an unstructured triangular grid in Python using Matplotlib

Improve
Improve
Like Article
Like
Save
Share
Report

Matplotlib is a Python module which can be used for data visualization; and analyse the data graphically in the form of pie-chart, histogram, bar graph and many more. It also has an ability to generate MATLAB-like frameworks.

Unstructured triangular grid

An unstructured triangular grid contains n_points points and n_tri triangles which can either be specified by the user or automatically generated using a Delaunay triangulation.

Syntax : matplotlib.tri.Triangulation(x, y, triangles=None, mask=None)

Parameters:

  • x, y : specifies coordinates of grid points.
  • triangles : [optional] integer array-like of shape (n_tri, 3)
  • mask : [optional] it specifies which triangles are masked out.

Creating a pseudocolor plot of an unstructured triangular grid

We can plot a pseudo-color unstructured triangular grid with the tripcolor() function of the pyplot library.

Syntax : matplotlib.pyplot.tripcolor(*args, cmap=None, alpha=1.0, edgecolors=None, facecolors=None, shading=’flat’, norm=None, vmax=None, vmin=None, **kwargs)

Parameters:

  1. cmap : It can be None or matplotlib has a number of built-in colormaps accessible via matplotlib.cm.get_cmap
  2. alpha : It can be None or alpha value between 0 to 1.
  3. edgecolors :
    • If its None, edges will not be visible.
    • ‘face’ represents the same color as faces.
    • color sequence will set a color.
  4. facecolors : Mention the typefaces
  5. shading : It can be either ‘flat’ or ‘gouraud’
  6. norm : If its None defaults to normalize().
  7. vimax : It can be either None or the scalar value.
  8. vimin : It can be either None or the scalar value. ( vimax and vimin are used in conjunction with normalize data)

Example 1 :




import matplotlib.pyplot as plt
import matplotlib.tri as tri
import numpy as np
import math
  
# Creating a Triangulation without 
# specifying the triangles results in the
# Delaunay triangulation of the points.
  
# First create the x and y coordinates of the points.
angles = 36
n_radii = 8
min_radius = 0.25
radii = np.linspace(min_radius, 0.95, n_radii)
  
angles1 = np.linspace(0, 2 * math.pi, angles, endpoint = False)
angles1 = np.repeat(angles1[..., np.newaxis], n_radii, axis = 1)
angles1[:, 1::2] += math.pi / angles
  
x = (radii * np.cos(angles1)).flatten()
y = (radii * np.sin(angles1)).flatten()
z = (np.cos(radii)*np.cos(angles1 * 3.0)).flatten()
  
# Create  Delaunay triangulation.
triang = tri.Triangulation(x, y)
  
# Mask off unwanted triangles.
x1 = x[triang.triangles].mean(axis = 1)
y1 = y[triang.triangles].mean(axis = 1)
mask = np.where(x1 * x1 + y1 * y1 < min_radius * min_radius, 1, 0)
triang.set_mask(mask)
  
# Illustrate shading.
plt.figure()
plt.gca().set_aspect('equal')
  
tri = plt.tripcolor(triang, z,
                    shading ='gouraud',
                    cmap = plt.cm.rainbow,
                    alpha = 0.5,
                    edgecolors ='k')
  
plt.title('tripcolor_example1')
plt.colorbar(tri)


Output :

Example 2 :




xy = np.asarray([
    [-0.101, 0.872], [-0.080, 0.883], [-0.069, 0.888], [-0.054, 0.890],
    [-0.045, 0.897], [-0.057, 0.895], [-0.073, 0.900], [-0.087, 0.898],
    [-0.090, 0.904], [-0.069, 0.907], [-0.069, 0.921], [-0.080, 0.919],
    [-0.073, 0.928], [-0.052, 0.930], [-0.048, 0.942], [-0.062, 0.949],
    [-0.054, 0.958], [-0.069, 0.954], [-0.087, 0.952], [-0.087, 0.959],
    [-0.080, 0.966], [-0.085, 0.973], [-0.087, 0.965], [-0.097, 0.965],
    [-0.097, 0.975], [-0.092, 0.984], [-0.101, 0.980], [-0.108, 0.980],
    [-0.104, 0.987], [-0.102, 0.993], [-0.115, 1.001], [-0.099, 0.996],
    [-0.101, 1.007], [-0.090, 1.010], [-0.087, 1.021], [-0.069, 1.021],
    [-0.052, 1.022], [-0.052, 1.017], [-0.069, 1.010], [-0.064, 1.005],
    [-0.048, 1.005], [-0.031, 1.005], [-0.031, 0.996], [-0.040, 0.987],
    [-0.045, 0.980], [-0.052, 0.975], [-0.040, 0.973], [-0.026, 0.968],
    [-0.020, 0.954], [-0.006, 0.947], [ 0.003, 0.935], [ 0.006, 0.926],
    [ 0.005, 0.921], [ 0.022, 0.923], [ 0.033, 0.912], [ 0.029, 0.905],
    [ 0.017, 0.900], [ 0.012, 0.895], [ 0.027, 0.893], [ 0.019, 0.886],
    [ 0.001, 0.883], [-0.012, 0.884], [-0.029, 0.883], [-0.038, 0.879],
    [-0.057, 0.881], [-0.062, 0.876], [-0.078, 0.876], [-0.087, 0.872],
    [-0.030, 0.907], [-0.007, 0.905], [-0.057, 0.916], [-0.025, 0.933],
    [-0.077, 0.990], [-0.059, 0.993]])
x, y = np.rad2deg(xy).T
  
triangles = np.asarray([
    [67, 661], [652, 66], [ 1, 662], [642, 65], [633, 64],
    [60, 59, 57], [ 2, 643], [ 3, 634], [ 0, 671], [624, 63],
    [57, 59, 56], [59, 58, 56], [61, 60, 69], [57, 69, 60], [ 4, 62, 68],
    [ 659], [61, 68, 62], [69, 68, 61], [ 95, 70], [ 687],
    [ 4, 705], [ 869], [56, 69, 57], [69, 56, 52], [70, 109],
    [54, 53, 55], [56, 55, 53], [68, 704], [52, 56, 53], [11, 10, 12],
    [69, 71, 68], [68, 13, 70], [10, 70, 13], [51, 50, 52], [13, 68, 71],
    [52, 71, 69], [12, 10, 13], [71, 52, 50], [71, 14, 13], [50, 49, 71],
    [49, 48, 71], [14, 16, 15], [14, 71, 48], [17, 19, 18], [17, 20, 19],
    [48, 16, 14], [48, 47, 16], [47, 46, 16], [16, 46, 45], [23, 22, 24],
    [21, 24, 22], [17, 16, 45], [20, 17, 45], [21, 25, 24], [27, 26, 28],
    [20, 72, 21], [25, 21, 72], [45, 72, 20], [25, 28, 26], [44, 73, 45],
    [72, 45, 73], [28, 25, 29], [29, 25, 31], [43, 73, 44], [73, 43, 40],
    [72, 73, 39], [72, 31, 25], [42, 40, 43], [31, 30, 29], [39, 73, 40],
    [42, 41, 40], [72, 33, 31], [32, 31, 33], [39, 38, 72], [33, 72, 38],
    [33, 38, 34], [37, 35, 38], [34, 38, 35], [35, 37, 36]])
  
xmid = x[triangles].mean(axis = 1)
ymid = y[triangles].mean(axis = 1)
x0 = -8
y0 = 70
zfaces = np.exp(-0.01 * ((xmid - x0) * (xmid - x0) + (ymid - y0) * (ymid - y0)))
                          
fig3, ax3 = plt.subplots()
ax3.set_aspect('equal')
  
tpc = ax3.tripcolor(x, y, triangles,
                    facecolors = zfaces,
                    cmap ='Greys'
                    edgecolors ='k',
                    shading = 'flat',
                    alpha = 0.5)
  
ax3.set_title('tripcolor_example2')
ax3.set_xlabel('Longitude')
ax3.set_ylabel('Latitude')
  
fig3.colorbar(tpc)


Output :



Last Updated : 03 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads