python matplotlib 畫圖

python matplotlib 畫圖


author@jason_ql(lql0716)
http://blog.csdn.net/lql0716


1、代碼

# -*- coding: utf-8 -*-
"""
Created on Thu Jul 06 21:59:39 2017

@author: lql0716
"""

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(-20,20)  #自變量x
y = x**2  #變量y

plt.figure(num = 3, figsize = (8, 12), dpi = 100) #設置畫圖的圖片的大小,figsize大概爲釐米尺寸,分辨率dpi

plt.plot(x, y, 'r*', label = 'line', linewidth = 3)
plt.title('Function')  # 標題
plt.xlabel('x (cm)')  # x軸的名稱
plt.ylabel('y (cm)')  # y軸的名稱

xt = np.arange(-20,20, 10) #設置x軸刻度步長
plt.xticks(xt)
yt = np.arange(0, 400, 20) #設置y軸刻度步長
plt.yticks(yt)


plt.legend(loc = 0) #參數loc,設置label在圖中的位置,含義如下
#'best'         : 0, (only implemented for axes legends)(自適應方式)
#'upper right'  : 1,
#'upper left'   : 2,
#'lower left'   : 3,
#'lower right'  : 4,
#'right'        : 5,
#'center left'  : 6,
#'center right' : 7,
#'lower center' : 8,
#'upper center' : 9,
#'center'       : 10,

plt.grid(True)  #True表示顯示網格線,其它參數linestyle 設置線顯示的類型(一共四種), color 設置網格的顏色, linewidth 設置網格的寬度
#plt.grid(True, linestyle = "-.", color = "r", linewidth = "3")  
plt.savefig("D:/photo/1-1.jpg")  #保存繪製的圖片
plt.show()    

2、效果圖

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章