python數據可視化——matplotlib 用戶手冊入門:pyplot 畫圖


參考matplotlib官方指南:

https://matplotlib.org/tutorials/introductory/pyplot.html#sphx-glr-tutorials-introductory-pyplot-py

pyplot是常用的畫圖模塊,功能非常強大,下面就來見識下它的能力吧

1.快速畫出常見圖形

 

 

 2.使用關鍵字字符串作圖

 

3.使用類別變量畫圖

4.創建多圖

 

 1 import matplotlib.pyplot as plt  2 %matplotlib inline  3 plt.figure(1)                # the first figure
 4 plt.subplot(211)             # the first subplot in the first figure
 5 plt.plot([1, 2, 3])  6 plt.subplot(212)             # the second subplot in the first figure
 7 plt.plot([4, 5, 6])  8 
 9 
10 plt.figure(2)                # a second figure
11 plt.plot([4, 5, 6])          # creates a subplot(111) by default
12 
13 plt.figure(1)                # figure 1 current; subplot(212) still current
14 plt.subplot(211)             # make subplot(211) in figure1 current
15 plt.title('Easy as 1, 2, 3') # subplot 211 title

 

 1 import matplotlib.pyplot as plt  2 import numpy as np  3 
 4 np.random.seed(19680801)  5 data = np.random.randn(2, 100)  6 
 7 fig, axs = plt.subplots(2, 2, figsize=(5, 5))  8 axs[0, 0].hist(data[0])  9 axs[1, 0].scatter(data[0], data[1]) 10 axs[0, 1].plot(data[0], data[1]) 11 axs[1, 1].hist2d(data[0], data[1]) 12 
13 plt.show()

5.添加文本:軸線標簽,屬性標簽

 1 import matplotlib.pyplot as plt  2 import numpy as np  3 mu, sigma = 100, 15
 4 x = mu + sigma * np.random.randn(10000)  5 
 6 # the histogram of the data
 7 n, bins, patches = plt.hist(x, 50, normed=True, facecolor='g', alpha=0.75)  8 
 9 
10 plt.xlabel('Smarts') 11 plt.ylabel('Probability') 12 plt.title('Histogram of IQ') 13 plt.text(60, .025, r'$\mu=100,\ \sigma=15$')   # 支持 LaTex格式
14 plt.axis([40, 160, 0, 0.03]) 15 plt.grid(True) 16 plt.show()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM