您的位置:首页 > 编程语言 > PHP开发

matplotlib生成数据

2020-07-14 11:11 369 查看

1 . 绘制简单的折线图

import matplotlib.pyplot as plt
squares = [1,4,9,16,25]
plt.plot(squares)
plt.show()

1.1 修改标签文字和线条粗细

import matplotlib.pyplot as plt
squares = [1,4,9,16,25]
plt.plot(squares,linewidth=5)
#设置图标标题,并且给坐标加上标签
plt.title("Square Numbers",fontsize=24)
plt.xlabel("Value",fontsize=14)
plt.ylabel("Square",fontsize=14)
#设置刻度标记的大小
#指定的实参将影响x轴和y轴的刻度
plt.tick_params(axis='both',labelsize=14)
plt.show()

1.2 校正图形
发现上述图形出现偏差,当你向plt()提供一系列数字的时候。他x轴第一个点对应y轴的第一个点

import matplotlib.pyplot as plt
input_value = [1,2,3,4,5]
squares = [1,4,9,16,25]
plt.plot(input_value,squares,linewidth=5)
plt.show()

2 . 使用scatter()绘制散点图并设置样式
使用scatter()时需要传递一队x,y坐标。它将在指定位置绘制一个点。

import matplotlib.pyplot as plt
plt.scatter(3,4)
plt.show()
import matplotlib.pyplot as plt
plt.scatter(3,4)
plt.title("Square Number",fontsize=24)
plt.xlabel("Value",fontsize=14)
plt.ylabel("Square of Value",fontsize=14)
#设置刻度标记大小
plt.tick_params("both",labelsize=14)
plt.show()

绘制一系列点
import matplotlib.pyplot as plt
x_values=[1,2,3,4,5]
y_values=[1,4,9,16,25]
plt.scatter(x_values,y_values,s=10)#s为点的尺寸

2.1 自动计算数据

import matplotlib.pyplot as plt
x_value=list(range(1,1001))
y_value=[x**2 for x in x_value ]
plt.scatter(x_value,y_value,s=1)
#设置每个坐标轴的取值范围
plt.axis([0,1001 ,0,110000])
plt.show()

2.2 删除数据点的轮廓

#绘制散点图时默认是黑色边框蓝色点,该方法去除黑色边框
plt.scatter(x_values,y_values,s=10,edgecolor='none')

2.3 自定义颜色

#要修改数据点的颜色,可以向scatter()传递参数c,并将其设置为要使用的颜色名称
plt.scatter(x_values,y_values,s=10,c='red',edgecolor='none')

2.4 自动保存图标

plt.savefig("square_plot.png",bbox_inches='tight')
#第一个参数是图表的名称,第二个参数是去掉图表多余的空白部分

3 . 随机漫步
3.1创建一个RandomWalk类

from random import choice
import matplotlib.pyplot as plt
class RandomWalk():
'''一个生成随机漫步数据的表'''
def __init__(self,num_points=5000):
self.num_points=num_points

#所有随机数漫步都始于(0,0)
self.x_value=[0]
self.y_value=[0]
def fill_walk(self):
'''计算随机漫步包含的所有点'''

#不断漫步,知道列表达到指定的长度
while len(self.x_value)<self.num_points:
x_direction = choice([-1,1])
x_distance = choice([0,1,2,3,4])
x_step=x_distance*x_direction

y_direction = choice([-1, 1])
y_distance = choice([0, 1, 2, 3, 4])
y_step = y_distance * y_direction

#拒绝原地踏步
if self.x_value==0 and self.y_value==0:
continue
next_x=self.x_value[-1]+x_step
next_y=self.y_value[-1]+y_step

self.x_value.append(next_x)
self.y_value.append(next_y)

rw = RandomWalk()
rw.fill_walk()
plt.scatter(rw.x_value,rw.y_value,s=10)
#隐藏图表中的坐标轴
plt.axes().get_xaxis().set_Visible(False)
plt.axes().get_yaxis().set_Visible(False)
#增加点数,给RandowWalk附一个参数
rw = RandomWalk(50000)
#按照点的顺序进行上色
point_numbers = list(range(rw.num_points))
plt.scatter(rw.x_value,rw.y_value,s=10,c=point_numbers)
plt.show()
#调整尺寸适应屏幕
plt.figure(dpi=123,figsize=(10,6))
#dpi指的是像素 需要给figsize指定一个元组来存放宽高;

practice
模拟分子运动

from random import choice
import matplotlib.pyplot as plt
class RandomWalk():
'''一个生成随机漫步数据的表'''
def __init__(self,num_points=5000):
self.num_points=num_points

#所有随机数漫步都始于(0,0)
self.x_value=[0]
self.y_value=[0]
def fill_walk(self):
'''计算随机漫步包含的所有点'''

#不断漫步,知道列表达到指定的长度
while len(self.x_value)<self.num_points:
x_direction = choice([-1,1])
x_distance = choice([0,1,2,3,4])
x_step=x_distance*x_direction

y_direction = choice([-1, 1])
y_distance = choice([0, 1, 2, 3, 4])
y_step = y_distance * y_direction

if self.x_value==0 and self.y_value==0:
continue
next_x=self.x_value[-1]+x_step
next_y=self.y_value[-1]+y_step

self.x_value.append(next_x)
self.y_value.append(next_y)

rw = RandomWalk()
rw.fill_walk()
plt.plot(rw.x_value,rw.y_value)
plt.savefig("WaterPath.png")
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: