Visual learning.day1

squares ⇔ square
Plot ⇔ plot
Font size
Label ⇔ label
tick mark
params ⇔ parameter
scatter scattering

Visual learning

-----

The first day of study

1. Understand the matplotlib library.

When we draw the chart, we need to import matplotlib.pyplot.

Because the module pyplot contains many functions for generating charts.

2. Learn to draw a simple line chart.

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

Here, we define a list named (squares), pass the list to the function plot(), and then use the function show() to draw the icon.

3. Modify the label text and line thickness of the chart

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

#Set chart title and label axis
plt.title("Square Numbers",fontsize=24)
plt.xlabel("Value",fontsize=14)
plt.ylabel("Square of Value",fontsize=14)

#Set the scale mark size
plt.tick_params(axis="both",labelsize=14)
plt.show()

1. In plot():

Parameter: squares, passing the data of the list squares to plot
Parameter: linewidth, which determines the thickness of the line drawn by plot.

2. In title ():

Parameter: "Square Numbers" determines the title of the chart (Chinese is not allowed),
Parameter: fontsize=24 specifies the size of the text in the chart.

3. In xlabel() ylabel():

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

4. Tick? Params() sets the style of the scale.

Actual parameter: axis="both" determines the scale on the x-axis and y-axis,
Parameter: labelsize=14 sets the scale mark to font size 14.

4. Correct the figure

When we find that the chart does not have accurate data.

We can fix the problem by doing the following:
The reason may be that when we give a series of numbers, the x coordinate corresponding to the first data point given by it is 0, but we calculate at the beginning according to the first x coordinate (this is the default setting of the system when we set it). In order to change this default setting, we can give plot() in and out of the input value and the output value at the same time.

 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)
#Set chart title and label axis
plt.title("Square Numbers",fontsize=24)
plt.xlabel("Value",fontsize=14)
plt.ylabel("Square of Value",fontsize=14)
#Set the scale mark size
plt.tick_params(axis="both",labelsize=14)
plt.show()

As you can see, the x-axis and y-axis have become the values we provide. It does not need to make assumptions about how output values are generated, and the final graph is correct. (ps: when using the plot() function, you can specify various arguments, and you can also use many functions to customize the graph)

Use scatter() to draw and style a scatter chart

Sometimes, you need to draw a scatter chart and style individual data points.

For example, you might want to show smaller values in one color and larger values in another. When drawing large datasets, you can also set the same style for each point, and then use different style options
Draw some new points to highlight them.

1. Use scatter() to draw a single point

If you want to draw a single point, you can use the scatter() function and pass it a pair of x,y coordinates, which will draw a point at the specified location

import matplotlib.pyplot as plt

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

#Set chart title and label axis
plt.title("Square Numbers",fontsize=24)
plt.xlabel("Value",fontsize=14)
plt.ylabel("Square of Value",fontsize=14)

#Set the scale mark size
plt.tick_params(axis="both",which="major",labelsize=14)

plt.show()

Here, we call the scatter() function,
And used:
Parameter s=200 sets the size of the point used in drawing.
Argument 2 is the x coordinate of the point.
Argument 4 is the y coordinate of the point.

2. Use scatter() to draw a series of points

If you want to draw a series of points,
Two lists of x and y values can be passed to scatter():

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)

#Set chart title and label axis
plt.title("Square Numbers",fontsize=24)
plt.xlabel("Value",fontsize=14)
plt.ylabel("Square of Value",fontsize=14)
#Set the scale mark size
plt.tick_params(axis="both",labelsize=14)
plt.show()

Among them:
The list x ﹣ values contains the numbers whose squares are calculated.
The list y [values] contains the square of each of the preceding numbers.
When these lists are passed to scatter(), matplotlib reads a value from each list in turn to draw a point.
Therefore, the coordinates of all drawn points are (1,1), (2,4), (3,9), (4,16), (5,25), respectively

Published 1 original article, praised 0, visited 24
Private letter follow

Posted by schoolmommy on Sun, 19 Jan 2020 04:55:37 -0800