1
+
2
+ # =============================================================================
3
+ # 快捷键 ctrl + 4 产生这部分注释模板
4
+ # =============================================================================
5
+
6
+
7
+ #%% 导入库
8
+ # =============================================================================
9
+ # 导入库
10
+ # =============================================================================
11
+
12
+ import numpy as np
13
+ import matplotlib .pyplot as plt
14
+ import seaborn as sns
15
+ #%% 等差数列
16
+ # =============================================================================
17
+ # 等差数列
18
+ # =============================================================================
19
+
20
+ a0 = 1 # 首项
21
+ n = 10 # 项数
22
+ d = 2 # 公差
23
+
24
+ a_array = np .arange (a0 , a0 + n * d , d )
25
+ print ('打印等差数列' ); print (a_array )
26
+
27
+ fig = plt .figure (figsize = (8 ,8 ))
28
+ plt .scatter (np .arange (n ), a_array )
29
+ plt .title ('Arithmetic Progression' )
30
+ plt .xlabel ('Index, $n$' )
31
+ plt .ylabel ('Value, $a_n$' )
32
+ plt .show ()
33
+ #%% 二元函数
34
+ # =============================================================================
35
+ # 二元函数
36
+ # =============================================================================
37
+
38
+ x1_array = np .linspace (- 3 , 3 , 301 )
39
+ x2_array = np .linspace (- 3 , 3 , 301 )
40
+ xx1 , xx2 = np .meshgrid (x1_array , x2_array )
41
+ # np.meshgrid() 函数将 x1_array 和 x2_array 两个一维数组转化为一个二维的网格点矩阵
42
+
43
+ ff = xx1 * np .exp (- xx1 ** 2 - xx2 ** 2 )
44
+ # 二元函数的曲面数据
45
+
46
+ # 可视化
47
+ fig = plt .figure (figsize = (8 ,8 ))
48
+ ax = fig .add_subplot (projection = '3d' )
49
+ # 绘制二元函数网格曲面
50
+ ax .plot_wireframe (xx1 , xx2 , ff , rstride = 10 , cstride = 10 )
51
+ plt .show ()
52
+ #%%
53
+ # =============================================================================
54
+ # 鸢尾花数据
55
+ # =============================================================================
56
+
57
+ # 加载鸢尾花数据集
58
+ iris_df = sns .load_dataset ('iris' )
59
+ # 显示数据集前5行
60
+ print ('打印鸢尾花数据前5行' ); print (iris_df .head ())
61
+
62
+ #%%%
63
+ fig , ax = plt .subplots (figsize = (8 ,8 ))
64
+ ax = sns .scatterplot (data = iris_df , x = "sepal_length" ,
65
+ y = "sepal_width" , hue = "species" )
66
+ # hue 用不同色调表达鸢尾花的类别
67
+
68
+ ax .set_xlabel ('Sepal length (cm)' )
69
+ ax .set_ylabel ('Sepal width (cm)' )
70
+ ax .set_xticks (np .arange (4 , 8 + 1 , step = 1 ))
71
+ ax .set_yticks (np .arange (1 , 5 + 1 , step = 1 ))
72
+ ax .axis ('scaled' )
73
+ ax .grid (linestyle = '--' , linewidth = 0.25 , color = [0.7 ,0.7 ,0.7 ])
74
+ ax .set_xbound (lower = 4 , upper = 8 )
75
+ ax .set_ybound (lower = 1 , upper = 5 )
76
+
77
+ #%%
0 commit comments