Matplotlib processing scientific calculation data-L3

Source: Internet
Author: User
Tags api manual python script

The process of using matplotlib for scientific computing data can be simple or complex, and is usually determined by requirements. For example, simple to get two-dimensional data, according to the X coordinates to draw the corresponding y-coordinate values, which belong to matplotlib very basic function. From a general technical point of view, in order to achieve scripting code reuse, we should use object-oriented or modular programming method to write the data Processing program code, this tutorial is highly recommended methodology:
(This blog also tries to write with Markdown, previously just tried on GitHub) sorting out scientific data, determining the processing objects and requirements to determine the visual way, such as two-dimensional or three-dimensional graphs The module design of matplotlib script program according to the module design writing Python script code Generation visualization result Induction This reusable code, for other scientific calculation data processing module design

Modular programming is often associated with object-oriented programming, because the Python scripting language itself is an object-oriented programming language, so it provides a range of technologies to support object-oriented and modular features.
This tutorial will use the following design to ensure that the Matplotlib program code is sufficiently reusable, and the process flowchart for each scientific calculation data is as follows: Created with Raphaël 2.1.0 Start calculation data read compute data transform drawing data generate matplotlib operation visualization Results Compute data Reading module: The output of experimental data or scientific computing programs, which usually exist in a text file. Allow the existence of unformatted data; Data Transformation module: Transform into formatted data, such as two-dimensional data storage, grid data storage, etc. drawing data Generation module: Read formatted data, stored in vectors, matrices, or multidimensional tables. If it is necessary to perform a coordinate calculation, pixel calculation and other content, generate the content of subsequent visual drawings; Matplotlib operation module: Various kinds of visual drawing operation, can be encapsulation, provide function interface to the drawing data generation module, formal parameters are mainly various drawing data; visualization result module: displaying visualization results , matplotlib operations can directly generate pictures, or use Ipython these interactive tools for real-time display. instance (non 0 cloud map of the drawing matrix)

Data visualization or data visualisation is viewed by many disciplines as a modern equivalent of visual communication. It involves the creation and study of the visual representation of data (a study of the creation of data visualization and the virtual display of data), meaning "information that has been abstracted in some schematic form (this means that information can be highly abstracted into a schematic), including attributes or variables for the units of Informa tion ". --[Wikipedia]

A large number of matrix operations are involved in scientific computation, especially in the solution of linear equations and nonlinear systems. Matrix is often divided into dense matrix and sparse matrix, these two properties of the matrix if the same algorithm often does not achieve good computational efficiency. And if sparse matrices have certain peculiarities, such as banding or symmetry, it is more appropriate to use some special algorithms. Here we use Matplotlib to draw the image of the Matrix, show its sparsity, and observe the uniqueness of the matrix in a visual way.
Module Design
Use a table to illustrate:

Module Interface Design
Calculating data Reads Matrix data is read from the file, Read_matrix
Computing Data transformations Included in the calculation data read process, read-only nonzero matrix element coordinates
Drawing data generation Because the calculation data has been formatted for storage, this step no longer requires
Matplotlib operation Initialize the picture and artboard, INIT_VD, and draw the coordinate of the matrix element Non-zero by the rectangular square, Matrix_element_draw.
Visualization results Set picture name and keep formatting, FINISH_VD

From the above design, some of the modules are simplified processing, but the core of the drawing rectangle and draw the matrix program code can be reused later.
In addition, we can give a format of matrix data stored in the file:

2
1 1 3.5
1 2 0
2 1 0 2 2 3.5

The first line of 2 indicates that the matrix is 2x2, the first element in each row is the row coordinate, the second element is the column coordinate, and the third element is the value of the matrix element.
Of course, in order to reduce the amount of storage, the general data file only stores the matrix element is not a 0-value location:

2
1 1 3.5
2 2 3.5

After a decrease in the amount of data stored, the effect is the same as described earlier. For this example, the script code in the writing process should be based on the data file storage mode to adjust the content, fortunately we introduced a modular design, the function interface has been fixed, so no matter what format storage matrix data files, we only need to increase the Read_matrix function within the function can also achieve the purpose of code reuse.
this corresponds to the Python script code 
Using Matplotlib and NumPy:

Import OS
import sys
import numpy as NP
import Matplotlib.pyplot as plt

FILLS = [' White ', ' blue ', ' gray ', ' Red ']

def INIT_VD ():
  fig = plt.figure ()
  ax = fig.add_subplot (aspect= ' equal ') return
  AX

def FINISH_VD (Name, pic_name):
  plt.title (name)
  Plt.xlabel ("X Direction")
  Plt.xlabel ("Y Direction")
  plt.savefig (Pic_name, dpi=90)

if __name__ = = ' __main__ ':
  ax = INIT_VD ()
  Read_matrix (size, x, Y, Non_zero)

  print "Matrix has total%d Non-zero elements!"%non_zero matrix_element_draw

  (ax, x, Y, fills[1)) 
  FINISH_VD ("Matrix State", "Plot_matrix.png")

The program code for

This section Read_matrix and Matrix_element_draw is not posted, please refer to the instructions in the Design section to add it yourself, if you are interested to contact the author ( mingpz@qq.com) to obtain the source code. Conclusion This article is mainly about a methodology that will run through the entire tutorial on the visualization of scientific computing data to obtain code reuse goals. Matplotlib's operations can be accessed from the official website API manual, the actual problem is always the combination of these APIs, through the modular design can be used in advance of some of the operation of two encapsulation, throughout the tutorial to use. The example of this paper has a certain difficulty, if the in-depth study will involve computer graphics and numerical analysis of related content, especially large-scale matrix data visualization and equivalent statistical processing, widely used in meteorology, oceanography and other fields.
(recommended Netlib official website, for each developer who is engaged in scientific computing or interested in the reference)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.