python中有一段语句是 def die sys..exit 请问def die是什么意思?结束程序不是只用sys.exit就行了吗

Python025

python中有一段语句是 def die sys..exit 请问def die是什么意思?结束程序不是只用sys.exit就行了吗,第1张

你是不是少了什么东西

def die():

sys.exit()

这样还比较正常一点

只是吧sys.exit()封装成一个函数而已

die()要比

sys.exit()少打不少字符呢,对吧

简单介绍python中的绘图

绘制简单的折线图

散点图

设置每个坐标轴的取值范围

将主点颜色改成白,边缘颜色为红

自动保存图表

随机漫步

随机漫步渐变色

小知识点

绘制起点和终点

画布尺寸

隐藏坐标轴

pygal

roll one dice

绘制简单的折线图

首先下载matplotlib安装程序

import matplotlib.pyplot as plt

x_values = [1, 2, 3, 4, 5, 6] # 代表x轴的值

y_values = [1, 2, 4, 8, 16, 32] # 代表与x相对应的y轴的值

# plt.plot(y_values, linewidth=3)

plt.plot(x_values, y_values, linewidth=3)

# 设置图表标题,并改变横纵坐标的名称

plt.title("figure test", fontsize=24)

plt.xlabel("xValue", fontsize=14)

plt.ylabel("yValue", fontsize=14)

# 更改刻度标记的大小

plt.tick_params(axis='both', labelsize=12)

plt.show() # 打开matplotlib查看器,显示绘制的图形

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

散点图

import matplotlib.pyplot as plt

# 绘制单个x = 2, y = 4坐标轴上的点

# plt.scatter(2, 4, s=100) # s用来更改单个点的大小

# 绘制一群点

x_values = [1, 2, 3, 4, 5]

y_values = [1, 2, 4, 8, 16]

plt.scatter(x_values, y_values, s=100)

# 设置图表标题,并改变横纵坐标的名称

plt.title("figure test", fontsize=24)

plt.xlabel("xValue", fontsize=14)

plt.ylabel("yValue", fontsize=14)

# 更改刻度标记的大小

plt.tick_params(axis='both', which = 'major', labelsize=12)

plt.show()

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

另一种方法绘制此图:(自动生成数据)

import matplotlib.pyplot as plt

# 绘制单个x = 2, y = 4坐标轴上的点

# plt.scatter(2, 4, s=100) # s用来更改单个点的大小

# 绘制一群点

i = 1

x_values = list(range(1, 6))

y_values = [2**(i-1) for i in x_values]

plt.scatter(x_values, y_values, s=100)

# 设置图表标题,并改变横纵坐标的名称

plt.title("figure test", fontsize=24)

plt.xlabel("xValue", fontsize=14)

plt.ylabel("yValue", fontsize=14)

# 更改刻度标记的大小

plt.tick_params(axis='both', which='major', labelsize=12)

plt.show()

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

设置每个坐标轴的取值范围

plt.axis([0, 100, 0, 1000])

1

1

前面对应x, 后面对应y

将主点颜色改成白,边缘颜色为红

plt.scatter(x_values, y_values, c='white', edgecolor='red', s=20)

plt.scatter(x_values, y_values, c=(1, 1, 1), edgecolor='red', s=20)

1

2

1

2

c for color含三个0~1之间的小数值

颜色映射(colormap)

plt.scatter(x_values, y_values, c=y_values, cmap=plt.cm.Reds,

edgecolor='none', s=40)

1

2

1

2

要了解pyplot中所有的颜色映射: https://matplotlib.org/gallery/color/colormap_reference.html#sphx-glr-gallery-color-colormap-reference-py

自动保存图表

plt.savefig(‘Figure1.png’, bbox_inches=‘tight’)

第一个参数表示名字, 将其以Figure1.png存储在example.py同一个文件夹里

第二个参数表示将图表多余的空白区域裁剪掉

随机漫步

随机游走(random walk)也称随机漫步,随机行走等是指基于过去的表现,无法预测将来的发展步骤和方向。核心概念是指任何无规则行走者所带的守恒量都各自对应着一个扩散运输定律 ,接近于布朗运动,是布朗运动理想的数学状态,现阶段主要应用于互联网链接分析及金融股票市场中。(来自百度百科)

import matplotlib.pyplot as plt

from random import choice

class RandomWalk:

def __init__(self, num_points=5000):

self.num_points = num_points

# 从(0, 0)开始出发

self.x_values = [0]

self.y_values = [0]

def start_walk(self):

while len(self.x_values) <self.num_points:

x_direction = choice([-1, 1])

x_distance = choice([0, 1, 2])

x_walk = x_direction * x_distance

y_direction = choice([1, -1])

y_distance = choice([0, 10, 20])

y_walk = y_direction * y_distance

# 拒绝原地不动

if x_walk == 0 and y_walk == 0:

continue

# 计算下一个点的x和y值

next_x = self.x_values[-1] + x_walk # 从x_values的倒数第一个开始加上x方向走的距离

next_y = self.y_values[-1] + y_walk # 从y_values的倒数第一个开始加上y方向走的距离

self.x_values.append(next_x)

self.y_values.append(next_y)

randomwalk = RandomWalk()

randomwalk.start_walk()

plt.scatter(randomwalk.x_values, randomwalk.y_values, s=5)

plt.show()

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

随机漫步渐变色

randomwalk = RandomWalk()

randomwalk.start_walk()

point_numbers = list(range(randomwalk.num_points))

plt.scatter(randomwalk.x_values, randomwalk.y_values, c=point_numbers, cmap=plt.cm.Reds, edgecolor='none', s=5)

plt.show()

1

2

3

4

5

6

7

1

2

3

4

5

6

7

小知识点

绘制起点和终点

randomwalk = RandomWalk()

randomwalk.start_walk()

point_numbers = list(range(randomwalk.num_points))

plt.scatter(randomwalk.x_values, randomwalk.y_values, c=point_numbers, cmap=plt.cm.Reds, edgecolor='none', s=5)

# 绘制起点和终点

plt.scatter(0, 0, c='green', edgecolors='none', s=100)

plt.scatter(randomwalk.x_values[-1], randomwalk.y_values[-1], c='red', edgecolors='none', s=100)

plt.show()

1

2

3

4

5

6

7

8

9

10

11

1

2

3

4

5

6

7

8

9

10

11

画布尺寸

plt.figure(dpi=128, figsize=(10, 6))

单位为英寸;函数figure()用于指定图表的宽度、高度、分辨率和背景色。

dpi: 传递分辨率

隐藏坐标轴

plt.axes().get_xaxis().set_visible(False)

plt.axes().get_yaxis().set_visible(False)

pygal

http://www.pygal.org/en/stable/documentation/types/index.html

roll one dice

from random import randint

import pygal

class Die:

def __init__(self, num_sides=6):

self.num_sides = num_sides

def roll(self):

return randint(1, self.num_sides)

die = Die()

results = []

# 掷100次骰子

for roll_num in range(100):

result = die.roll()

results.append(result)

frequencies = []

for value in range(1, die.num_sides + 1):

frequency = results.count(value)

frequencies.append(frequency)

horizontal_bar_chart = pygal.Bar()

horizontal_bar_chart.title = "randomly roll a 6-side die"

horizontal_bar_chart.x_labels = ['1', '2', '3', '4', '5', '6']

horizontal_bar_chart.x_title = "Result"

horizontal_bar_chart.y_title = "Frequency"

horizontal_bar_chart.add('6side', frequencies)

horizontal_bar_chart.render_to_file('die_visual.svg')

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35