1. 程式人生 > >python Matplotlib繪圖 基礎筆記

python Matplotlib繪圖 基礎筆記

                                      python Matplotlib 基礎筆記

目錄

       1、可查備忘之程式碼筆記

        2、效果


   1、可查備忘之程式碼筆記

# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
from scipy.misc import imread, imsave, imresize

#Matplotlib是一個作相簿。這裡簡要介紹matplotlib.pyplot模組,功能和MATLAB的作圖功能類似。

x=np.linspace(0,20,16)[:,np.newaxis]
noise=np.random.normal(0,0.1,size=x.shape)
y1=np.power(x,2)+noise
y2=np.cos(x)

#1、折線圖和散點圖
#1.1 初步體驗
#折線圖
#plt.plot(x,y1,color='red',linewidth=1.0,linestyle='--')
plt.plot(x,y1)
plt.show()

#散點圖
#plt.scatter(x,y2,marker='o',  s = 20,c="green")
plt.scatter(x,y2,marker='o',  s = 20,c="red")
plt.show()

# 1.2 同一張圖
plt.plot(x,y1,color='red')
plt.plot(x,y2,color='blue')
plt.show()

# 1.3 多個圖在一個列表中
plt.subplot(1,2,1)   #行  列  序號
plt.plot(x,y1)
plt.subplot(1,2,2)
plt.scatter(x,y2,marker='o',  s = 10,c="black")
plt.show()

# 1.4 圖的軸標籤,圖例,圖示題
plt.plot(x,y1,c='red')
plt.plot(x,y2,c='blue')
plt.xlabel('x axis label')         #x座標標籤
plt.ylabel('y axis label')         #y座標標籤
plt.title('squre and sin')        #標題
plt.legend(['squre', 'sine'])   #圖例
plt.show()


#1.5 畫布的使用
#1.5.1 體驗
fig=plt.figure()
p1 = fig.add_subplot(211)
p2=  fig.add_subplot(212)
p1.plot(x,y1,c='red')
p2.scatter(x,y2)
plt.show()

#1.5.2  完整引數 設定畫布大小

#設定座標軸範圍
#plt.xlim((-1,0.5))
#plt.ylim((-1,0.5))
plt.figure(num=1,figsize=(8,5))
plt.plot(x,y1)
plt.figure(num=2,figsize=(6,4))
plt.plot(x,y2)
plt.show()

# 1.6 顯示影象
img=imread('E:/TEST_IMG/1.jpg')
plt.imshow(img)
plt.show()

# 2、 3D繪圖
#............................待續

 

    2、效果