1. 程式人生 > >matplotlib實戰繪製各種圖形

matplotlib實戰繪製各種圖形

size = (m,n,3) 圖片的一般形式就是這樣的

rgb 0-255 jpg圖片 166,255,89 0.0-1.0 png圖片 0.1,0.2,0.6

灰度處理以後 rgb---->gray 166,255,89 ---> 190 0.1,0.2,0.6 -- > 0.4

size = (m,n)

import scipy.misc as misc

import numpy as np
import pandas as pd
from pandas import Series,DataFrame

import matplotlib.pyplot as plt
%matplotlib inline

face_image = misc.face()
plt.imshow(face_image)

face_gray = misc.face(gray=True)
plt.imshow(face_gray,cmap='gray')

三種方法

  • 最大值法
  • 平均值法
  • RGB權重法[0.299,0.587,0.114]

plt.imshow(face_image.max(axis=2),cmap='gray')

plt.imshow(face_image.mean(axis=2),cmap='gray')

n = np.array([0.299,0.587,0.114])
plt.imshow(np.dot(face_image,n),cmap='gray')

matplotlib

一、Matplotlib基礎知識

Matplotlib中的基本圖表包括的元素

  • x軸和y軸 axis 水平和垂直的軸線
  • x軸和y軸刻度 tick 刻度標示座標軸的分隔,包括最小刻度和最大刻度
  • x軸和y軸刻度標籤 tick label 表示特定座標軸的值
  • 繪圖區域 axes 實際繪圖的區域

只含單一曲線的圖

x = np.arange(-np.pi,np.pi,0.1)
y = np.sin(x)
plt.plot(x,y)

包含多個曲線的圖

plt.plot(x,np.sin(x),x,np.cos(x))

設定子畫布

axes = plt.subplot()

plt.figure(figsize=(8,8))
# 設定字畫布,返回的值就是當前字畫布的座標系物件
axes1 = plt.subplot(2,2,1)
axes2 = plt.subplot(222)
axes3 = plt.subplot(223)
axes4 = plt.subplot(224)

網格線

繪製正弦餘弦

