【Matplotlib】使用速记

【持续更新】

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python.   

参考:Matplotlib 中文网

简介

参考:菜鸟教程 | Matplotlib 教程

%matplotlib inline】是一个魔法函数(Magic Functions)。官方给出的定义是:IPython有一组预先定义好的所谓的魔法函数(Magic Functions),你可以通过命令行的语法形式来访问它们。可见“%matplotlib inline”就是模仿命令行来访问magic函数的在IPython中独有的形式。%matplotlib inline 可以在Ipython编译器里直接使用,功能是可以内嵌绘图,并且可以省略掉plt.show()这一步。

在spyder或者pycharm实际运行代码的时候,可以直接注释掉。【注意:既然是IPython的内置magic函数,那么在Pycharm中是不会支持的。】

魔法函数

magic函数分两种:一种是面向行的,另一种是面向单元型的。

行magic函数是用前缀“%”标注的,很像我们在系统中使用命令行时的形式,例如在Mac中就是你的用户名后面跟着“$”。“%”后面就是magic函数的参数了,但是它的参数是没有被写在括号或者引号中来传值的。

单元型magic函数是由两个“%%”做前缀的,它的参数不仅是当前“%%”行后面的内容,也包括了在当前行以下的行。

legend

Plotting legends in Matplotlib. There are many ways to create and customize legends in Matplotlib. 

参考:Legend Demo

pyplot

matplotlib.pyplot is a state-based interface to matplotlib. It provides a MATLAB-like way of plotting.

pylab 是 matplotlib 面向对象绘图库的一个接口。它的语法和 Matlab 十分相近。也就是说,它主要的绘图命令和 Matlab 对应的命令有相似的参数。

 画图。常用:

import matplotlib.pyplot as plt

figtext

matplotlib.pyplot.figtext,在figure上添加text。

plt.figtext(x, y, s, fontdict=None, **kwargs)

figure

matplotlib.pyplot.figure,Create a new figure, or activate an existing figure.

plt.figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True, FigureClass=<class 'matplotlib.figure.Figure'>, clear=False, **kwargs)
plt.figure(figsize=(15,6)) # 指定图像尺寸等属性

text

matplotlib.pyplot.text,Add text to the axes.

plt.text(x, y, s, fontdict=None, **kwargs)
plt.text(a, b+0.05, '%.0f'%b, ha='center', va='bottom', fontsize=10) # example

xticks

matplotlib.pyplot.xticks,Get or set the current tick locations and labels of the x-axis. 设置x轴的属性。

plt.xticks(ticks=None, labels=None, **kwargs)

**kwargs属性参见text的设置。

subplots

Create a figure and a set of subplots. This utility wrapper makes it convenient to create common layouts of subplots, including the enclosing figure object, in a single call.

源码参考点击

plt.subplots(nrows=1, ncols=1, *, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)

使用:

fig, ax = plt.subplots()

bar

matplotlib.pyplot.bar,画柱状图。

plt.bar(x, height, width=0.8, bottom=None, *, align='center', data=None, **kwargs)
# The bars are positioned at x with the given alignment. Their dimensions are given by height and width. The vertical baseline is bottom (default 0).

使用可以参考:Matplotlib数据可视化(5):柱状图与直方图

specgram

直接输入原始数据,可以画频谱图。

plt.specgram(x, NFFT=None, Fs=None, Fc=None, detrend=None, window=None, noverlap=None, cmap=None, xextent=None, pad_to=None, sides=None, scale_by_freq=None, mode=None, scale=None, vmin=None, vmax=None, *, data=None, **kwargs)

input

parameters

x 1-D array or sequence 数组或序列
Fs scalar 标量.  The default value is 2.
window callable or ndarray
sides {'default', 'onesided', 'twosided'}
pad_to 执行TTF填充的点数,默认none,此时等于NFFT。
NFFT FFT点数,2的指数最好,默认值256。不能被用作zero padding。可以用pad_to代替。
detrend {'none', 'mean', 'linear'} or callable, default 'none'
scale_by_freq

{bool, optional}. The default is True for MATLAB compatibility. The parameters detrend and scale_by_freq do only apply when mode is set to ‘psd’.

mode {'default', 'psd', 'magnitude', 'angle', 'phase'}.  Default is 'psd'.
scale {'default', 'linear', 'dB'}
cmap A matplotlib.colors.Colormap instance; if None, use default determined by rc
xextent None or (xmin, xmax)

output

parameters

spectrum 2-D array. Columns are the periodograms of successive segments.
freqs 1-D array. The frequencies corresponding to the rows in spectrum.
t 1-D array. The times corresponding to midpoints of segments (i.e., the columns in spectrum).
im instance of class AxesImage . The image created by imshow containing the spectrogram

Compute and plot a spectrogram of data in x. Data are split into NFFT length segments and the spectrum of each section is computed. The windowing function window is applied to each segment, and the amount of overlap of each segment is specified with noverlap. The spectrogram is plotted as a colormap (using imshow).

colormap

https://matplotlib.org/tutorials/colors/colormaps.html

boxplot

matplotlib.pyplot.boxplot,箱线图绘制。

matplotlib.pyplot.boxplot(x, notch=None, sym=None, vert=None, whis=None, positions=None, widths=None, patch_artist=None, bootstrap=None, usermedians=None, conf_intervals=None, meanline=None, showmeans=None, showcaps=None, showbox=None, showfliers=None, boxprops=None, labels=None, flierprops=None, medianprops=None, meanprops=None, capprops=None, whiskerprops=None, manage_ticks=True, autorange=False, zorder=None, *, data=None)
原文地址:https://www.cnblogs.com/ytxwzqin/p/12072028.html