本文實例為大家分享了python使用matplotlib畫柱狀圖、散點圖的具體代碼,供大家參考,具體內容如下
柱狀圖(plt.bar)
代碼與注釋
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
import numpy as np from matplotlib import pyplot as plt plt.figure(figsize = ( 9 , 6 )) n = 8 x = np.arange(n) + 1 #x是1,2,3,4,5,6,7,8,柱的個數 # numpy.random.uniform(low=0.0, high=1.0, size=none), normal #uniform均勻分布的隨機數,normal是正態分布的隨機數,0.5-1均勻分布的數,一共有n個 y1 = np.random.uniform( 0.5 , 1.0 ,n) y2 = np.random.uniform( 0.5 , 1.0 ,n) plt.bar(x,y1,width = 0.35 ,facecolor = 'lightskyblue' ,edgecolor = 'white' ) #width:柱的寬度 plt.bar(x + 0.35 ,y2,width = 0.35 ,facecolor = 'yellowgreen' ,edgecolor = 'white' ) #水平柱狀圖plt.barh,屬性中寬度width變成了高度height #打兩組數據時用+ #facecolor柱狀圖里填充的顏色 #edgecolor是邊框的顏色 #想把一組數據打到下邊,在數據前使用負號 #plt.bar(x, -y2, width=width, facecolor='#ff9999', edgecolor='white') #給圖加text for x,y in zip (x,y1): plt.text(x + 0.3 , y + 0.05 , '%.2f' % y, ha = 'center' , va = 'bottom' ) for x,y in zip (x,y2): plt.text(x + 0.6 , y + 0.05 , '%.2f' % y, ha = 'center' , va = 'bottom' ) plt.ylim( 0 , + 1.25 ) plt.show() |
結果
散點圖(plt.scatter)
代碼與注釋
1
2
3
4
5
6
7
8
9
10
11
|
plt.figure(figsize = ( 9 , 6 )) n = 1000 #rand 均勻分布和 randn高斯分布 x = np.random.randn( 1 ,n) y = np.random.randn( 1 ,n) t = np.arctan2(x,y) plt.scatter(x,y,c = t,s = 25 ,alpha = 0.4 ,marker = 'o' ) #t:散點的顏色 #s:散點的大小 #alpha:是透明程度 plt.show() |
結果
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/zhuiqiuk/article/details/70943535