如何使用 Matplotlib 在Python中可视化稀疏矩阵?
Matplotlib是Python中用于数组二维图的惊人可视化库。 Matplotlib 是一个基于 NumPy 数组构建的多平台数据可视化库,旨在与更广泛的 SciPy 堆栈配合使用。
使用 Matplotlib 可视化稀疏矩阵
Spy是一个用于将数组可视化为图像的函数,类似于 matplotlib imshow函数,但它用于稀疏矩阵而不是密集矩阵的情况。稀疏矩阵是大多数元素为零的矩阵。
Spy函数使用两种绘图样式来可视化数组,它们是:
- 图片风格
- 标记样式
两种样式都可用于完整数组,但在 spmatrix 实例的情况下,只有标记样式有效。如果marker或markersize为None ,则使用imshow函数,并将所有剩余的关键字参数传递给该函数;否则,将返回一个 Line2D 对象,其中 marker 的值确定标记类型,并将任何剩余的关键字参数传递给 plot函数。
Syntax: matplotlib.pyplot.spy(Z, precision=0, marker=None, markersize=None, aspect=’equal’, origin=’upper’, \*\*kwargs)
Return value:
The return type depends on the plotting style, i.e. AxesImage or Line2D.
参数:
Parameter | Value | Use |
---|---|---|
Z | array-like (M, N) | The array to be plotted |
Precision | float 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. |
其他参数:**kwargs
这些是有助于获得不同绘图样式的附加参数。
Property | Description |
---|---|
agg_filter | a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array |
alpha | float or None |
animated | bool |
antialiased | bool |
clip_box | Bbox |
clip_on | bool |
clip_path | Patch or (Path, Transform) or None |
color | color |
contains | callable |
dash_capstyle | {‘butt’, ’round’, ‘projecting’} |
dash_joinstyle | {‘miter’, ’round’, ‘bevel’} |
dashes | sequence 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’} |
figure | figure |
fillstyle | {‘full’, ‘left’, ‘right’, ‘bottom’, ‘top’, ‘none’} |
grid | str |
in_layout | bool |
label | object |
linestyle | {‘-‘, ‘–‘, ‘-.’, ‘:’, ”, (offset, on-off-seq), …} |
linewidth | float |
marker | marker style |
markeredgecolor | color |
markeredgewidth | float |
markerfacecolor | color |
markerfacecoloralt | color |
markersize | float |
markevery | None or int or (int, int) or slice or List[int] or float or (float, float) |
path_effects | Abstract path effects |
picker | float or callable[[Artist, Event], Tuple[bool, dict]] |
pickradius | float |
rasterized | bool or None |
sketch_params | (scale: float, length: float, randomness: float) |
snap | bool or None |
solid_capstyle | {‘butt’, ’round’, ‘projecting’} |
solid_joinstyle | {‘miter’, ’round’, ‘bevel’} |
transform | matplotlib.transforms.Transform |
url | str |
visible | bool |
xdata | 1D array |
ydata | 1D array |
zorder | float |
示例 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)
输出:
示例 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)
输出: