当前位置:网站首页>Machine learning [Matplotlib]
Machine learning [Matplotlib]
2022-07-27 03:47:00 【hike76】
List of articles
One Matplotlib
Matplotlib Is dedicated to the development of 2D Chart ( Include 3D Chart ), Gradually 、 Interactive way to realize data visualization
Visualization is a key auxiliary tool in data mining , Can clearly understand the data , To adjust our analytical methods .
Can visualize data , More intuitive presentation , Make the data more objective 、 More persuasive
1 Simple implementation
matplotlib.pytplot It contains a series of things like matlab Drawing function of .
# Import required libraries
import matplotlib.pyplot as plt
# Create a canvas
plt.figure(figsize=(10, 10), dpi=100)
# Draw line chart
plt.plot([1, 2, 3, 4, 5, 6 ,7], [17,17,18,15,11,11,13])
# Display images
plt.show()
Some attribute descriptions
figsize: Specify the length and width of the graph
dpi: The clarity of the image
return fig object
2 Matplotlib Image structure

3 Basic drawing function
demand : Draw a city 11 Point to 12 spot 1 Every... In an hour 5 Line chart of temperature change in minutes , The temperature range is 15 degree ~18 degree
(1) Prepare the data , Draw the initial line chart
import matplotlib.pyplot as plt
import random
# 0. Prepare the data
x = range(60)
y_shanghai = [random.uniform(15, 18) for i in x]
# 1. Create a canvas
plt.figure(figsize=(20, 8), dpi=80)
# 2. Draw line chart
plt.plot(x, y_shanghai)
# 3. Display images
plt.show()
(2) Add custom x,y scale
plt.xticks(x, **kwargs) x: The scale value to display
plt.yticks(y, **kwargs) y: The scale value to display
# structure x Axis scale label
# take i The value of is filled in braces
x_ticks_label = ["11 spot {} branch ".format(i) for i in x]
# structure y Axis scale
y_ticks = range(40)
# modify x,y The scale display of axis coordinates
# from 0 To 60, The interval is 5,
plt.xticks(x[::5], x_ticks_label[::5])
#plt.xticks(x_ticks_label[::5]) # error , The first parameter must be a number
# from 0 Start 40 end , The interval is 5
plt.yticks(y_ticks[::5])
(3) Add grid display
true Make the function effective ,linestyle Manage dashes (–) Or solid line (-),alpha Mesh transparency
plt.grid(True, linestyle='--', alpha=0.5)
(4) Add a description
add to x Axis 、y Axis description information and title
adopt fontsize Parameter can modify the size of the font in the image
plt.xlabel(" Time ")
plt.ylabel(" temperature ")
plt.title(" At noon, 11 spot 0 be assigned to 12 Diagram of temperature change between points ", fontsize=20)
(5) Image saving
Save the image to the specified path
plt.show() Will release figure resources , If you save an image after it is displayed, you can only save an empty image .
plt.savefig("path")
4 Drawing multiple images in one coordinate system
demand : Add another city's temperature change
Collect the temperature changes in Beijing on that day , The temperature is 1 C to 3 degree . Just need to do it again plot that will do , But you need to distinguish between lines ,
# Increase the temperature data of Beijing
y_beijing = [random.uniform(1, 3) for i in x]
# Draw line chart
plt.plot(x, y_shanghai,label=" Shanghai ")
# Use it many times plot You can draw multiple broken lines
plt.plot(x, y_beijing, color='r', linestyle='--',label=" Beijing ")
# Show Legend , Description information , Which color line represents which city
plt.legend(loc="best")
Use plt.legend() Show the legend , But it needs to be in plt.plot() Set in label value
(1) Graphic style
| Color characters (color) | Style character (linestyle) |
|---|---|
| r Red | - Solid line |
| g green | - - Dotted line |
| b Blue | -. Point line |
| w white | : Dotted line |
| c Cyan | ’ ’ leave a blank 、 Space |
| m Magenta | |
| y yellow | |
| k black |
(2)loc Code
| Location String | Location Code |
|---|---|
| ‘best’ | 0 |
| ‘upper right’ | 1 |
| ‘upper left’ | 2 |
| ‘lower left’ | 3 |
| ‘lower right’ | 4 |
| ‘right’ | 5 |
| ‘center left’ | 6 |
| ‘center right’ | 7 |
| ‘lower center’ | 8 |
| ‘upper center’ | 9 |
| ‘center’ | 10 |
(3) Complete code
# 0. Prepare the data
x = range(60)
y_shanghai = [random.uniform(15, 18) for i in x]
y_beijing = [random.uniform(1,3) for i in x]
# 1. Create a canvas
plt.figure(figsize=(20, 8), dpi=100)
# 2. The plot
plt.plot(x, y_shanghai, label=" Shanghai ")
plt.plot(x, y_beijing, color="r", linestyle="--", label=" Beijing ")
# 2.1 add to x,y Axis scale
# structure x,y Axis scale label
x_ticks_label = ["11 spot {} branch ".format(i) for i in x]
y_ticks = range(40)
# Scale display
plt.xticks(x[::5], x_ticks_label[::5])
plt.yticks(y_ticks[::5])
# 2.2 Add grid display
plt.grid(True, linestyle="--", alpha=0.5)
# 2.3 Add a description
plt.xlabel(" Time ")
plt.ylabel(" temperature ")
plt.title(" At noon, 11 spot --12 Point to the temperature change map of a city ", fontsize=20)
# 2.4 Image saving
plt.savefig("./test.png")
# 2.5 Add legend
plt.legend(loc=0)
# 3. Image display
plt.show()
5 Display images in multiple coordinate systems
matplotlib.pyplot.subplots(nrows=1, ncols=1, **fig_kw) Create one with multiple axes( Coordinate system / Drawing area ) Graph
Parameters:
nrows, ncols : There are several rows and columns of coordinate system
int, optional, default: 1, Number of rows/columns of the subplot grid.
Returns:
fig : Picture object
axes : Returns the corresponding number of coordinate systems , Use axes Draw the image
There are different ways to set the title :
set_xticks
set_yticks
set_xlabel
set_ylabel
# 0. Prepare the data
x = range(60)
y_shanghai = [random.uniform(15, 18) for i in x]
y_beijing = [random.uniform(1, 5) for i in x]
# 1. Create a canvas
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(20, 8), dpi=100)
# 2. The plot
axes[0].plot(x, y_shanghai, label=" Shanghai ")
axes[1].plot(x, y_beijing, color="r", linestyle="--", label=" Beijing ")
# 2.1 add to x,y Axis scale
x_ticks_label = ["11 spot {} branch ".format(i) for i in x]
y_ticks = range(40)
# Scale display
axes[0].set_xticks(x[::5])
axes[0].set_yticks(y_ticks[::5])
axes[0].set_xticklabels(x_ticks_label[::5]) # Replace... With a string
axes[1].set_xticks(x[::5])
axes[1].set_yticks(y_ticks[::5])
axes[1].set_xticklabels(x_ticks_label[::5]) # Replace... With a string
# 2.2 Add grid display
axes[0].grid(True, linestyle="--", alpha=0.5)
axes[1].grid(True, linestyle="--", alpha=0.5)
# 2.3 Add a description
axes[0].set_xlabel(" Time ")
axes[0].set_ylabel(" temperature ")
axes[0].set_title(" At noon, 11 spot --12 Point Beijing temperature change diagram ", fontsize=20)
axes[1].set_xlabel(" Time ")
axes[1].set_ylabel(" temperature ")
axes[1].set_title(" At noon, 11 spot --12 Point Shanghai temperature change diagram ", fontsize=20)
# # 2.4 Image saving
plt.savefig("./test.png")
# # 2.5 Add legend
axes[0].legend(loc=0)
axes[1].legend(loc=0)
# 3. Image display
plt.show()
6 Application scenarios of line chart
- Present the company's products ( Different regions ) Number of active users per day
- present app Number of downloads per day
- Present the new functions of the product , The number of user clicks over time
Use plt.plot() draw sin Images
import numpy as np
# 0. Prepare the data
x = np.linspace(-10, 10, 1000) # from -10 To 10 Generate 1000 Data
y = np.sin(x)
# y = x * x * x
# 1. Create a canvas
plt.figure(figsize=(20, 8), dpi=100)
# 2. Draw function image
plt.plot(x, y)
# 2.1 Add grid display
plt.grid()
# 3. Display images
plt.show()
7 Common graphic drawing
(1) Broken line diagram
A statistical chart showing the increase or decrease of statistical quantity by the rise or fall of a broken line
characteristic : It can show the trend of data change , Reflect the change of things .( change )
api:plt.plot(x, y)
(2) Scatter plot
Two sets of data are used to form multiple coordinate points , Look at the distribution of coordinate points , Judge whether there is some association between two variables or summarize the distribution pattern of coordinate points .
characteristic : Determine whether there is a quantitative correlation trend between variables , Show outliers ( The law of distribution )
api:plt.scatter(x, y)
(3) Histogram
Data arranged in columns or rows of a worksheet can be drawn into a histogram .
characteristic : Draw even discrete data , Can see the size of each data at a glance , Compare the differences between the data .( Statistics / contrast )
api:plt.bar(x, width, align='center', **kwargs)
Parameters:
x : Data to be passed
width : The width of the histogram
align : The position alignment of each histogram
{‘center’, ‘edge’}, optional, default: ‘center’
**kwargs :
color: Choose the color of the histogram
(4) Histogram
Data distribution is represented by a series of vertical stripes or line segments with different heights . Generally, the horizontal axis is used to represent the data range , The vertical axis shows the distribution .
characteristic : Draw continuous data to show the distribution of one or more groups of data ( Statistics )
api:matplotlib.pyplot.hist(x, bins=None)
Parameters:
x : Data to be passed
bins : Group spacing
(5) The pie chart
Used to indicate the proportion of different classifications , Compare various classifications by radian size .
characteristic : The proportion of classified data ( Proportion )
api:plt.pie(x, labels=,autopct=,colors)
Parameters:
x: Number , Automatically calculate percentage
labels: The name of each part
autopct: The percentage display specifies %1.2f%%
colors: Color of each part
(6) Scatter plot
# 0. Prepare the data
x = [225.98, 247.07, 253.14, 457.85, 241.58, 301.01, 20.67, 288.64,
163.56, 120.06, 207.83, 342.75, 147.9 , 53.06, 224.72, 29.51,
21.61, 483.21, 245.25, 399.25, 343.35]
y = [196.63, 203.88, 210.75, 372.74, 202.41, 247.61, 24.9 , 239.34,
140.32, 104.15, 176.84, 288.23, 128.79, 49.64, 191.74, 33.1 ,
30.74, 400.02, 205.35, 330.64, 283.45]
# 1. Create a canvas
plt.figure(figsize=(20, 8), dpi=100)
# 2. Draw a scatter plot
plt.scatter(x, y)
# 3. Display images
plt.show()
(7) Histogram drawing
demand : Compare the box office receipts of each movie
# 0. Prepare the data
# The name of the movie
movie_name = [' The thor 3: The gods at dusk ',' Justice League: Injustice for All ',' Murder on the Orient express ',' Dream seeking travel notes ',' Global storms ',' Legend of conquering the devil ',' chase ',' Seventy-seven days ',' Secret War ',' Wild beast ',' Other ']
# Abscissa
x = range(len(movie_name))
# Box office data
y = [73853,57767,22354,15969,14839,8725,8716,8318,7916,6764,52222]
# 1. Create a canvas
plt.figure(figsize=(20, 8), dpi=100)
# 2. Draw a histogram
plt.bar(x, y, width=0.5, color=['b','r','g','y','c','m','y','k','c','g','b'])
# 2.1 modify x The scale of the axis shows
plt.xticks(x, movie_name)
# 2.2 Add grid display
plt.grid(linestyle="--", alpha=0.5)
# 2.3 Add the title
plt.title(" Movie box office revenue comparison ")
# 3. Display images
plt.show()
[ Reference link ]:https://matplotlib.org/index.html
边栏推荐
- Contour detection based on OpenCV (2)
- Daffodils (day 78)
- 网络安全/渗透测试工具AWVS14.9下载/使用教程/安装教程
- [untitled] JDBC connection database read timeout
- Details of impala implementation plan
- 数字孪生实际应用:智慧城市项目建设解决方案
- What are "full five unique" and "full two unique"? Any difference?
- PyCharm中Debug模式进行调试详解
- 深圳家具展首日,金可儿展位三大看点全解锁!
- J-3-point practice in the second game of 2022 Niuke multi school
猜你喜欢

