Python中的 Matplotlib.pyplot.matshow()
Matplotlib是Python中用于数组二维图的惊人可视化库。 Matplotlib 是一个基于 NumPy 数组构建的多平台数据可视化库,旨在与更广泛的 SciPy 堆栈配合使用。它是由 John Hunter 在 2002 年推出的。
matplotlib.pyplot.matshow()
matplotlib.pyplot.matshow()函数用于在新图形窗口中将数组表示为矩阵。左上角设置为原点,行(数组的第一维)以水平形式显示。图形窗口的纵横比是根据阵列设置的,以避免图形又短又窄。 x 轴刻度标签放置在顶部。
Syntax: matplotlib.pyplot.matshow(A, fignum=None, **kwargs)
Parameters:
- A:: It is an array like object that represents the matrix. It is a required parameter.
- fignum: It accepts three values namely ‘None’, ‘False’ or an integer value. If the value is set to None it would create a new window of the figure with automated numbering. If the value is a non-zero integer then it is drawn into the figure respective to the given number or it creates it if it doesn’t exist. If the ‘0’ is set as the value of this parameter then it uses the current axes or it creates one if it doesn’t exist.
Returns: It returns an image of Axesimage class.
Other parameters: It also accepts the imshow argument for showing image.
示例 1:
Python3
import matplotlib.pyplot as plot
import numpy as np
# an array with linearly increasing values
array = np.diag(range(20))
plot.matshow(array)
plot.show()
Python3
import numpy as np
import matplotlib.pyplot as plt
alphabets = ['A', 'B', 'C', 'D', 'E']
# randomly generated array
random_array = np.random.random((5, 5))
figure = plt.figure()
axes = figure.add_subplot(111)
# using the matshow() function
caxes = axes.matshow(random_array, interpolation ='nearest')
figure.colorbar(caxes)
axes.set_xticklabels(['']+alphabets)
axes.set_yticklabels(['']+alphabets)
plt.show()
输出:
示例 2:
Python3
import numpy as np
import matplotlib.pyplot as plt
alphabets = ['A', 'B', 'C', 'D', 'E']
# randomly generated array
random_array = np.random.random((5, 5))
figure = plt.figure()
axes = figure.add_subplot(111)
# using the matshow() function
caxes = axes.matshow(random_array, interpolation ='nearest')
figure.colorbar(caxes)
axes.set_xticklabels(['']+alphabets)
axes.set_yticklabels(['']+alphabets)
plt.show()
输出: