import matplotlib.pyplot as plt
import numpy as np
import matplotlib.cm as cm
values = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],
[2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],
[1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],
[0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],
[0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],
[1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],
[0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]])
fig, ax = plt.subplots()
ax.imshow(values, cmap=cm.Greens)
ax.set_axis_off()
# annotate the heatmap
for i in range(values.shape[0]):
for j in range(values.shape[1]):
text = ax.text(j, i, values[i, j],
ha="center", va="center")
fig.tight_layout()
plt.show()
字符串
的数据 1.或者,您可以使用带注解的欣顿图,以更好地查看数组中值的大小和符号:
import matplotlib.pyplot as plt
import numpy as np
# based on the matplotlib example "Hinton diagrams":
# https://matplotlib.org/stable/gallery/specialty_plots/hinton_demo.html#sphx-glr-gallery-specialty-plots-hinton-demo-py
def hinton(matrix, max_weight=None, ax=None):
"""Draw Hinton diagram for visualizing a weight matrix."""
ax = ax if ax is not None else plt.gca()
if not max_weight:
max_weight = 2 ** np.ceil(np.log2(np.abs(matrix).max()))
ax.patch.set_facecolor('gray')
ax.set_aspect('equal', 'box')
ax.xaxis.set_major_locator(plt.NullLocator())
ax.yaxis.set_major_locator(plt.NullLocator())
for (x, y), w in np.ndenumerate(matrix):
color = 'white' if w > 0 else 'black'
size = np.sqrt(abs(w) / max_weight)
rect = plt.Rectangle([x - size / 2, y - size / 2], size, size,
facecolor=color, edgecolor=color)
ax.add_patch(rect)
textcolor = 'black' if w > 0 else 'white'
ax.text(x, y,'{0:3.2f}'.format(w), ha="center", va="center", size=size*30, color=textcolor)
ax.autoscale_view()
ax.invert_yaxis()
if __name__ == '__main__':
# Fixing random state for reproducibility
np.random.seed(19680801)
hinton(np.random.rand(6, 4) - 0.5)
plt.show()
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import PathPatch
from matplotlib.text import TextPath
from matplotlib.transforms import Affine2D
import mpl_toolkits.mplot3d.art3d as art3d
# copied from the "Draw flat objects in 3D plot" matplotlib example:
# https://matplotlib.org/stable/gallery/mplot3d/pathpatch3d.html#sphx-glr-gallery-mplot3d-pathpatch3d-py
def text3d(ax, xyz, s, zdir="z", size=None, angle=0, usetex=False, **kwargs):
"""
Plots the string *s* on the axes *ax*, with position *xyz*, size *size*,
and rotation angle *angle*. *zdir* gives the axis which is to be treated as
the third dimension. *usetex* is a boolean indicating whether the string
should be run through a LaTeX subprocess or not. Any additional keyword
arguments are forwarded to `.transform_path`.
Note: zdir affects the interpretation of xyz.
"""
x, y, z = xyz
if zdir == "y":
xy1, z1 = (x, z), y
elif zdir == "x":
xy1, z1 = (y, z), x
else:
xy1, z1 = (x, y), z
text_path = TextPath((0, 0), s, size=size, usetex=usetex)
trans = Affine2D().rotate(angle).translate(xy1[0], xy1[1])
p1 = PathPatch(trans.transform_path(text_path), **kwargs)
ax.add_patch(p1)
art3d.pathpatch_2d_to_3d(p1, z=z1, zdir=zdir)
# generate 3D data array
values = np.random.rand(4, 4, 4)
x_num = values.shape[0]
y_num = values.shape[1]
z_num = values.shape[2]
# all voxels are going to be filled
filled = np.ones((x_num, y_num, z_num), dtype=bool)
# plot voxels
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.voxels(filled=filled, facecolor='red', edgecolor='k', alpha=0.3)
# place values inside voxels
for x in range(x_num):
for y in range(y_num):
for z in range(z_num):
text3d(ax, (x + 0.2, y, z + 0.4), '{0:3.2f}'.format(values[x, y, z]), zdir="y", size=0.3, ec="none", fc="k")
ax.set_axis_off()
plt.show()
2条答案
按热度按时间pkln4tw61#
你可以从第一原则开始:
字符串
简单示例
更多相关示例
e4eetjau2#
1.为了可视化简单的2D数组,你可以使用带注解的热图,例如:
字符串
的数据
1.或者,您可以使用带注解的欣顿图,以更好地查看数组中值的大小和符号:
型
的
1.如果你想显示3D数组的内容,那么你可能会使用像带注解的voxels这样的东西:
型
的
在这张图中,这些值不容易辨别,但Matplotlib允许你旋转3D图。数组的某些部分可以通过参数“filled”来突出显示(例如,显示最近的拼接结果)。