日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網(wǎng)站首頁 編程語言 正文

Python利用隨機函數(shù)生成變化圖形詳解_python

作者:PursuitingPeak ? 更新時間: 2022-06-24 編程語言

鑒于上一篇中最后三個問題:

1、上述程序是否能進行優(yōu)化(比如功能相同的)

2、創(chuàng)建三個3個實例,用了3個語句,能否建一個函數(shù),只輸入一個數(shù)n,就自動創(chuàng)建n個實例?同時,每個實例的num_times隨機,(n比較大時,num_times應該比較小)

3、當實現(xiàn)上述功能后,程序運行,只輸入一個參數(shù)(創(chuàng)建實例的個數(shù)),就會自動生成對應的num_times,并分別調(diào)用相關(guān)函數(shù)生成對應圖表。

可以,在類Rand_moving()中計算每一步的方向和移動位置時,都用到了一個乘法公式,

x_direction = choice([1,-1])       #x的移動方向,1向上,0不變,-1向下
            x_distance = choice([0,1,2,3,4,5]) #x的每次移動的像素,
            x_step = x_direction*x_distance    #移動方向乘以移動距離,以確定沿x移動的距離
            
            y_direction =  choice([1,-1])      #y的移動方向,1向上,0不變,-1向下
            y_distance = choice([0,1,2,3,4,5]) #y的每次移動的像素,
            y_step = y_direction*y_distance    #移動方向乘以移動距離,以確定沿y移動的距離

因此可以整理出一個計算方法,可以直接調(diào)用,至于2,3很明顯,也是可以完成的。

第一步,將乘法公式提出來單獨形成一個作用于自身的方法,代碼如下:

def get_step(self,direction,distance):
          return distance*direction
        
    def fill_moving(self):
        while len(self.x_values)<self.num_times:#循環(huán)不斷運行,直到漫步包含所需數(shù)量的點num_times
            x_step = self.get_step(choice([1,-1]),choice([0,1,2,3,4,5]))     #直接調(diào)用get_step方法,使代碼更加簡潔
            y_step = self.get_step(choice([1,-1]),choice([0,1,2,3,4,5]))

完成第1問,代碼變得更加簡潔明了。

第2、3個問題:

1)要能生成實例個數(shù)n,且還有對應的移動次數(shù)num_times,可考慮用字典,即實例個數(shù)為key,對應移動次數(shù)num_times為鍵值,當輸入2時,如? caselist={’1‘:'150000',’2‘:'250000'}

2)讀取字典每個項目,將對應的key和value傳遞給由Rand_moving類創(chuàng)建的實例,key的個數(shù)即為需要創(chuàng)建實例的個數(shù),對應的value值為移動次數(shù)。

3)輸入數(shù)字,即為字典key的個數(shù),存入字典,同時利用隨機函數(shù)生成一個num_times保存到對應的value中。

完成思路:

1,重新定義一個類 New_case() 作用:接收一個數(shù)據(jù),并根據(jù)這個數(shù)據(jù)自動生成一個字典,

class New_case():
    #定義New_case類
    def __init__(self,numbers): #定義要創(chuàng)建的實例個數(shù)        
        self.numbers=numbers     
        self.caselist={}  #定義一個空的caselist字典
        self.case = 0       
       while self.case < self.numbers: #當變量case小于給定值時,
            self.case += 1
            times = choice([100000,150000,200000,250000])#隨機選擇一個移動次數(shù)
            self.caselist[self.case] = times  #將value與key對應

2、需要循環(huán)讀取字典的key和value,并將value傳遞給類Rand_moving,隨后再運行fill_moving()生成數(shù)據(jù)并保存到列表,隨即用plt.scatter()進行繪圖

for key,value in self.caselist.items(): #字典不為空
            colorkey=str(key)        #  將字典關(guān)鍵字轉(zhuǎn)為字符串存到變量colorkey中
            examplecase = Rand_moving(int(value))  #創(chuàng)建實例,將對應的value值傳遞類Rand_moving
            examplecase.fill_moving()  #調(diào)用類Rand_moving中的方法fill_moving()計算移動相關(guān)數(shù)據(jù)并保存到列表中
            plt.figure(dpi=128,figsize=(12, 10))   #創(chuàng)建畫面屏幕          
            plt.scatter(examplecase.x_values,examplecase.y_values,c=self.colors[colorkey],s=15)
        plt.show()

上篇中的代碼 ?c=y_values, cmap=plt.cm.Reds為什么這里不再用,是因為這里循環(huán)的時候一直出現(xiàn)紅色Reds,為了對比,創(chuàng)建了一新字典colors{},將生成的個數(shù)與顏色相對應。所以上述代碼中修改為 c=self.colors[colorkey]

類New_case()?全部代碼如下:

import matplotlib.pyplot as plt

from rand_moving import *

class New_case():
    #定義New_case類
    def __init__(self,numbers): #定義要創(chuàng)建的實例個數(shù)        
        self.numbers=numbers     
        self.caselist={}  #定義一個空的cases列表
        self.case = 0    #定義一個case變量   
        self.colors={'1':'red','2':'orange','3':'yellow','4':'green','5':'blue','6':'puple'}#創(chuàng)建了一新字典colors{},將生成的個數(shù)與顏色相對應
        
        while self.case < self.numbers:  #小于給定實例個數(shù)時
            self.case += 1
            times = choice([100000,150000,200000,250000]) #隨機生成一個移動次數(shù)
            self.caselist[self.case] = times    #將變量case作為key, times作為value保存到字典中      
       
    def case_moving(self):  #重新定義一個方法,即訪問字典所有項
        for key,value in self.caselist.items(): #字典不為空
            colorkey=str(key)        #  將字典關(guān)鍵字轉(zhuǎn)為字符串存到變量colorkey中
            examplecase = Rand_moving(int(value))  #創(chuàng)建實例,將對應的value值傳遞類Rand_moving
            examplecase.fill_moving()  #調(diào)用類Rand_moving中的方法fill_moving()計算移動相關(guān)數(shù)據(jù)并保存到列表中
            plt.figure(dpi=128,figsize=(12, 10))   #創(chuàng)建畫面屏幕         
            plt.scatter(examplecase.x_values,examplecase.y_values,c=self.colors[colorkey],s=15)#注意調(diào)用了上述新字典的顏色
        plt.show()

3、主程序

主程序中有一個交互,需要輸入一個數(shù)據(jù),然后調(diào)用相關(guān)相關(guān)類創(chuàng)建實例(慢慢成調(diào)包俠了!^v^)

import matplotlib.pyplot as plt

from rand_moving import *   
from new_case import *

print("Please enter the number:")  #交互,請輸入一個數(shù),模擬運行,不需要太大的數(shù)據(jù)。
n = input()   #將輸入的數(shù)據(jù)保存到變量n中,注意所有輸入均為字符串,

testcase = New_case(int(n)) #將n轉(zhuǎn)為整型數(shù)據(jù),創(chuàng)建實例個數(shù) 
testcase.case_moving()

實際運行效果,輸入4,生成4個數(shù)據(jù)圖形(為展示較全,原圖已縮小):

self.colors={'1':'red','2':'orange','3':'yellow','4':'green','5':'blue','6':'puple'}?注意:圖的顏色分別與colors字典中對應。

當然如果覺得數(shù)軸很礙眼,那就在類類New_case()?中的plt.figure()之后加上

plt.axes().get_xaxis().set_visible(False)
plt.axes().get_yaxis().set_visible(False) 

原文鏈接:https://www.cnblogs.com/codingchen/p/16192714.html

欄目分類
最近更新