Matplotlib.axes.Axes.tricontourf() in Python - GeeksforGeeks

13 Apr, 2020
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.
The Axes.tricontourf() function in axes module of matplotlib library is also used to draw contours on an unstructured triangular grid. tricontour and tricontourf draw contour lines and filled contours, respectively.
Syntax:
Axes.tricontourf(ax, *args, **kwargs)
Parameters: This method accept the following parameters that are described below:
x, y: These parameter are the x and y coordinates of the data which is to be plot.
triangulation: This parameter is a matplotlib.tri.Triangulation object.
Z: This parameter is is the array of values to contour, one per point in the triangulation.
**kwargs: This parameter is Text properties that is used to control the appearance of the labels.All remaining args and kwargs are the same as for matplotlib.pyplot.plot().
All remaining args and kwargs are the same as for matplotlib.pyplot.plot().
Note: tricontourf-only keyword arguments:antialiased: This parameter is a bool enable antialiasing which used in contours on an unstructured triangular grid.
Returns: This returns the list of 2 Line2D containing following:
The lines plotted for triangles edges.
The markers plotted for triangles nodes
Below examples illustrate the matplotlib.axes.Axes.tricontourf() function in matplotlib.axes:
Example-1:
# Implementation of matplotlib functionimport matplotlib.pyplot as pltimport matplotlib.tri as mtriimport numpy as np      # Create triangulation.x = np.asarray([0, 1, 0, 3, 0.5, 1.5, 2.5, 1, 2, 1.5])y = np.asarray([0, 0, 0, 0, 1.0, 1.0, 1.0, 2, 2, 3.0])triangles = [[0, 1, 4], [1, 5, 4], [2, 6, 5], [4, 5, 7],             [5, 6, 8], [5, 8, 7], [7, 8, 9], [1, 2, 5],              [2, 3, 6]]  triang = mtri.Triangulation(x, y, triangles)z = np.cos(2.5 * x*x) * np.cos(1.5 * y*x)      fig, axs = plt.subplots()t = axs.tricontourf(triang, z)fig.colorbar(t)   axs.set_title('matplotlib.axes.Axes.tricontourf() Example')plt.show()
Output:
Example-2:
# Implementation of matplotlib functionimport matplotlib.pyplot as pltimport matplotlib.tri as triimport numpy as np    n_angles = 26n_radii = 10min_radius = 0.35radii = np.linspace(min_radius, 0.95, n_radii)  angles = np.linspace(0, 4 * np.pi, n_angles, endpoint = False)angles = np.repeat(angles[..., np.newaxis], n_radii, axis = 1)angles[:, 1::2] += np.pi / n_angles  x = (10 * radii * np.cos(angles)).flatten()y = (10 * radii * np.sin(angles)).flatten()z = (np.cos(4*(radii)**2) * np.cos(3 * (angles)**2)).flatten()  triang = tri.Triangulation(x, y)  triang.set_mask(np.hypot(x[triang.triangles].mean(axis = 1),                         y[triang.triangles].mean(axis = 1))                < min_radius)    fig1, ax1 = plt.subplots()ax1.set_aspect('equal')tcf = ax1.tricontourf(triang, z)fig1.colorbar(tcf)ax1.set_title('matplotlib.axes.Axes.tricontourf() Example')plt.show()
Output:
Python-matplotlib
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()