numpy 将二维数组另存为数字图表图像

5vf7fwbs  于 2022-11-24  发布在  其他
关注(0)|答案(1)|浏览(151)

可以使用以下命令在控制台中打印整个阵列:

import sys
import numpy
numpy.set_printoptions(threshold=sys.maxsize)

但是否也有导出一种“数字图表图像”的选项?例如

import numpy as np

numberchart = np.range(100)

WANTED_RESULT.png
我用matplotlib绘制了某种热图,但我正在寻找像.png这样的图像格式

harvest = 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()
im = ax.imshow(harvest)

y, x = harvest.shape

ax.set_xticks(np.arange(x))
ax.set_yticks(np.arange(y))

plt.setp(ax.get_xticklabels())#, rotation=45, ha="right",
         #rotation_mode="anchor")

# Loop over data dimensions and create text annotations.
for i in range(x):
    for j in range(y):
        text = ax.text(j, i, harvest[i, j],
                       ha="center", va="center", color="w")

ax.set_title("NumberChart")
fig.tight_layout()
plt.show()
kh212irz

kh212irz1#

我对你的代码做了一些小的修改。它更像是一个工作区,但做了你希望我相信的事情:

import matplotlib.pyplot as plt
harvest = 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()
im = ax.imshow(harvest*0, cmap="Greys")

y, x = harvest.shape

ax.set_xticks(np.arange(x))
ax.set_yticks(np.arange(y))

plt.setp(ax.get_xticklabels())#, rotation=45, ha="right",
     #rotation_mode="anchor")

# Loop over data dimensions and create text annotations.
for i in range(x):
    for j in range(y):
        text = ax.text(j, i, harvest[i, j],
                   ha="center", va="center", color="k")

ax.set_xticks(np.arange(-.5, harvest.shape[0], 1), minor=True)
ax.set_yticks(np.arange(-.5, harvest.shape[1], 1), minor=True)
plt.tick_params(
    axis='x',     
    which='both',      
    bottom=False,      
    top=False,         
    labelbottom=False)
plt.tick_params(
    axis='y',          
    which='both',      
    left=False,      
    top=False,      
    labelleft=False)
ax.grid(which='minor', color='k', linestyle='-', linewidth=1)
ax.set_title("NumberChart")
fig.tight_layout()
plt.show()

相关问题