Matplotlib python 基本用法

來源:互聯網
上載者:User

標籤:風格   技術   style   映像   簡單的   範圍   div   pytho   基本用法   

1.簡單的繪製函數

1 import matplotlib.pyplot as plt2 import numpy as np3 x = np.linspace(-1, 1, 50)4 y1 = x + 15 plt.plot(x, y1)6 plt.show()

   np.linspace(-1, 1, 50)

    從-1到1取50個點,繪製函數y=x+1

import matplotlib.pyplot as pltimport numpy as npx1 = np.linspace(-1, 1, 5)
x2 = np.linspace(-1, 1, 50)y1
= x1 * x1
y2 = x2 * x2plt.plot(x1, y1)
plt.plot(x2, y2)plt.show()

  換一個函數,換成y=x*x,可以更直觀的感受np.linspace()的第三個參數的作用

  np.linspace()第三個參數為5:藍色

  np.linspace()第三個參數為50:紅色

 

2.修改線條的樣式

  直接在plt.plot()中添加樣式

 

1 import matplotlib.pyplot as plt2 import numpy as np3 x1 = np.linspace(-1, 1, 5)4 x2 = np.linspace(-1, 1, 50)5 y1 = x1 * x16 y2 = x2 * x27 plt.plot(x1, y1)8 plt.plot(x2, y2, color="gray", linewidth=10, linestyle="--")9 plt.show()

 

  color:顏色

  linewidth:線寬

  linestyle:線條風格

修改後映像為

3.修改座標軸

  

 1 import matplotlib.pyplot as plt 2 import numpy as np 3 x1 = np.linspace(-1, 1, 50) 4 y1 = x1 * x1 5 plt.plot(x1, y1) 6 plt.xlim(-1, 10) 7 plt.ylim(-2, 3) 8  9 plt.xlabel("i am x")10 plt.ylabel("i am y")11 plt.show()

   plt.xlim():x軸的範圍

  plt.ylim():y軸的範圍

  plt.xlabel:x軸標註

  plt.ylabel:y軸標註

 

  修改座標軸的數值

 1 import matplotlib.pyplot as plt 2 import numpy as np 3 x1 = np.linspace(-1, 1, 50) 4 y1 = x1 * x1 5 plt.plot(x1, y1) 6 plt.xlim(-1, 2) 7 plt.ylim(-2, 3) 8  9 plt.xlabel("i am x")10 plt.ylabel("i am y")11 12 new_ticks = np.linspace(-1, 2, 5)13 plt.xticks(new_ticks)14 plt.yticks([-2, 0, 2, 3], ["very bad", "just so so", "good", "perfect"])15 plt.show()

   

  改變字型,添加特殊符號(注意轉義)就不提了

  

 

Matplotlib python 基本用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.