绘制折线图
折线图
import matplotlib.pyplot as plt
input_data=[1,2,3,4,5]
squares= [1,4,9,16,25]
plt.plot(input_data,squares,lineWidth=5)
plt.title('Square Numbers',fontsize=24)
plt.xlabel('Value',fontsize=14)
plt.ylabel('Square of Value',fontsize=14)
plt.tick_params(axis='both',labelsize=14)
plt.show()
点的描绘
import matplotlib.pyplot as plt
x_values=list(range(1,100))
y_values=[x**2 for x in x_values]
plt.scatter(x_values,y_values,c=y_values,cmap=plt.cm.Greens,
edgecolor='none',s=10)
plt.axis([0,100,1,11000])
#保存图片
plt.savefig('squares_plot.png',bbox_inches='tight')
plt.show()
数据漫步
random_walk.py
from random import choice
class RandomWalk():
def __init__(self,num_points=5000):
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
next_x=self.x_values[-1]+x_step
next_y=self.y_values[-1]+y_step
if x_distance==0 and y_distance==0:
continue
self.x_values.append(next_x)
self.y_values.append(next_y)
rw_visual.py
# coding=gbk
import matplotlib.pyplot as plt
from random_walk import RandomWalk
rw=RandomWalk()
rw.fill_walk()
point_numbers= list(range(rw.num_points))
# 绘制漫步图
#plt.scatter(rw.x_values,rw.y_values,s=1)
# 绘制颜色渐变的漫步图
plt.scatter(rw.x_values,rw.y_values,c=point_numbers,cmap=plt.cm.Reds,
s=1)
#突出起点和终点
plt.scatter(0,0,c='blue',edgecolor='none',s=100)
plt.scatter(rw.x_values[-1],rw.y_values[-1],c='red',edgecolor='none',
s=100)
# 隐藏坐标轴
plt.axes().get_xaxis().set_visible(False)
plt.axes().get_yaxis().set_visible(False)
#设置绘图窗口的属性(尺寸,分辨率,背景色)
plt.figure(figsize=(10,6))
plt.show()
绘制直方图
from die import Die
import pygal
die=Die()
#生成数据
results=[]
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)
#结果可视化,绘制直方图
hist=pygal.Bar()
hist.title="Results of rolling one D6 1000 times."
hist.x_labels=['1','2','3','4','5','6']
hist.x_title='Result'
hist.y_title='Frequency of Result'
hist.add('D6',frequencies)
hist.render_to_file('die_visual.svg')
最后生成的svg文件,用浏览器打开,即可浏览到