
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Draw Unstructured Triangular Grid in Python Using Matplotlib
Python is a popularly used programming language. It offers a wide range of tools and libraries which can be used for solving different problems, one of them is Matplotlib. This library provides various functions for data visualization and creating different plots. In this article, we will be using Matplotlib for drawing an unstructured triangular grid as liners or markers in python.
What is Matplotlib and How to Install it?
Matplotlib is one of the libraries of python. This library is very strong tool for serving the purpose of plotting graphs for visualizing data. It has a module named "pyplot" which makes things easy for plotting by providing features to control line styles, font properties, formatting axes, etc. It can also integrate with other libraries such as numpy which is a library known for numerical computing in python.
You can install this library on your system by passing a command in the command prompt ?pip install matplotlib'.
After installing the library in your system you can import it in your code for using the functions offered. Since we have to draw an unstructured triangular grid, matplotlib have several functions for it such as triplot() and plot_trisurf() etc.
What is an Unstructured Triangular Grid?
The unstructured triangular grid is popularly used in understanding different systems such as computational modeling, simulations, where the data is complex and needs to be solved for understanding the underlying concept.
So, unstructured triangular grid in itself is a way of dividing Euclidean plane into different shapes such as triangles and tetrahedral without following a regular pattern. Hence, you can use it to visualize complex data.
Now, what does lines or markers mean here? Lines or markers are a part of it, they show how the grid is represented in a plot. Lines connect the vertices of the triangles in the grid and hence provide a visual representation of the boundaries of the triangles in the grid, whereas markers represent the vertices of the triangles in the grid.
Example
Let's draw unstructured triangular grid using matplotlib in python.
import matplotlib.pyplot as plt import numpy as np import matplotlib.tri as tri # for generating random points ran_points = 80 x = np.random.rand(ran_points) y = np.random.rand(ran_points) # triangulation of the points triangle_point = tri.Triangulation(x, y) # Plot the triangulation with markers plt.triplot(triangle_point, 'bo-', lw=1) plt.title('Unstructured Triangular Grid') plt.show()
Output
Example
Here, we have imported three libraries and the code remains same. You make changes to the triangular grid.
In the last line of plotting the triangular grid we have used the ?plt.tricontourf(tri, values)' for creating a filled contour plot.
The tricontourf() function use linear estimated values for computing at the vertices of each triangle and then fill the triangles with the colour of the corresponding value.
Then, the ?plt.triplot(tri, marker='2', markersize=3, linestyle='-', color='k')' function is used it covers the triangular grid on top of the contour plot as black lines using triplot.
import numpy as np import matplotlib.pyplot as plt import matplotlib.tri as mtri # for generating random points ranpoints = 30 x = np.random.rand(ranpoints) y = np.random.rand(ranpoints) tri = mtri.Triangulation(x, y) # Generate random values to color the triangles values = np.random.rand(ranpoints) # Plot the triangular grid plt.tricontourf(tri, values) plt.triplot(tri, marker='2', markersize=3, linestyle='-', color='k') plt.show()
Output
Common Errors
You may face some of the common errors while drawing unstructured triangular grids using matplotlib. Let's see some of them with solution of each.
ValueError: You may face this error if the length of the x and y arrays passed to the triangulation constructor are not equal. For fixing this error, try to put same number of elements for each.
AttributeError: This error can occur when get_masked_triangles are used on a triangulation object which has no mask set. For fixing this error, you have to set a mask for the triangulation object using triang.set_mask method.
TypeError: If you try to access an element of a float object using subscript notation. For avoiding this you should make sure that the variable accessed is an array or a list.
ValueError: The third argument must be a format string - this error can occur when the format string passed to the triplot() function is not valid. To avoid this error, the format string should be a valid matplotlib line style format.
Conclusion
In this article, we have drawn different unstructured triangular grid as markers or lines using matplotlib. We started the article from the basics that is by explaining what matplotlib is and the functions which will be used to draw unstructured triangular grid.
A brief explanation is given on unstructured triangular grid and its use. Using different functions of Matplotlib library we have drawn the grid. This shows how powerful and useful python libraries are for simplifying the complex data using data visualization.