ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

【小白程序圆python学习记】数据可视化

2021-01-19 23:33:29  阅读:183  来源: 互联网

标签:10 plt num python 小白 rw values 可视化 scatter


数据可视化

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]

标签:10,plt,num,python,小白,rw,values,可视化,scatter
来源: https://blog.csdn.net/xiaogongju11/article/details/112854780

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有