【小白程序圆python学习记】数据可视化
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了【小白程序圆python学习记】数据可视化,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4164字,纯文字阅读大概需要6分钟。
内容图文
![【小白程序圆python学习记】数据可视化](/upload/InfoBanner/zyjiaocheng/610/1c5bebc489c0454592391b0bc40111c0.jpg)
数据可视化
import matplotlib
折线图
#创建折线图
import matplotlib.pyplot as plt
squres=[1,4,9,16,25]
plt.plot(squres)
plt.show()
#修改标签文字和线条粗细
plt.plot(squres,linewidth=5)
plt.show()
#添加图表标题,给坐标轴加标签
import matplotlib.pyplot as plt
squres=[1,4,9,16,25]
plt.plot(squres)
plt.title("Squre Numbers",fontsize=10)
plt.xlabel("Value",fontsize=10)
plt.ylabel("Squre of Value",fontsize=10)
#设置刻度标记大小
plt.tick_params(axis="both",labelsize=10)
plt.show()
#校正图形
input_values=[1,2,3,4,5]
squres=[1,4,9,16,25]
plt.plot(input_values,squres,linewidth=5)
散点图
#绘制单个散点图
plt.scatter(2,4)
plt.show()
#调整图形
plt.scatter(2,4,s=50)#调整点的大小
plt.show()
plt.scatter(2,4,s=50)
plt.title("Squre Scatter",fontsize=10)
plt.xlabel("Value",fontsize=10)
plt.ylabel("Squre of Value",fontsize=10)
plt.tick_params(axis="both",which="major",labelsize=10)
plt.show()
# 绘制多个散点图
x_values=[1,2,3,4,5]
y_values=[1,4,9,16,25]
plt.scatter(x_values,y_values,s=50)
自动计算数据
x_values=list(range(1,1001))
y_values=[x**2 for x in x_values]
plt.scatter(x_values,y_values,s=10)
删除黑色轮廓
plt.scatter(x_values,y_values,edgecolor="none",s=10)
自定义颜色
plt.scatter(x_values,y_values,c="red",edgecolor="none",s=10)
#使用RGB三原色绘图
plt.scatter(x_values,y_values,c=(1,0.5,0.8),edgecolor="none",s=10)
plt.scatter(x_values,y_values,c=y_values,cmap=plt.cm.Reds,edgecolor="none",s=10)
自动保存图表
plt.savefig("squres_plot.png",bbox_inches="tight") #以XX名字命名图片,把边缘白色剪掉
随机漫步
from random import choice
class RandomWalk():
def __init__(self,num_points=5000):
self.num_points=num_points
self.x_values=[0] #漫步始于[0,0]
self.y_values=[0]
def fill_walk(self):
while len(self.x_values)<self.num_points:
x_direction=choice([1,-1])
x_distance=choice([0,1,2,3,4])
x_step=x_direction*x_distance
y_direction=choice([1,-1])
y_distance=choice([0,1,2,3,4])
y_step=y_direction*y_distance
if x_step==0 and y_step==0:
continue
next_x=self.x_values[-1]+x_step
next_y=self.y_values[-1]+y_step
self.x_values.append(next_x)
self.y_values.append(next_y)
import matplotlib.pyplot as plt
rw=RandomWalk()
rw.fill_walk()
plt.scatter(rw.x_values,rw.y_values,s=15)
模拟多次随机漫步
while True:
rw=RandomWalk()
rw.fill_walk()
plt.scatter(rw.x_values,rw.y_values,s=15)
plt.show()
keep_running=input("Make another walk?(y/n):")
if keep_running=="n":
break
Make another walk?(y/n):n
设置随机漫步图样式
#着色
while True:
rw=RandomWalk()
rw.fill_walk()
point_numbers=list(range(rw.num_points))
plt.scatter(rw.x_values,rw.y_values,c=point_numbers,cmap=plt.cm.Reds,edgecolor="none",s=10)
plt.show()
keep_running=input("Make another walk?(y/n):")
## 重新绘制起点和终点
while True:
plt.scatter(rw.x_values,rw.y_values,c=point_numbers,cmap=plt.cm.Blues,edgecolor="none",s=10)
plt.scatter(0,0,c="green",edgecolors="none",s=50)
plt.scatter(rw.x_values[-1],rw.y_values[-1],c="red",edgecolors="none",s=50)
keep_running=input("Make another walk?(y/n):")
## 隐藏坐标轴
while True:
plt.scatter(rw.x_values[-1],rw.y_values[-1],c="red",edgecolors="none",s=50)
plt.axes().get_xaxis().set_visible(False)
plt.axes().get_yaxis().set_visible(False)
plt.show()
## 增加点数
rw=RandomWalk(5000)
rw.fill_walk()
point_numbers=list(range(rw.num_points))
plt.scatter(rw.x_values,rw.y_values,c=point_numbers,cmap=plt.cm.Blues,s=10)
plt.show()
##调整尺寸以适应屏幕
rw=RandomWalk()
rw.fill_walk()
plt.figure(figsize=(10,6))
plt.show()
使用pygal模拟掷骰子
from random import randint
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=[]
for roll_num in range(1,100):
result=die.roll()
results.append(result)
print(results)
[6, 6, 2, 5, 2, 3, 4, 1, 6, 3, 4, 1, 6, 1, 3, 5, 5, 3, 4, 6, 4, 2, 5, 5, 4, 4, 5, 4, 2, 2, 4, 4, 1, 2, 4, 3, 4, 5, 2, 5, 4, 2, 5, 3, 5, 6, 2, 6, 5, 2, 1, 4, 2, 1, 2, 4, 4, 5, 6, 5, 3, 6, 3, 3, 5, 3, 6, 4, 4, 3, 6, 2, 6, 2, 6, 6, 6, 2, 6, 4, 5, 4, 3, 2, 5, 5, 2, 6, 6, 5, 6, 2, 6, 1, 2, 2, 1, 1, 2]
##计算每个点出现的次数
result=[]
for roll_num in range(1000):
result=die.roll()
results.append(result)
frequencies=[]
for value in range(1,die.num_sides+1):
frequency=results.count(value)
frequencies.append(frequency)
print(frequencies)
[172, 184, 195, 207, 153, 188]
内容总结
以上是互联网集市为您收集整理的【小白程序圆python学习记】数据可视化全部内容,希望文章能够帮你解决【小白程序圆python学习记】数据可视化所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。