当前位置:网站首页>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
边栏推荐
猜你喜欢

关于使用hyperbeach出现/bin/sh: 1: packr2: not found的解决方案

DTS搭载全新自研内核,突破两地三中心架构的关键技术|腾讯云数据库

mysql如何优化

The function and application of lpci-252 universal PCI interface can card

Solution to Chinese garbled code in console header after idea connects to database to query data

Detailed explanation of const usage in C language

Number of 0 at the end of factorial
![[untitled] JDBC connection database read timeout](/img/24/726ed8b3419866244a1b69e6485d7c.png)
[untitled] JDBC connection database read timeout

The new version of Alibaba Seata finally solves the idempotence, suspension and empty rollback problems of TCC mode

若依的环境的部署以及系统的运行
随机推荐
Wechat applet generation Excel
【树链剖分】2022杭电多校2 1001 Static Query on Tree
NLP hotspots from ACL 2022 onsite experience
Spark Learning Notes (VI) -- spark core core programming RDD action operator
[tree chain dissection] template question
Introduction to database - Introduction to database
connman介绍
【无标题】JDBC连接数据库读超时
[tree chain dissection] 2022 Hangzhou Electric Multi school 21001 static query on tree
LPCI-252通用型PCI接口CAN卡的功能和应用介绍
Worthington papain dissociation system solution
Member array and pointer in banyan loan C language structure
477-82(236、61、47、74、240、93)
Meta Quest内容生态总监谈App Lab设计初衷
代码回滚,你真的理解吗?
Redis source code learning (33), command execution process
Contour detection based on OpenCV (2)
unity之二维数组实现正六边形地图
Docker creates MySQL 8.x container and supports Mac and arm architecture chips
Spark: ranking statistics of regional advertising hits (small case)