Spark: ranking statistics of regional advertising hits (small case)

阿里 Seata 新版本终于解决了 TCC 模式的幂等、悬挂和空回滚问题
![[tree chain dissection] 2022 Hangzhou Electric Multi school 21001 static query on tree](/img/b3/58c053d082807a0e5639e5b968aa20.png)
[tree chain dissection] 2022 Hangzhou Electric Multi school 21001 static query on tree

Explain详解

477-82(236、61、47、74、240、93)

The application and significance of digital twins are the main role and conceptual value of electric power.

数据库概论 - 数据库的介绍

DTS is equipped with a new self-developed kernel, which breaks through the key technology of the three center architecture of the two places Tencent cloud database

477-82(236、61、47、74、240、93)

mysql底层数据结构
随机推荐
Solution to Chinese garbled code in console header after idea connects to database to query data
Unity game, the simplest solution of privacy agreement! Just 3 lines of code! (Reprinted)
百融榕树数据分析拆解方法
Characteristics and determination scheme of Worthington pectinase
数字孪生实际应用:智慧城市项目建设解决方案
如何进行 360 评估
Learning and understanding of four special data types of redis
Characteristics and determination scheme of Worthington pectinase
Introduction to database - Introduction to database
PyCharm中Debug模式进行调试详解
[learn FPGA programming from scratch -54]: high level chapter - FPGA development based on IP core - principle and configuration of PLL PLL IP core (Altera)
[understanding of opportunity -52]: the depth of communication varies from person to person
数据库概论 - MySQL的简单介绍
智能体重秤方案主控采用CSU18M91
快速排序及优化
Design method and test method of APP interface use case
Method of converting curtain article OPML into markdown
Record the problem of PHP program accessing system files incorrectly
Kettle读取按行分割的文件
常见弱口令大全