数据可视化指的是通过可视化表示探索数据,它与数据挖掘紧密相关,而数据挖掘指的是用代码来探索数据集的规律和关联。
数据集可以是用一小行代码表示的小型数字列表,也可以是达到几百吉字节的数据。
1>利用matplotlib绘制各种图表。文档
示例一:
1、通过plot()函数绘制立方函数图的5个立方数的折线图:
2、plt.title()设置表的标题以及size设置对应的字体大小
3、plt.xlabel和plt.ylabel设置x轴和y轴的标识以及字体的大小
4、plt.tick_params()里面的参数设置两个坐标轴的刻度的大小
5、!!!plt.show()把图标展示出来,如果想存储图片一定要在show前调用plt.savefig("chart.jpg")
效果图:
图片.png
import matplotlib.pyplot as plt
#draw the chart of five cubic number
x_values=list(range(1,6))
y_values=[x**3 for x in x_values]
plt.plot(x_values,y_values,linewidth=5,c=(0,0,1))
plt.title("Cubic number",size=24)
plt.xlabel("Value",size=14)
plt.ylabel("Result",size=14)
plt.tick_params(axis="both",labelsize=14)
#to save the chart as jpg
plt.savefig("cubic chart",bbox_inches='tight')
plt.show()
示例二:
绘制一个含5000个点的立方彩图:
2>利用plt.scatter()绘制一系列点
1、Scatter()里面的cmap是一个颜色颜色映射,并且通过c=y_value指定了颜色的深浅随着y值越大,
蓝色表现得越深。
效果图:
图片.png
import matplotlib.pyplot as plt
#draw the chart of five cubic number
x_values=list(range(1,50001))
y_values=[x**3 for x in x_values]
plt.scatter(x_values,y_values,c=y_values,cmap=plt.cm.Blues,edgecolor='none',s=40)
plt.title("Cubic number",size=24)
plt.xlabel("Value",size=14)
plt.ylabel("Result",size=14)
plt.tick_params(axis="both",labelsize=14)
#to save the chart as jpg
plt.savefig("cubic chart",bbox_inches='tight')
plt.show()
示例三:(综合)
效果图
画出来的图描述分子运动,先写一个RandomWalk类,分子的移动距离通过函数get_step获得,分子的移动则通过函数fill_walk()实现,就是把原位置加上step后的新位置存储在列表里。最后通过列表的点把画出来实现。
import matplotlib.pyplot as plt
import random
class Randomwalk(object):
def __init__(self,num_points=5000):
self.num_points=num_points
#all data are sart at (0,0)
self.x_values=[0]
self.y_values=[0]
def get_step(self):
direction=random.choice([-2,-1,0,1,2])
distance=random.choice([0,1,2,3,4,5,6,7,8,9])
step=direction*distance
#print step
return step
def fill_walk(self,x_step,y_step):
#decide to move the distance and the direction of the movement
#refuse the step=0
#computing the next point of the distance and direction
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)
#instablished a Randomwalk object,and draw all the points it included
while True:
rw=Randomwalk(80000)
while len(rw.x_values)<rw.num_points:
x_step=rw.get_step()
y_step=rw.get_step()
rw.fill_walk(x_step, y_step)
point_numbers=list(range(rw.num_points))
#改变屏幕的尺寸
plt.figure(figsize=(10,6))
#把坐标为(0,0)的点的颜色设置为绿色,边缘颜色设置为none。size(大小)设置为100
plt.scatter(0,0,c='green',edgecolor='none',s=100)
plt.scatter(rw.x_values[-1],rw.y_values[-1],c='red',edgecolor='none',s=100)
#把折线的粗细设置为2,颜色设置为红色
plt.plot(rw.x_values,rw.y_values,linewidth=2,c='red')
#把坐标轴隐藏
plt.axes().get_xaxis().set_visible(False)
plt.axes().get_yaxis().set_visible(False)
plt.show()
answer=raw_input()
if answer=='n':
break```