Visual learning .day1

squares⇔ square
plot⇔ drawn
fontsize⇔ font size
label⇔ tag
tick⇔ mark
params⇔ parameters
scatter⇔ scattering, scatter

Visual learning

——————————

The first day of learning

1. Understand matplotlib library.

在我们绘制图表时,需要import matplotlib.pyplot。

Because pyplot module includes many functions used to generate the graph.

2. Learn to draw a simple line graph.

import matplotlib.pyplot as plt
squares = [1,4,9,16,25]
plt.plot(squares)
plt.show

Here the definition of a named (squares) list, the list will be passed to the function plot (), and finally use the function show () icon will be drawn.

3. Modify the chart label text and line thickness

  import matplotlib.pyplot as plt
squares = [1,4,9,16,25]
plt.plot(squares,linewidth=5)

#设置图表标题,并给坐标轴加上标签
plt.title("Square Numbers",fontsize=24)
plt.xlabel("Value",fontsize=14)
plt.ylabel("Square of Value",fontsize=14)

#设置刻度标记的大小
plt.tick_params(axis="both",labelsize=14)
plt.show()

1.plot () in:

Parameters: squares, the squares of the list of data to the plot
parameters: linewidth, determines the thickness plot to draw the line.

2.title () in:

Parameters: "Square Numbers" determines the title of the chart (can not use Chinese),
parameters: fontsize = 24 specifies the size of the graph text.

3.xlabel () ylabel () in:

Parameters: "Value", "Square of Value" determines the x, y axis title,
parameters: fontsize = 14 determines the font size.

4.tick_params () to set the style scale.

Argument: axis = "both" determines the scale of the x and y axes,
the parameter: labelsize = 14 the tick marks 14 is set to the font size.

4. The correction pattern

当我们发现图表,没有准确的绘制数据时。

We can look at the operation to fix the problem:
The reason may be that when we given a series of numbers, given its first data corresponding point corresponding to the x coordinate of 0, but we are in accordance with the first x-coordinate for the start computing (this is in itself as a set, the default settings of the system), in order to change this default setting, we can give the plot () at the same time out of the input value, the output value.

 import matplotlib.pyplot as plt

input_values = [1, 2, 3, 4, 5]
squares = [1, 4, 9, 16, 25]
plt.plot(input_values, squares, linewidth=5)
#设置图表标题并给坐标轴加上标签
plt.title("Square Numbers",fontsize=24)
plt.xlabel("Value",fontsize=14)
plt.ylabel("Square of Value",fontsize=14)
#设置刻度标记的大小
plt.tick_params(axis="both",labelsize=14)
plt.show()

It can be seen that, at this time the x-axis and y-axis has become available to us the value. It does not need to make assumptions about the way to generate an output value of the final graphic is correct. (Ps: using plot () may specify a variety of arguments to a function, the function can also be used for a number of customized graphics)

Use Scatter () plotted scattergram and styling

有时候,需要绘制散点图并设置各个数据点的样式。

For example, you might want to show a smaller value in one color, and display greater value with another color. When drawing large data sets, you can also set for each point are the same style, and then use a different style options re
new to draw some point, to highlight them.

1. Scatter () rendering single point

If you want to draw a single point, may be used Scatter () function, passing it the one pair of x, y coordinates, it will draw a dot at a designated position

import matplotlib.pyplot as plt

plt.scatter(2,4,s=200)

#设置图表标题并给坐标轴加上标签
plt.title("Square Numbers",fontsize=24)
plt.xlabel("Value",fontsize=14)
plt.ylabel("Square of Value",fontsize=14)

#设置刻度标记的大小
plt.tick_params(axis="both",which="major",labelsize=14)

plt.show()

Here, we call Scatter () functions,
and uses:
argument s = 200 set the size of dots used to draw shapes.
Argument 2 x coordinates as points.
4 as the argument y coordinate point.

2. Use the scatter () draws a series of points

If you want to plot a series of points,
it may be () is transmitted to the scatter lists contain the two values x and y values:

import matplotlib.pyplot as plt
x_values = [1,2,3,4,5]
y_values = [1,4,9,16,25]

plt.scatter(x_values,y_values,s=100)

#设置图表标题并给坐标轴指定标签
plt.title("Square Numbers",fontsize=24)
plt.xlabel("Value",fontsize=14)
plt.ylabel("Square of Value",fontsize=14)
#设置刻度标记的大小
plt.tick_params(axis="both",labelsize=14)
plt.show()

Wherein:
the list comprises a digital x_values its square is calculated.
Y_values list comprising the square of each digit.
When the list is passed to scatter (), matplotlib sequentially reads a value from each list to draw a point.
Therefore, all the plotted points of coordinates (1,1), (2,4), (3,9), (4,16), (5,25)

Published an original article · won praise 0 · Views 40

Guess you like

Origin blog.csdn.net/qq_46014581/article/details/104037327