1. 程式人生 > 實用技巧 >第十五章 生成資料

第十五章 生成資料

------------恢復內容開始------------

1.繪製簡單的折線圖,效果圖如下

import matplotlib.pyplot as plt
#使用平方資料來繪製表格
input_values=[1,2,3,4,5]
squars=[1,4,9,6,25]
#修改標籤和文字線條粗細,linewidth 表示粗細
plt.plot(input_values,squars,linewidth=5)

#設定圖示的標題,並且給座標軸加上標籤
plt.title("Title",fontSize=20)
plt.xlabel("X title",fontSize=16)
plt.ylabel(
"Y title",fontSize=15) #設定刻度標記的大小 plt.tick_params(axis='both',labelsize=14) plt.show()

2.繪製簡單的,效果圖如下

import matplotlib.pyplot as plt
# 使用Scatter 繪製散點圖,自動計算座標軸的範圍
x_values=list(range(1,1001))
y_values=[x**2 for x in x_values]

# 刪除資料點的輪廓,c 表示顏色,c將較淺的顏色顯示較小的值,顏色對映
plt.scatter(x_values,y_values,c=y_values,cmap=plt.cm.Blues,edgecolor='
none',s=40) plt.xlabel("x title",fontSize=16) plt.ylabel("y title",fontSize=15) #設定刻度標記的大小 plt.tick_params(axis='both',labelsize=14) # 設定座標軸的取值範圍 plt.axis([0,1100,0,1100000]) plt.show() #儲存圖片 plt.savefig("D:\\2.png",bbox_inches='tight') #第一個引數儲存的是位置,第二個是空白區域去掉

3.繪製漫步圖

# 漫步資料的類
import  matplotlib.pyplot as plt
from random import choice class Randomwalk(): #一個生成隨機漫步資料的類 def __init__(self,num_points=5000): #初始化隨機漫步的屬性 self.num_points=num_points #所有漫步都從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]) 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) #只要程式處於活動狀態就模擬 while True: rw=Randomwalk() rw.fill_walk() #生成一個數字列表,包含數字個數與漫步包含的點數相同 point_numbers=list(range(rw.num_points)) #給點著色 plt.scatter(rw.x_values,rw.y_values,c=point_numbers,cmap=plt.cm.Blues,edgecolors='none',s=15) plt.show() keep_running=input("Make another walk?(y/n):") if keep_running=='n': break

4.漫步方法同上,隱藏座標軸

# 漫步資料的類
import  matplotlib.pyplot as plt
from random import choice
class Randomwalk():
    def __init__(self,num_points=5000):
        self.num_points=num_points
        #所有漫步都從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])
            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)

#只要程式處於活動狀態就模擬
while True:
 rw=Randomwalk(50000)
 rw.fill_walk()
 point_numbers=list(range(rw.num_points))
#突出起點和終點
 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)
 plt.scatter(rw.x_values, rw.y_values, c=point_numbers, cmap=plt.cm.Blues,edgecolors='none', s=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

------------恢復內容結束------------