📜  如何使用 Matplotlib 在Python中可视化稀疏矩阵?

📅  最后修改于: 2022-05-13 01:55:23.520000             🧑  作者: Mango

如何使用 Matplotlib 在Python中可视化稀疏矩阵?

Matplotlib是Python中用于数组二维图的惊人可视化库。 Matplotlib 是一个基于 NumPy 数组构建的多平台数据可视化库,旨在与更广泛的 SciPy 堆栈配合使用。

使用 Matplotlib 可视化稀疏矩阵

Spy是一个用于将数组可视化为图像的函数,类似于 matplotlib imshow函数,但它用于稀疏矩阵而不是密集矩阵的情况。稀疏矩阵是大多数元素为零的矩阵。

稀疏矩阵及其表示

Spy函数使用两种绘图样式来可视化数组,它们是:

  • 图片风格
  • 标记样式

两种样式都可用于完整数组,但在 spmatrix 实例的情况下,只有标记样式有效。如果markermarkersizeNone ,则使用imshow函数,并将所有剩余的关键字参数传递给该函数;否则,将返回一个 Line2D 对象,其中 marker 的值确定标记类型,并将任何剩余的关键字参数传递给 plot函数。

参数:

ParameterValueUse
Zarray-like (M, N)The array to be plotted
Precisionfloat or ‘present’, optional
default:zero
If precision is 0, any non-zero value will be plotted; else, values of |Z| > precision will be plotted.
For spmatrix instances, there is a special case: if precision is ‘present’, any value present in the array will be plotted, even if it is identically zero.
Origin{‘upper’, ‘lower’}, optional
default:’upper’
Place the [0, 0] index of the array in the upper left or lower left corner of the axes.
Aspect{‘equal’, ‘auto’, None} or float, optional
default:’equal’
It controls the aspect ratio of the axes. The aspect is of particular relevance for images since it may distort the image, i.e. pixel will not be square.
  • ‘equal’: Ensures an aspect ratio of 1. Pixels will be square.
  • ‘auto’: The axes is kept fixed and the aspect is adjusted so that the data fit in the axes. In general, this will result in non-square pixels.
  • None: Use rcParams[“image.aspect”]
  • 其他参数:**kwargs

    这些是有助于获得不同绘图样式的附加参数。

    PropertyDescription
    agg_filtera filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array
    alphafloat or None
    animatedbool
    antialiasedbool
    clip_boxBbox
    clip_onbool
    clip_pathPatch or (Path, Transform) or None
    colorcolor
    containscallable
    dash_capstyle{‘butt’, ’round’, ‘projecting’}
    dash_joinstyle{‘miter’, ’round’, ‘bevel’}
    dashessequence of floats (on/off ink in points) or (None, None)
    data(2, N) array or two 1D arrays
    drawstyle{‘default’, ‘steps’, ‘steps-pre’, ‘steps-mid’, ‘steps-post’}
    figurefigure
    fillstyle{‘full’, ‘left’, ‘right’, ‘bottom’, ‘top’, ‘none’}
    gridstr
    in_layoutbool
    labelobject
    linestyle{‘-‘, ‘–‘, ‘-.’, ‘:’, ”, (offset, on-off-seq), …}
    linewidthfloat
    markermarker style
    markeredgecolorcolor
    markeredgewidthfloat
    markerfacecolorcolor
    markerfacecoloraltcolor
    markersizefloat
    markeveryNone or int or (int, int) or slice or List[int] or float or (float, float)
    path_effectsAbstract path effects
    pickerfloat or callable[[Artist, Event], Tuple[bool, dict]]
    pickradiusfloat
    rasterizedbool or None
    sketch_params(scale: float, length: float, randomness: float)
    snapbool or None
    solid_capstyle{‘butt’, ’round’, ‘projecting’}
    solid_joinstyle{‘miter’, ’round’, ‘bevel’}
    transformmatplotlib.transforms.Transform
    urlstr
    visiblebool
    xdata1D array
    ydata1D array
    zorderfloat

    示例 1:

    # Implementation of matplotlib spy function
    import matplotlib.pyplot as plt
    import numpy as np
      
      
    x = np.random.randn(50, 50)
      
    x[15, :] = 0.
    x[:, 40] = 0.
      
    plt.spy(x)
    

    输出:
    可视化稀疏矩阵python

    示例 2:

    # Implementation of matplotlib spy function
    import matplotlib.pyplot as plt
    import numpy as np
      
    x = np.random.randn(50, 50)
    x[15, :] = 0.
    x[:, 40] = 0.
      
    plt.spy(x, precision = 0.1, markersize = 5)
    

    输出:
    可视化稀疏矩阵python