目录
第二部分 项目-数据可视化
第十五章 生成数据
数据分析
概念:~~~巴拉巴拉,自己网上查询吧,什么可视化报表,优劣势分析,前瞻性窥探,高性能之类的。
测试文件下载数据的地址:https://codechina.csdn.net/weixin_45847167/python3-learn/-/tree/master/csv
安装插件matplotlib
1.安装matplotlib
import matplotlib 没有抱错就证明安装成功了。
测试下插件
import matplotlib.pyplot as plt
squares = [1, 4, 9, 16, 25]
plt.plot(squares)
plt.show()
曲线图
丰富下其他参数:https://blog.csdn.net/weixin_42468475/article/details/105957702 (看起来不就是折线图吗。。。)
import matplotlib.pyplot as plt
squares = [1, 4, 9, 16, 25]
plt.plot(squares,linewidth=5) #具体参数有哪些得找下文档,看起来有点像java-applet或者awt那个东西
plt.title("Square numbers",fontsize=24)
plt.xlabel("value",fontsize=15)
plt.ylabel("Square of value",fontsize=15)
plt.tick_params(axis="both",labelsize=15)
plt.show()
执行效果:
视乎就是横轴多了标尺而已。
https://blog.csdn.net/daoxu_hjl/article/details/103690761
散点图
import matplotlib.pyplot as plt
y_values = [1, 4, 9, 16, 25]
x_values=[1,2,3,4,5]
#绘制的结果就是 (1,1) (4,2) (9,3)(16,4)(25,5)
plt.scatter(x_values,y_values,s=100)
plt.title("Square numbers",fontsize=25) #设置图标-横标题
plt.xlabel("value",fontsize=11) #x方向标签
plt.ylabel("Square of value",fontsize=11) #y方向的标签
plt.tick_params(axis="both",which='major',labelsize=12) # 标尺大小?
plt.show()
执行结果
自动计算点,y=x^2
import matplotlib.pyplot as plt
x_values=list(range(1,1001))
y_values = [x**2 for x in x_values]
# y轴自动计算根据x的值得平方向上滑行
plt.axis([0,1100,0,1100000])
plt.scatter(x_values,y_values,s=5)
plt.title("Square numbers",fontsize=25) #设置图标-横标题
plt.xlabel("value",fontsize=11) #x方向标签
plt.ylabel("Square of value",fontsize=11) #y方向的标签
plt.tick_params(axis="both",which='major',labelsize=12)
plt.show()
执行效果:
线条渐变颜色
import matplotlib.pyplot as plt
x_values=list(range(1,1001))
y_values = [x**2 for x in x_values]
# y轴自动计算根据x的值得平方向上滑行
plt.axis([0,1100,0,1100000])
#s调线粗细,edgecolor线的颜色,c是顶端的颜色,也支持 RGB(三色配置),和颜色渐变(映射)
# plt.scatter(x_values,y_values,s=20,edgecolor='blue',c='red')
# plt.scatter(x_values,y_values,s=20,edgecolor='none',c=(0,0,0.8))
plt.scatter(x_values,y_values,s=20,edgecolor='none',c=y_values,cmap=plt.cm.Blues)
plt.title("Square numbers",fontsize=25) #设置图标-横标题
plt.xlabel("value",fontsize=11) #x方向标签
plt.ylabel("Square of value",fontsize=11) #y方向的标签
plt.tick_params(axis="both",which='major',labelsize=12)
plt.show() #显示到图形
# plt.savefig('abc.png',bbox_inches='tight') #保存到指定文件,第一个参数文件路径,第二个参数去除多余空白
执行效果:
随机漫步 random_walk.py
import matplotlib.pyplot as plt
from random import choice
class RandomWalk():
"""docstring for RandomWalk"""
def __init__(self, num_points=500):
self.num_points = num_points
self.x_values = [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
# 计算下一个点的x,y值
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)
# 随机生成一堆的x,y数据,通过scatter组件呈现散点图
rw = RandomWalk()
rw.fill_walk()
print('x:')
print(rw.x_values)
print('y:')
print(rw.y_values)
plt.scatter(rw.x_values, rw.y_values, s=15)
plt.show()
效果:
多个随机
import matplotlib.pyplot as plt
from random import choice
class RandomWalk():
"""docstring for RandomWalk"""
def __init__(self, num_points=500):
self.num_points = num_points
self.x_values = [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
# 计算下一个点的x,y值
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)
# 随机生成一堆的x,y数据,通过scatter组件呈现散点图
rw = RandomWalk()
rw.fill_walk()
# print('x:')
# print(rw.x_values)
# print('y:')
# print(rw.y_values)
plt.scatter(rw.x_values, rw.y_values, s=15)
#多个随机漫步图示
rw2 = RandomWalk()
rw2.fill_walk()
plt.scatter(rw2.x_values, rw2.y_values, s=15)
plt.show()
执行效果:
生成多个散点图,muti_random_walk.py
# -*- coding: utf-8 -*
import matplotlib.pyplot as plt
from random_walk import RandomWalk
for x in list(range(0, 5)):
rw = RandomWalk(500)
rw.fill_walk()
plt.scatter(rw.x_values, rw.y_values, s=15)
# 突出起点和终点
plt.scatter(0, 0, c='green', edgecolors='none', s=100)
plt.scatter(rw.x_values[-1], rw.y_values[-1], c='red', edgecolors='none',
s=100)
# yOrN = input('let’s continue? y/n') #这个编辑器有点小问题,懒得写这个测试了。
# if yOrN.lower() == 'y':
# continue
# else:
# break
# 隐藏坐标轴,没好使。。。。
# plt.axes().get_xaxis().set_visible(False)
# plt.axes().get_yaxis().set_visible(False)
# 调整灰图的尺寸,视乎没看出啥效果
# plt.figure(dpi=128, figsize=(10, 6))
plt.show()
# print('the end!')
安装插件:pygal
ruishens-MacBook-Pro:python_work ruishen$ pip3 install --user pygal
Collecting pygal
Downloading pygal-2.4.0-py2.py3-none-any.whl (127 kB)
|████████████████████████████████| 127 kB 463 kB/s
Installing collected packages: pygal
Successfully installed pygal-2.4.0
柱状图
编写文件:die.py文件
from random import randint
class Die():
"""docstring for Die"""
def __init__(self, num_sides=6):
self.num_sides = num_sides
def roll(self):
return randint(1,self.num_sides)
die_visual.py文件
from die import Die
import pygal
die = Die()
results = []
for x in range(500):
results.append(die.roll())
print(results)
freqences = []
for value in range(1, die.num_sides+1):
freqences.append(results.count(value))
print(freqences)
#报表参数信息
hist = pygal.Bar()
hist.title = "Results of rolling one D6 1000 times."
hist.x_labels = ['1', '2', '3', '4', '5']
hist.title = 'Result'
hist.y_labels = 'Frequency of Result'
hist.add('D6', freqences)
hist.render_to_file('die_visual.svg')
生成报表查看: pygal.Bar() 生成柱状图
今天简单的试了一下用idea 来配置https://blog.csdn.net/qq_38188725/article/details/80623710
比sublime好多了,因为可以随便点进去看有那么调用方法之类的,方便不少。不知道sublime是不是也有