The above little game tutorial can not be written down, later write it, today learn something new, learn more, find Python more powerful Ah!
Data visualization refers to the exploration of data through visual representations, which are closely related to data mining, and data mining refers to the use of code to explore the laws and associations of datasets. A dataset can be a small list of numbers that can be represented in a single line of code, or it can be gigabytes of data.
One of the most popular tools is Matplotlib, which is a mathematical drawing library that we will use to make simple charts such as line and scatter plots. We will then generate a more interesting data set based on the random walk concept-a chart generated from a series of random decisions. We will also use the Pygal package, which focuses on generating charts that are suitable for display on digital devices. By using Pygal, you can highlight elements and resize them as you interact with the chart, and easily adjust the size of the entire chart so that it fits on a micro smart watch or a giant display. We will use Pygal to explore the results of the dice in various ways.
Line Chart 1 draw a simple line chart
Below you can use Matplotlib to draw a simple line chart and customize it to enable richer data visualization. We will draw this chart using the square number sequence 1, 4, 9, 16, and 25来.
Import Matplotlib.pyplot as Pltsquares = [1, 4, 9, 25]plt.plot (squares) plt.show ()
Running results such as:
Plt.show () opens the Matplotlib viewer and displays the drawn graphics,
2 modifying label text and line weights
Import Matplotlib.pyplot as Pltsquares=[1,4,9,16,25]plt.plot (squares,linewidth=5) #设置图标标题 and label the Axes plt.title (" Square Numbers ", fontsize=24) Plt.xlabel (" value ", fontsize=14) Plt.ylabel (" Square of Value ", fontsize=14) # Set the size of the tick mark Plt.tick_params (axis= "both", labelsize=14) Plt.show ()
The code comments are detailed, and here are a few more highlights:
(1) The parameter linewidth determines the thickness of the line plotted by plot (). The function title () assigns a caption to the chart
(2) functions Xlabel () and Ylabel () allow you to set the title for each axis
(3) in the above code, there are multiple parameters fontsize specifies the size of the text in the chart.
(4) function tick_params () sets the style of the scale
Running results such as:
3 Correcting images
After the graph is easier to read, we find that the data is not plotted correctly: The end point of the line chart indicates that 4.0 squared is 25! Fix this problem below.
When you provide a series of numbers to plot (), it assumes that the first data point corresponds to the X coordinate value of 0, but our first point corresponds to an X value of 1. To change this default behavior, we can provide the input and output values for plot ():
Import Matplotlib.pyplot as Pltinput_values=[1,2,3,4,5]squares=[1,4,9,16,25]plt.plot (Input_values,squares, linewidth=5) #设置图标标题 and label the Axes 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 ()
The results are as follows:
Two scatter Figure 1 use scatter () to draw a scatter plot and set its style.
To draw a single point, you can use the function scatter () and pass it a pair of x and y coordinates, which will draw a point at the specified location:
Import Matplotlib.pyplot as Pltplt.scatter (2,4) plt.show ()
Here's how to style the output to make it more interesting: Add a title, label the axis, and make sure that all the text is large enough to see:
Import Matplotlib.pyplot as Pltplt.scatter (2,4,s=400) # set the chart title and label The Axes plt.title ("Square Numbers", fontsize=24) Plt.xlabel ("value", fontsize=14) Plt.ylabel ("Square of Value", fontsize=14) # Sets the size of the tick mark Plt.tick_params (axis= ' both ', Which= ' Major ', labelsize=14) plt.show ()
2 draw a series of points using scatter ()
To draw a series of points, pass two to scatter () with a list of x and Y values, as shown below:
Import Matplotlib.pyplot as Pltx_values=[1,2,3,4,5]y_values=[1,4,9,16,25]plt.scatter (x_values,y_values,s=400) # Set the chart title and label The Axes plt.title ("Square Numbers", fontsize=24) Plt.xlabel ("value", fontsize=14) Plt.ylabel ("Square of Value" , fontsize=14) # Sets the size of the tick mark Plt.tick_params (axis= ' both ', which= ' major ', labelsize=14) plt.show ()
The list x_values contains the number whose square value is to be computed, and the list y_values contains the square value of each number described above. When these lists are passed to scatter (), Matplotlib draws a single point from each list by reading a value in turn. The coordinates of the points to be drawn are (1, 1), (2, 4), (3, 9), (4, 16), and (5, 25), the final result
Three automatic calculation data
The manually calculated list of values that you want to include may be inefficient, especially if you need to draw a lot of points. Instead of having to manually calculate a list of point coordinates, let the python loop do the calculations for us. Here is the code that draws 1000 points:
Import Matplotlib.pyplot as Pltx_values = List (range (1,1001)) y_values = [x**2 for x in X_values]plt.scatter (X_values,y_va LUES,S=40) # Set the chart title and label The Axes plt.title ("Square Numbers", fontsize=24) Plt.xlabel ("Value", fontsize=14) Plt.ylabel (" Square of Value ", fontsize=14) # Sets the size of the tick mark Plt.tick_params (axis= ' both ', which= ' major ', labelsize=14) # Set the range of values for each coordinate plt.axis ([0,1100,0,1100000]) plt.show ()
Because of the large data set, we set the point smaller and use the function axis () to specify the range of values for each axis. The function axis () requires four values: the minimum and maximum values for the x and Y axes, and the results are as follows:
Four delete the outline of a data point
Matplotlib allows you to specify colors for each point in the scatter plot. The default is blue dots and black outlines, and the scatter plot contains a little bit of data that works well. But when you draw many points, the black outlines may stick together. To delete the outline of a data point you can pass the argument edgecolor= ' none ' when calling scatter ():
Plt.scatter (X_values, y_values, edgecolor= ' None ', s=40) after the corresponding call is modified to the above code, if you run scatter_squares.py again, you will see a blue solid point in the chart.
Five Custom Colors
To modify the color of a data point, pass the parameter C to scatter () and set it to the name of the color you want to use, as follows
Plt.scatter (X_values, y_values, c= ' Red ', edgecolor= ' none ', s=40)
You can also customize colors using the RGB color mode. To specify a custom color, pass the parameter C and set it to a tuple that contains the decimal values between the three 0~1, which represent the red, green, and blue components, respectively. For example, the following line of code creates a scatter plot that consists of a light blue dot:
Plt.scatter (X_values, Y_values, c= (0, 0, 0.8), edgecolor= ' None ', s=40)
The closer the value is to 0, the darker the specified color, the closer the value is to 1, and the lighter the specified color.
Six using color maps
A color map (COLORMAP) is a series of colors that fade from the starting color to the end color. In visualizations, color maps are used to highlight the laws of the data, for example, you might display smaller values with lighter colors and use darker colors to display larger values.
The module Pyplot has a set of color mappings built into it. To use these color mappings, you need to tell Pyplot how to set the color of each point in the dataset. The following shows how to set the color based on the Y value of each point:
Plt.scatter (X_values, Y_values, C=y_values, cmap=plt.cm.blues,edgecolor= "None", s=40)
We set the parameter C to a list of Y values and use the parameter cmap to tell Pyplot which color map to use. The code displays a point with a lower Y value as a light blue color, and a point with a large Y value as a dark blue, resulting shape.
Seven saving pictures automatically
To have the program automatically save the chart to a file, you can replace the call to Plt.show () with a call to Plt.savefig ():
Plt.savefig (' squares_plot.png ', bbox_inches= ' tight ')
The first argument specifies what file name to save the chart in, and the file is stored in the same directory as the scatter_squares.py, and the second argument specifies that the extra blank area of the chart is clipped. Omit this argument if you want to preserve extra white space around the chart.
Python Project Practice II (Generate data) first article