1. 程式人生 > >生成資料作業習題

生成資料作業習題

15-1:數字的三次方稱其立方,繪製一個圖形,顯示前5個整數的立方,在繪製一個顯示前5000個整數的立方值

#-*-coding:GBK-*-
#-*-coding:utf-8-*-
import matplotlib.pyplot as plt

x=[1,2,3,4,5]
y=[1,8,27,64,125]

plt.scatter(x,y,s=5)
plt.title('cube numbers',fontsize=24)
plt.xlabel("value",fontsize=10)
plt.ylabel("cube",fontsize=10)
plt.tick_params(axis='both',which='major',labelsize=14)
plt.show()

#-*-coding:GBK-*-
#-*-coding:utf-8-*-
import matplotlib.pyplot as plt

x=list(range(1,5001))
y=[dd**3 for dd in x]

plt.scatter(x,y,s=5)
plt.title('cube numbers',fontsize=24)
plt.xlabel("value",fontsize=10)
plt.ylabel("cube",fontsize=10)
plt.tick_params(axis='both',which='major',labelsize=14)
plt.show()

 

15-2:彩色立方:新增顏色對映

新增綠色:

#-*-coding:GBK-*-
#-*-coding:utf-8-*-
import matplotlib.pyplot as plt

x=list(range(1,5001))
y=[dd**3 for dd in x]

plt.scatter(x,y,c=y,cmap=plt.cm.Greens,s=5)
plt.title('cube numbers',fontsize=24)
plt.xlabel("value",fontsize=10)
plt.ylabel("cube",fontsize=10)
plt.tick_params(axis='both',which='major',labelsize=14)
plt.show()

 

15-3:分子運動,將散點換為線條圖

#-*-coding:GBK-*-
#-*-coding:utf-8-*-
#隨機漫步類
from random import choice
class RandomWalk():
    def __init__(self,num_points=5000):
        self.num_points=num_points
#所有的隨機漫步始於(0,0)
        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,5])
            x_step=x_direction*x_distance

            y_direction=choice([1,-1])
            y_distance=choice([0,1,2,3,4,5])
            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)
#-*-coding:GBK-*-
#-*-coding:utf-8-*-
import matplotlib.pyplot as plt
from randomwalk import RandomWalk
while True:
    rw=RandomWalk()
    rw.fill_walk()
    #調整螢幕大小
    plt.figure(figsize=(10,6))
    point_numbers=list(range(rw.num_points))
    plt.plot(rw.x_values,rw.y_values,linewidth=1)
   
     #隱藏座標軸
    plt.axes().get_xaxis().set_visible(False)
    plt.axes().get_yaxis().set_visible(False)
    plt.show()

    keep_running=input("make another walk?(y/n):")
    if keep_running=='n':
        break

 

15-6 自動生成標籤:修改die.py/dice——visual.py將設定hist.x_labels值餓列表替換成自動生成的這種列表的迴圈,

 

15-7:兩個D8的骰子

#-*-coding:GBK-*-
#-*-coding:utf-8-*-
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)
#-*-coding:GBK-*-
#-*-coding:utf-8-*-

from die import Die
import pygal
die_1=Die(8)
die_2=Die(8)
results=[]
for roll_num in range(1000):
	result=die_1.roll()+die_2.roll()
	results.append(result)
print(results)
#分析結果
frequencies=[]
max_result=die_1.num_sides+die_2.num_sides
for value in range(2,max_result+1):
	frequency=results.count(value)
	frequencies.append(frequency)
print(frequencies)
#對結果進行視覺化處理
hist=pygal.Bar()
hist.title='Results of rolling D8+D8 1000 times.'
hist.x_labels=[str(dd) for dd in range(2,max_result+1)]
hist.x_title="Results"
hist.y_title="frequencies of results"
hist.add('D8+D8',frequencies)
hist.render_to_file('dice_visual.svg')

結果: 

 

15-8:同時扔三個D6骰子,視覺化處理結果資料

#-*-coding:GBK-*-
#-*-coding:utf-8-*-

#-*-coding:GBK-*-
#-*-coding:utf-8-*-
from die import Die
import pygal
die_1=Die()
die_2=Die()
die_3=Die()
results=[]
for roll_num in range(1000):
	result=die_1.roll()+die_2.roll()+die_3.roll()
	results.append(result)

#分析結果
frequencies=[]
max_result=die_1.num_sides+die_2.num_sides+die_3.num_sides
for value in range(3,max_result+1):
	frequency=results.count(value)
	frequencies.append(frequency)

#對結果進行視覺化處理
hist=pygal.Bar()
hist.title='Results of rolling D6+D6+D6 1000 times.'
hist.x_labels=[str(dd) for dd in range(3,max_result+1)]
hist.x_title="Results"
hist.y_title="frequencies of results"
hist.add('D6+D6+D6',frequencies)
hist.render_to_file('dice_visual.svg')

 

15-9:將點數相乘,視覺化處理結果

#-*-coding:GBK-*-
#-*-coding:utf-8-*-
from die import Die
import pygal
die_1=Die()
die_2=Die()
results=[]
for roll_num in range(1000):
	result=die_1.roll()*die_2.roll()
	results.append(result)

#分析結果
frequencies=[]
max_result=die_1.num_sides*die_2.num_sides
for value in range(1,max_result+1):
	frequency=results.count(value)
	frequencies.append(frequency)

#對結果進行視覺化處理
hist=pygal.Bar()
hist.title='Results of rolling D6*D6 1000 times.'
hist.x_labels=[str(dd) for dd in range(1,max_result+1)]
hist.x_title="Results"
hist.y_title="frequencies of results"
hist.add('D6*D6',frequencies)
hist.render_to_file('dice_visual.svg')