1. 程式人生 > >matplotlib 樹狀圖

matplotlib 樹狀圖

import numpy as np
import matplotlib.pyplot as plt
k = 16
x = np.arange(k)#x軸的範圍
y = np.random.rand(k)#隨機生成個數作為柱狀圖的高度值
plt.bar(x,y)#畫出x 和 y的柱狀圖
for x,y in zip(x,y):#增加數值
    #在樹狀圖的頂部繪製數值居中靠底邊對齊 水平  垂直
    plt.text(x,y,'%.2f' % y,ha = 'center',va='bottom')

plt.show()

#----------------------------------------------------------------------------------------補充

a = [1,2,3]
b = [4,5,6]
for x ,y in zip(a,b):
    print(x,y)
    
1 4
2 5
3 6