matplotlib繪制矢量圖像(svg),pdf and ps文件


機器學習的過程中處理數據,會遇到數據可視化的問題.

大部分都是利用python的matplotlib庫進行數據的可視化處理.

plt.show() 默認都是輸出.png文件,圖片只要稍微放大一點,就糊的不行.

下面給出一段正常進行數據處理可視化輸出圖片的代碼

import pandas as pd
import matplotlib.pyplot as plt

with open('sourcedata2.csv')as f:
    df=pd.read_csv(f,header=0)

X=df[df.columns[1:6]]
y=df['Vibration']
plt.figure()
f,ax1=plt.subplots()
for i in range(1,7):
    number=320+i
    ax1.locator_params(nbins=3)
    ax1=plt.subplot(number)
    plt.title(list(df)[i])
    ax1.scatter(df[df.columns[i]],y)
plt.tight_layout(pad=0.4,w_pad=0.5,h_pad=1.0)
plt.show()

上面這段代碼會直接顯示出繪制的圖像.我們可以將圖片另存為.

但是我們是不是可以直接在代碼中將圖片進行保存吶?

matplotlib,pyplot中有一個內置函數savefig

查看savefig()函數的功能

savefig(*args, **kwargs)
    Save the current figure.
    
    Call signature::
    
      savefig(fname, dpi=None, facecolor='w', edgecolor='w',
              orientation='portrait', papertype=None, format=None,
              transparent=False, bbox_inches=None, pad_inches=0.1,
              frameon=None)
    
    The output formats available depend on the backend being used.
    
    Arguments:
    
      *fname*:
        A string containing a path to a filename, or a Python
        file-like object, or possibly some backend-dependent object
        such as :class:`~matplotlib.backends.backend_pdf.PdfPages`.
    
        If *format* is *None* and *fname* is a string, the output
        format is deduced from the extension of the filename. If
        the filename has no extension, the value of the rc parameter
        ``savefig.format`` is used.
    
        If *fname* is not a string, remember to specify *format* to
        ensure that the correct backend is used.
    
    Keyword arguments:
    
      *dpi*: [ *None* | ``scalar > 0`` | 'figure']
        The resolution in dots per inch.  If *None* it will default to
        the value ``savefig.dpi`` in the matplotlibrc file. If 'figure'
        it will set the dpi to be the value of the figure.
    
      *facecolor*, *edgecolor*:
        the colors of the figure rectangle
    
      *orientation*: [ 'landscape' | 'portrait' ]
        not supported on all backends; currently only on postscript output
    
      *papertype*:
        One of 'letter', 'legal', 'executive', 'ledger', 'a0' through
        'a10', 'b0' through 'b10'. Only supported for postscript
        output.
    
      *format*:
        One of the file extensions supported by the active
        backend.  Most backends support png, pdf, ps, eps and svg.

可以看到一個關鍵詞參數format,it support png,pdf,ps,eps and svg.

重新實現上一段代碼

X=df[df.columns[1:6]]
y=df['Vibration']
plt.figure()
f,ax1=plt.subplots()
for i in range(1,7):
    number=320+i
    ax1.locator_params(nbins=3)
    ax1=plt.subplot(number)
    plt.title(list(df)[i])
    ax1.scatter(df[df.columns[i]],y)
plt.tight_layout(pad=0.4,w_pad=0.5,h_pad=1.0)
plt.savefig(fname="name",format="svg")
plt.show()

在plt.savefig()函數的format參數選擇你需要保存圖片的格式,和圖片的名稱fname="your picture name"

就可以實現圖片多種格式的輸出.

利用graphviz來繪制繪圖

神經網絡的圖(也就是其基本的模型架構)可以使用process on 也可以在python庫函數中進行繪制

from pydotplus import graphviz


免責聲明!

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



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