python利用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"

就可以实现图片多种格式的输出.

猜你喜欢

转载自www.cnblogs.com/GeekDanny/p/9300201.html
今日推荐