設定grid引數(引數與plot函式相同),使用plt面向物件的方法,建立多個子圖顯示不同網格線

  • lw代表linewidth,線的粗細
  • alpha表示線的明暗程度
  • color代表顏色
  • axis顯示軸向
  • # 對不同的座標系分別設定網格線
    plt.figure(figsize=(16,4))
    axes1 = plt.subplot(141)
    axes2 = plt.subplot(142)
    axes3 = plt.subplot(143)
    axes4 = plt.subplot(144)

    axes1.grid(True)
    axes3.grid(True)

    # axis引數設定網格線顯示橫縱
    axes2.grid(axis='x')
    axes4.grid(axis='y')

    # 設定線寬、透明度、顏色
    # red green yellow blue black orange pink gray white purple cyan
    axes1.grid(color='red')
    axes2.grid(lw = 2)
    axes3.grid(alpha = 0.5)

  • 座標軸界限

    plt.axis([xmin,xmax,ymin,ymax])

  • plt.figure(figsize=(4,4))


    x = np.linspace(-1,1,100)
    y = (1-x**2)**0.5
    plt.plot(x,y)
    plt.axis([-4,4,-3,3])

  • 座標軸標籤

    xlabel方法和ylabel方法
    plt.ylabel('y = x^2 + 5',rotation = 60)旋轉

    • color 標籤顏色
    • fontsize 字型大小
    • rotation 旋轉角度
  • loc引數可以是2元素的元組,表示圖例左下角的座標

    • [0,0] 左下
    • [0,1] 左上
    • [1,0] 右下
    • [1,1] 右上
    In [122]:
    plt.plot(x,x,x,x*2,x,x*0.5)
    # 使用loc引數設定圖例位置
    plt.legend(['nomral','fast','slow'],loc=[-0.3,0.3])
    Out[122]:
    <matplotlib.legend.Legend at 0x1bee4870>

    圖例也可以超過圖的界限loc = (-0.1,0.9)

    ncol引數

    ncol控制圖例中有幾列,在legend中設定ncol,需要設定loc

    In [124]:
    plt.plot(x,x,x,x*2,x,x*0.5)
    # 使用nloc引數設定圖例的列數
    plt.legend(['nomral','fast','slow'],loc=9,ncol=3)
    Out[124]:
    <matplotlib.legend.Legend at 0x1bf93870>

    linestyle、color、marker

    修改線條樣式

    In [139]:
    x = np.random.randint(-20,30,size=(100,3))
    df = DataFrame(x)
    df
    . . .In [142]:
    plt.plot(df.index,df[0].cumsum(),linestyle='--',color='red',marker='o')
    plt.plot(df.index,df[1].cumsum(),linestyle='-.',color='blue',marker='H')
    plt.plot(df.index,df[2].cumsum(),ls=':',color='green',marker='d')
    plt.legend(['one','two','three'])
    Out[142]:
    <matplotlib.legend.Legend at 0x1d43e550>

    儲存圖片

    使用figure物件的savefig的函式

    • filename
      含有檔案路徑的字串或Python的檔案型物件。影象格式由副檔名推斷得出,例如,.pdf推斷出PDF,.png推斷出PNG (“png”、“pdf”、“svg”、“ps”、“eps”……)
    • dpi
      影象解析度(每英寸點數),預設為100
    • facecolor
      影象的背景色,預設為“w”(白色)
    In [148]:
    # 獲取fig物件
    fig = plt.figure()
    x = np.arange(-np.pi,np.pi,0.1)
    plt.plot(x,np.sin(x))
    fig.savefig('dancer.png',dpi=100,facecolor='blue')
    fig.savefig('dancer.jpg',dpi=100,facecolor='green')
    In [147]:
    png = plt.imread('dancer.png')
    jpg = plt.imread('dancer.jpg')
    display(png,jpg)
    . . .

    二、設定plot的風格和樣式

    plot語句中支援除X,Y以外的引數,以字串形式存在,來控制顏色、線型、點型等要素,語法形式為:
    plt.plot(X, Y, 'format', ...)

    點和線的樣式

    顏色

    引數color或c

    In [153]:
    x = np.arange(0,100)
    plt.plot(x,x**2,c = 'm')
    Out[153]:
    [<matplotlib.lines.Line2D at 0x1d76b770>]
    顏色值的方式
    • 別名
      • color='r'
    • 合法的HTML顏色名
      • color = 'red'
    顏色別名HTML顏色名顏色別名HTML顏色名
    藍色bblue綠色ggreen
    紅色rred黃色yyellow
    青色ccyan黑色kblack
    洋紅色mmagenta白色wwhite
    • HTML十六進位制字串
      • color = '#eeefff'
    • 歸一化到[0, 1]的RGB元組
      • color = (0.3, 0.3, 0.4)
    • jpg png 區別
    In [156]:
    plt.plot(x,x,color='#0000ff')
    plt.plot(x,2*x,color = '#00ff00')
    plt.plot(x,x/2,color = '#ff0000')
    Out[156]:
    [<matplotlib.lines.Line2D at 0x1a81b3f0>]
    In [157]:
    plt.plot(x,x,color=(0.3,0.3,0.4))
    Out[157]:
    [<matplotlib.lines.Line2D at 0x1a859830>]
    透明度

    alpha引數

    In [158]:
    plt.plot(x,x,color=(0.3,0.3,0.4),alpha=0.1)
    Out[158]:
    [<matplotlib.lines.Line2D at 0x1a88aa90>]
    背景色

    設定背景色,通過plt.subplot()方法傳入facecolor引數,來設定座標系的背景色

    In [163]:
    # 使用座標系物件繪製圖形
    # fig = plt.figure() # 通過此方法能得到畫布物件
    axes = plt.subplot(facecolor='c')
    axes.plot(x,x,color = 'g')
    Out[163]:
    [<matplotlib.lines.Line2D at 0x1d8b3e90>]
    In [164]:
    plt.subplot(facecolor = 'yellow')
    plt.plot(x,np.sin(x),color='red')
    Out[164]:
    [<matplotlib.lines.Line2D at 0x1efd02b0>]

    線型

    引數linestyle或ls

    線條風格描述線條風格描述
    '-'實線':'虛線
    '--'破折線'steps'階梯線
    '-.'點劃線'None' / ','什麼都不畫
    In [169]:
    x = np.arange(0,100,5)
    plt.plot(x,x,linestyle = '-',linewidth=1)
    plt.plot(x,x*2,linestyle = '--',linewidth=2)
    plt.plot(x,x*3,ls = '-.',lw=3)
    plt.plot(x,x*4,ls = ':',lw=4)
    plt.plot(x,x*5,ls = 'steps',lw=5)
    Out[169]:
    [<matplotlib.lines.Line2D at 0x1d44e730>]
    線寬

    linewidth或lw引數

    不同寬度的破折線

    dashes引數 eg.dashes = [20,50,5,2,10,5]

    設定破折號序列各段的寬度

    In [174]:
    x = np.arange(-np.pi,np.pi,0.1)
    plt.plot(x,np.sin(x),dashes=[5,1,
                
               

    相關推薦

    matplotlib實戰繪製各種圖形

    size = (m,n,3) 圖片的一般形式就是這樣的rgb 0-255 jpg圖片 166,255,89 0.0-1.0 png圖片 0.1,0.2,0.6灰度處理以後 rgb---->gray 166,255,89 ---> 190 0.1,0.2,0.6 -- > 0.4size =

    Quartz 2d 用CGContextRef 繪製各種圖形 (文字、圓、直線、弧線、矩形、扇形、橢圓、三角形、圓角形、貝塞爾曲線、圖片)

    首先了解下 CGContextRef  Graphics Context是圖形上下文,可以將其理解為一塊畫布,我們可以在上面進行繪畫操作,繪製完成後,將畫布放到我們的View 中顯示即可,View看著是一個畫框。 自己學習時實現的Demo,希望對大家有幫助,具體的實現看程式碼,並有

    使用Canvas繪製各種圖形

    Class Overview The Canvas class holds the "draw" calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the

    Python——使用matplotlib繪製各種柱狀圖

    Python——使用matplotlib繪製柱狀圖 轉載自:https://blog.csdn.net/qq_29721419/article/details/71638912 1、基本柱狀圖           首先要安

    matplotlib繪製常見圖形

    Matplotlib 是一個 Python 的 2D繪相簿,它以各種硬拷貝格式和跨平臺的互動式環境生成出版質量級別的圖形 [1]  。 通過 Matplotlib,開發者可以僅需要幾行程式碼,便可以生成繪圖,直方圖,功率譜,條形圖,錯誤圖,散點圖

    matplotlib畫廊--各種圖形繪圖程式碼

    要常看使用matplotlib可製作的各種圖表,請訪問http://matplotlib.org/的示例畫廊。單擊畫廊中的圖表, 就可檢視用於生成圖表的程式碼。 下邊將能夠繪畫的圖表列出,你可以點選網頁進入網頁檢視具體程式碼。

    matplotlib繪製基本圖形

    折線圖 import matplotlib.pyplot as plt import numpy as np x=np.arange(0,10,1) #建立一個0-10之間以1為間隔的numpy陣

    python matplotlib模組——繪製三維圖形、三維資料散點圖

    python matplotlib模組,是擴充套件的MATLAB的一個繪圖工具庫。他可以繪製各種圖形,可是最近最的一個小程式,得到一些三維的資料點圖,就學習了下python中的matplotlib模組,

    《機器學習實戰》第三章 3.2在python 中使用matplotlib註解繪製樹形圖

    《機器學習實戰》系列部落格主要是實現並理解書中的程式碼,相當於讀書筆記了。畢竟實戰不能光看書。動手就能遇到許多奇奇怪怪的問題。博文比較粗糙,需結合書本。博主邊查邊學,水平有限,有問題的地方評論區請多指教。書中的程式碼和資料,網上有很多請自行下載。 3.2.

    用shell或者python寫出各種圖形

    用shell或者python寫出各種圖形首先是shell等邊三角形[[email protected]/* */ my_script]# sh ff.sh num:6 * *** ***** ******* ********* *********** [[email&

    《R語言實戰》之 圖形初階(第三章)-- 初識

    space wid spa 開啟 display tps ping microsoft 目標 圖形初級 3.1 使用圖形 在交互式會話中,通過組條輸入語句構建圖形,直至得到想要的效果 attach(mtcars)

    matplotlib.pyplot 繪制圖形

    blank tar ref class tutorial -s 可視化 tps log 收集的一些覺得非常有用的繪圖的資料: Python--matplotlib繪圖可視化知識點整理 matplotlib.pyplot matplotlib gallerymatplotli

    python學習之matplotlib實戰

    pytho .sh itl linspace atp .py numpy width GC import numpy as np def main(): #print("hello") #line import matplotlib.pyplot

    小白python學習——matplotlib篇——繪製簡單點和直線、顏色,字型大小改變

    1.直線: import matplotlib.pyplot as plt input_values=[1,2,3,4,5] squares = [1,4,9,16,25] #設定圖表標題,並給座標軸加上標籤 plt.plot(input_values,squares,linewidth=5)

    不寫程式碼不用 Excel, 如何輕鬆搞定各種圖形化展現

    近期,大屏展示再次把 ”統計圖” 推向熱搜榜。或許你會問為什麼,這多半是因為大屏通過各種圖形元件集中呈現了使用者關心的資料,而其中每個元件基本都是一個呈現形態各異的統計圖,有的體現了某時間段某維度的資料走向,有的則是表示了某部分資料在整體的佔比,亦或是分佈聚集情況,凡此種種,不一而足。 相信資深

    計算各種圖形的周長(介面與多型)---Java

    Problem Description 定義介面Shape,定義求周長的方法length()。 定義如下類實現介面Shape的抽象方法: (1)三角形類Triangle (2)長方形類Rectangle (3)圓形類Circle等。 定義測試類ShapeTest,用Shape介面定義

    ★ Python 使用Matplotlib繪製點圖、折線圖、條狀圖與餅圖

    ★使用Python第三方庫matplotlib(2D繪相簿)繪製常見的: 點圖 折線圖 條狀圖 餅圖 ★引入: 常用的顏色c="???":             1:紅色‘red’ &n

    如何輕鬆搞定各種圖形化展現

    近期,大屏展示再次把 ”統計圖” 推向熱搜榜。或許你會問為什麼,這多半是因為大屏通過各種圖形元件集中呈現了使用者關心的資料,而其中每個元件基本都是一個呈現形態各異的統計圖,有的體現了某時間段某維度的資料走向,有的則是表示了某部分資料在整體的佔比,亦或是分佈聚集情況,凡此種種,不一而足。 相信資深

    Android使用shape繪製各種形狀

    在開發中經常會用到shape標籤來定義控制元件的背景,好處是減少apk的佔記憶體大小,shape標籤總共有四個圖形選項,分別是rectangle(矩形),oval(橢圓),line(橫線)和ring(圓環)。 res下新建一個Drawable resource file: 矩形效果:

    CAD技巧-怎麼在CAD中繪製螺旋圖形

    CAD技巧,怎麼在CAD中繪製螺旋圖形?小夥伴們都知道,在CAD行業中,日常的工作就是繪製編輯CAD圖紙,在編輯CAD圖紙檔案的時候都需要藉助CAD編輯器的繪製,CAD編輯器中有許多的功能可以提供我們使用,比如說繪製長方體,繪製多邊形等一些操作,但是怎麼在個繪製螺旋圖形?小夥伴們知道要怎麼進行操作嗎?下面我們