绘制折线图:
import matplotlib.pyplot as plt
import math
def pingfang(i):
return i ** 2
squares = list(map(pingfang, [1, 2, 3, 4, 5]))
input_values = list(range(1,6))
plt.plot(input_values,squares, linewidth=5)
plt.title("square number", fontsize=24)
plt.xlabel('value', fontsize=14)
plt.ylabel('square of value ', fontsize=14)
plt.tick_params(axis='both',labelsize = 14)
plt.show()
这里我是用了MAP函数,主要的意义在于我可以批量的进行数字乘方,然后返回一个列表,最终生成折线图。
散点图:
class RandomWalk():
def __init__(self, num_point=5000):
self.num_point = num_point
self.x_value = [0]
self.y_value = [0]
self.range1 = [1, -1]
self.range2 = list(range(1, 5))
def fill_walk(self):
while len(self.x_value) < self.num_point:
x_direction = choice(self.range1)
x_distance = choice(self.range2)
x_step = x_direction * x_distance
y_direction = choice(self.range1)
y_distance = choice(self.range2)
y_step = y_direction * y_distance
if x_step == 0 or y_step == 0:
continue
# self.x_value[-1]表示等于倒数的第一个数,意思就是每添加的一个数进行数字相加
next_x = self.x_value[-1] + x_step
next_y = self.y_value[-1] + y_step
self.y_value.append(next_y)
self.x_value.append(next_x)
num = RandomWalk()
num.fill_walk()
C = 0
print(num.x_value)
plt.scatter(num.x_value, num.y_value, s=15)
plt.show()
在做散点图随机漫步的时候我在想可不可统计出现数字的次数,于是进行了以下代码:
#将其进行数据的筛选,
delete1 = {}
for i in num.x_value:
if num.x_value.count(i) > 1:
delete1[i] = num.x_value.count(i)
#key = lambda x:x[0]的0意义在于,如果列表= 0,返回的是Key值排序,如果为1返回的是值的排序。
#sorted的意义在于返回元组
d_order=sorted(delete1.items(),key=lambda x:x[0],reverse=True)
>>>[(-297, 45), (-279, 42), (-250, 41), (-252, 38), (-246, 37), (-253, 37), (-261, 37), (-243, 36), (-242, 35), (-263, 34), (-248, 33), (-251, 33), (-254, 33), (-258, 33), (-275, 33), (-33, 32), (-215, 32)
于是我进行了根据value的排序,我对x轴进行重复出现最多的值进行从大到小排。这对于后续的数据排序有很大的意义,例如我可以统计重复值较大的数。
以上方法来自于https://asialee.blog.csdn.net/article/details/79675802?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.control。感谢博主。