Data visualization tools matplotlib_

matplotlib

Here Insert Picture Description
matplotlib is a library dedicated to drawing, when analyzing the data, it can be used to visualize data, presenting more intuitive. Here are a few plotted by matplot

1. Getting started is simple

import matplotlib.pyplot as plt
%matplotlib inline
plt.figure()
plt.plot([1,2,3],[3,5,7])
plt.show()

Here Insert Picture Description

2. Image Structure

Here Insert Picture Description
other instructions:

Figure a (canvas) may comprise a plurality of axes (coordinates / drawing area), but only a part of axes a figure.
A axes (coordinates / drawing area) may comprise a plurality of Axis (axis), comprising two coordinate systems is the 2d, 3d coordinate system is the three
auxiliary display layer in Axes (plot area) In addition to the data plotted something other than the image shown, including axes appearance (facecolor), border line (Spines), the axis (axis), a coordinate axis name (axis label), the axis scale (the tick), axis scale labels (tick label) , grid lines (grid), legend (Legend), title (title) and so on. The image display layer is provided can be more intuitive and easier to understand the user, but does not affect the substance of the image.

Describes the basic drawing functions 3.pyplot

matplotlib is inspired by MATLAB constructed. MATLAB is widely used in the field of data mapping language and tools. MATLAB language is process-oriented. Using the Recall function, MATLAB can easily use the command line to draw a line, and then use a set of functions to adjust the results.

matplotlib fully modeled in the form of a set of MATLAB graphics interface function, that is, matplotlib.pyplot. pyplot is Matplotlib sub library, which provides the API and graphics similar matlab, user quickly draw 2D graph. pyplot contains all the basic functions of matplotlib, we generally also operate it.

When in use, we can import the data by the following statement:

import matplotlib.pyplot as plt
import matplotlib.pyplot as plt    # 导入模块

# 1)创建画布(容器层)
plt.figure(figsize=(10, 5))    # 10为绘图对象长度,5为宽度
# 2)绘制折线图(图像层)
plt.plot([1, 2, 3, 4, 5, 6 ,7], [17,17,18,15,11,11,13])
# 3)显示图像
plt.show()

result
Here Insert Picture Description

3.1 Creating the Canvas

Every time we intend to draw a graphic, they both need a canvas, some places also called drawing objects. General use plt.figure () function to create a canvas, such as:
Here Insert Picture Description

By figsize parameters, we can set the length and width of the canvas, in inches; dpi by parameters, we can specify the resolution of the drawing object, i.e., the number of pixels per inch, the default value is 80. In the above example, the width of the image is: 10 * 80 = 800 pixels. We can see and the following results were consistent.

3.2 rendering image

We usually draw a picture using plt.plot. We need to draw the image of a canvas, we can use the above figure to set a specific canvas, if you do not exactly set the canvas, then, when we use the plot, Matplotlib will automatically help us generate a canvas.

Here are two examples, the effect of their settings are the same:

plt.plot(x, y, label="$cos($x^2)", color="red", linewidth=2)
plt.plot(x, y, "r-", label="cos($x^2)$")

Let's look at an example of these parameters:

(1) x, y: two numbers is easy to understand, is that the two variables. We want to determine a point on the coordinate axes tend to determine two values: the value of the abscissa and ordinate values. We have time to draw a variety of images, there are line charts and graphs, you can use it to set. DETAILED difference thereof arranged two examples will explain later.

(2) The third parameter: In the second example, we set the second parameter "r-", it means disposed solid red.
Here Insert Picture Description
3) label: set content in the legend. Sometimes we need to add some data that formula, we can add before and after the string " symbol number M a t p l O t l i b on meeting Make use it Inside Inlay of l a t e x lead Qing painted system I They 使 "Symbol, Matplotlib will use its built-in rendering engine latex mathematical equations we write. When some text settings, we will use symbols, such as: r " r e a l l   b a d reall\ bad . "backslash is because $ intermediate Spaces are not recognized, the backslash to escape, so that the English word displayed pretty much experience.

(4) color: If we do not use a third argument, this parameter can be used to specify the line color.

(5) linewidth: the width specified for the curve.

The image output 3.3

There are two ways the output image, in a directly displayed, is stored as a picture to a local

(1) Direct Show

We can plt.show () function to display the image, the display will release resources to the canvas, if you save the picture after displaying the image, the picture can only save space.

(2) to save the image to your local

We can () function to save the image to a specified path by plt.savefig to note here: if the direct display and save it locally to be anything, save to local operating functions to be written before the show. For example: plt.savefig ( "D: \ demo.png").

We can also be specified by setting the dpi resolution of the image parameters, such as: plt.savefig ( "D: \ demo.png", dpi = 120).

3.4 auxiliary display layer

After we drawn graphics by plt.plot function, it is a very simple diagram, all we usually join the auxiliary display layer so that the whole image becomes full, the following will introduce some common settings.

(1) Display Legend

If only the label plt.plot () also shows the legend not ultimately, also need plt.legend () displays the legend, such as:

plt.legend(loc="best")

Its other parameters, as shown below:
Here Insert Picture Description
Note: If set to best, matplotlib automatically find a suitable position to display the legend.

(2) Current title Plot Area

For example: plt.title ( "My Matplotlib map").

(3) X, Y axis title

For example: plt.xlabel ( "Time"), plt.ylabel ( "temperature")

(4) were added grid lines

Add grid lines we use the function: plt.grid ()

3.5 Comprehensive example

import matplotlib.pyplot as plt 
import numpy as np

x = np.linspace(-3, 3, 100)    # 生成-3到3范围内的100个点。
y = x*4+1
z = x**2

plt.figure(figsize=(8,4))
plt.plot(x, y, label="$x*2+1$", color="red", linewidth=2)
plt.plot(x, z, "b--", label="$X^2$")
plt.plot([1, 2, 3, 4, 5, 6 ,7], [7,7,8,5,0.3,1,0.3], "k:", label="折线图")
plt.grid()
plt.legend(loc="best")
plt.show()

result
Here Insert Picture Description

4.Matplotlib three-tier architecture summary

Container layer

  • Sketchpad Canvas
  • Canvas Figure
  • Axes graphics drawing area
    X-axis
    Y-axis

Auxiliary display layer

  • grid
  • Graduation
  • Scale Description
  • illustration

Image layer

  • line chart
  • Scatter
  • Histogram
  • Pie
  • Histogram
He published 195 original articles · won praise 17 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43141726/article/details/104820496