python數據可視化----繪製各類圖形

1.環境python

系統:windows10git

python版本:python3.6.1github

使用的庫:matplotlib,numpywindows

2.numpy庫產生隨機數幾種方法dom

import numpy as np
numpy.random
rand(d0, d1, ..., dn)

In [2]: x=np.random.rand(2,5)函數

In [3]: x
Out[3]:
array([[ 0.84286554,  0.50007593,  0.66500549,  0.97387807,  0.03993009],
       [ 0.46391661,  0.50717355,  0.21527461,  0.92692517,  0.2567891 ]])spa

randn(d0, d1, ..., dn)查詢結果爲標準正態分佈.net

In [4]: x=np.random.randn(2,5)code

In [5]: x
Out[5]:
array([[-0.77195196,  0.26651203, -0.35045793, -0.0210377 ,  0.89749635],
       [-0.20229338,  1.44852833, -0.10858996, -1.65034606, -0.39793635]])orm

randint(low,high,size)

生成low到high之間(半開區間 [low, high)),size個數據

In [6]: x=np.random.randint(1,8,4)

In [7]: x
Out[7]: array([4, 4, 2, 7])

random_integers(low,high,size)

生成low到high之間(閉區間 [low, high)),size個數據

In [10]: x=np.random.random_integers(2,10,5)

In [11]: x
Out[11]: array([7, 4, 5, 4, 2])

3.散點圖

x x軸
y y軸
s   圓點面積
c   顏色
marker  圓點形狀
alpha   圓點透明度                #其餘圖也相似這種配置
N=50
# height=np.random.randint(150,180,20)
# weight=np.random.randint(80,150,20)
x=np.random.randn(N)
y=np.random.randn(N)
plt.scatter(x,y,s=50,c='r',marker='o',alpha=0.5)
plt.show()

4.折線圖

x=np.linspace(-10000,10000,100) #將-10到10等區間分紅100份
y=x**2+x**3+x**7
plt.plot(x,y)
plt.show()

折線圖使用plot函數

5.條形圖

N=5
y=[20,10,30,25,15]
y1=np.random.randint(10,50,5)
x=np.random.randint(10,1000,N)
index=np.arange(N)
plt.bar(left=index,height=y,color='red',width=0.3)
plt.bar(left=index+0.3,height=y1,color='black',width=0.3)
plt.show()

orientation設置橫向條形圖

N=5
y=[20,10,30,25,15]
y1=np.random.randint(10,50,5)
x=np.random.randint(10,1000,N)
index=np.arange(N)
# plt.bar(left=index,height=y,color='red',width=0.3)
# plt.bar(left=index+0.3,height=y1,color='black',width=0.3)
#plt.barh() 加了h就是橫向的條形圖,不用設置orientation
plt.bar(left=0,bottom=index,width=y,color='red',height=0.5,orientation='horizontal')
plt.show()

6.直方圖

m1=100
sigma=20
x=m1+sigma*np.random.randn(2000)
plt.hist(x,bins=50,color="green",normed=True)
plt.show()

# #雙變量的直方圖
# #顏色越深頻率越高
# #研究雙變量的聯合分佈
#雙變量的直方圖
#顏色越深頻率越高
#研究雙變量的聯合分佈
x=np.random.rand(1000)+2
y=np.random.rand(1000)+3
plt.hist2d(x,y,bins=40)
plt.show()

7.餅狀圖

#設置x,y軸比例爲1:1,從而達到一個正的圓
#labels標籤參數,x是對應的數據列表,autopct顯示每個區域佔的比例,explode突出顯示某一塊,shadow陰影
labes=['A','B','C','D']
fracs=[15,30,45,10]
explode=[0,0.1,0.05,0]
#設置x,y軸比例爲1:1,從而達到一個正的圓
plt.axes(aspect=1)
#labels標籤參數,x是對應的數據列表,autopct顯示每個區域佔的比例,explode突出顯示某一塊,shadow陰影
plt.pie(x=fracs,labels=labes,autopct="%.0f%%",explode=explode,shadow=True)
plt.show()

8.箱型圖

import matplotlib.pyplot as plt
import numpy as np
data=np.random.normal(loc=0,scale=1,size=1000)
#sym 點的形狀,whis虛線的長度
plt.boxplot(data,sym="o",whis=1.5)
plt.show()
#sym 點的形狀,whis虛線的長度

 

github地址:https://github.com/nanxung/python-.git

碼雲地址:https://git.oschina.net/nanxun/pythonshujukeshihuajiantu.git

相關文章
相關標籤/搜索