Home > OS >  matplotlib triplot and tricontourf
matplotlib triplot and tricontourf

Time:02-12

I'm attempting to plot a 2D dataset having unstructured coordinates in matplotlib using tricontourf. I'm able to generate a plot of the 'mesh' with triplot, however when I use the same Triangulation object for tricontourf, I get an error (see below). What am I missing? Example:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

def lower(x):
    return 2   1*x

def upper(x):
    return 60   4*x

ni = 10
nj = 12

x = np.linspace(1,15,ni)

## make a trapezoid
xy = np.zeros((ni,nj,2),dtype=np.float32)
for i in range(len(x)):
    y = np.linspace(lower(x[i]),upper(x[i]),nj)
    xy[i,:,0] = x[i]
    xy[i,:,1] = y

## add noise
xy  = -0.1   0.2*np.random.rand(ni,nj,2)

## make tris 'indices list'
xi, yi    = np.meshgrid(range(ni), range(nj), indexing='xy')
inds_list = np.stack((xi,yi), axis=2)
inds_list = np.reshape(inds_list, (ni*nj,2), order='C')
inds_list = np.ravel_multi_index((inds_list[:,0],inds_list[:,1]), (ni,nj), order='C')
inds_list = np.reshape(inds_list, (ni,nj), order='F')

tris = np.zeros((2*(ni-1)*(nj-1),3), dtype=np.int64)
ci=0
for i in range(ni-1):
    for j in range(nj-1):
        tris[ci,0] = inds_list[i 1, j 1]
        tris[ci,1] = inds_list[i,   j 1]
        tris[ci,2] = inds_list[i,   j  ]
        ci =1
        tris[ci,0] = inds_list[i,   j  ]
        tris[ci,1] = inds_list[i 1, j  ]
        tris[ci,2] = inds_list[i 1, j 1]
        ci =1

triangulation = mpl.tri.Triangulation(x=xy[:,:,0].ravel(), y=xy[:,:,1].ravel(), triangles=tris)

fig1 = plt.figure(figsize=(4, 4/(16/9)), dpi=300)
ax1 = plt.gca()
ax1.triplot(triangulation, lw=0.5)
#ax1.tricontourf(triangulation)
fig1.tight_layout(pad=0.25)
plt.show()

...produces

enter image description here

however, uncommenting the line with ax1.tricontourf

throws the error:

Traceback (most recent call last):
  File "test.py", line 54, in <module>
    ax1.tricontourf(triangulation)
  File "C:\Users\steve\AppData\Roaming\Python\Python38\site-packages\matplotlib\tri\tricontour.py", line 307, in tricontourf
    return TriContourSet(ax, *args, **kwargs)
  File "C:\Users\steve\AppData\Roaming\Python\Python38\site-packages\matplotlib\tri\tricontour.py", line 29, in __init__
    super().__init__(ax, *args, **kwargs)
  File "C:\Users\steve\AppData\Roaming\Python\Python38\site-packages\matplotlib\contour.py", line 812, in __init__
    kwargs = self._process_args(*args, **kwargs)
  File "C:\Users\steve\AppData\Roaming\Python\Python38\site-packages\matplotlib\tri\tricontour.py", line 45, in _process_args
    tri, z = self._contour_args(args, kwargs)
  File "C:\Users\steve\AppData\Roaming\Python\Python38\site-packages\matplotlib\tri\tricontour.py", line 60, in _contour_args
    z = np.ma.asarray(args[0])
IndexError: list index out of range

I am using:

Python version: 3.8.9

matplotlib version: 3.5.1

CodePudding user response:

I would say you need to provide the array of values to contour, e.g.:

x= xy[:,:,0].ravel()
z= np.random.rand(x.shape[0])
ax1.tricontourf(triangulation, z)
  • Related