以图片形式表示 python 词典

Representing python dictionary in pictorial form

我需要一种以图形形式表示字典(或 NumPy 二维数组)的方法,可能如下图所示。

Q-table

我的字典目前是这样的 Q: {(0,'U'): -0.1, (0,'R'): -0.254, (0,'D'): -0.9, (0,'L'): -0.23, ...} 其中 U、R、D、L 对应方向,上、下、右、左

对于其他上下文,我希望可视化 SARSA 学习方法的 Q-table。我在 Jupyter notebook 中 运行 这个。 我是 运行 SARSA,超过 100k 集并且想每 10k 集 运行.

可视化 Q-table

我想 matplotlib 可以做到这一点?但是我对这种特殊的表示形式不是很熟悉。

如果有人知道更好的方式来表示 Q-table(与这种特殊的图片格式相反),我愿意接受建议。如果使用二维数组会更好,我也可以将 Q-table 表示为二维 numpy 数组而不是字典。

提前感谢您的任何回复!

我真的不知道 Q-table 是什么,但我确实花了很多时间尝试想象不同的事物。

根据我对你的问题的理解,你需要 10 个 table,我在下面的代码中将它们排列成 2 行 5 列的格子。也就是说,我希望这段代码可以扩展到您需要的任何数量。

我已经创建了一个字典,其中包含我认为 Q-table 中可能包含的内容的代表值?希望我的假设足够接近,您可以使用下面的代码解决您的问题。

from matplotlib import pyplot as plt
import numpy as np

n_row = 2 # number of rows
n_col = 5 # number of columns

# Make up some dummy data
Q = {}
for m in range(n_row * n_col):
    Q[(m, 'U')] = 2 * np.random.random() - 1
    Q[(m, 'D')] = 2 * np.random.random() - 1
    Q[(m, 'L')] = 2 * np.random.random() - 1
    Q[(m, 'R')] = 2 * np.random.random() - 1


# Plotting paramters:
boxsize = 0.5 # box size in inches
fontcol = 'k' # color of your U/D/L/R values
centerfontcol = [0.3, 0.3, 0.3] # color of the box number in the center
fontsize = 4   # font size to use

maxalpha = 0.3 # just to make boxes different backgrounds as per your
               # example if you want them all white, then remove this
               # and the "fill" command below

# Create a figure. Note that the "figsize" command gives yout the dimensions of
# your figure, in inches
fig = plt.figure(figsize = (n_col * boxsize, n_row * boxsize))

# This creates an axes for plotting. If you imagine your figure
# "canvas" as having normal coordinates where the bottom left is (0,0)
# and the top right is (1,1), then the line below gives you an axis
# that fills the entire area. The values give [Left, Bottom,
# Width, Height].
ax = plt.axes([0, 0, 1, 1])

# These are spacings from the edges of each table used in setting the
# text
xspace = 0.2 / n_col
yspace = 0.15 / n_row


m = 0 # m is a counter that steps through your tables

# When stepping through each table, we set things up so that the
# limits of the figure are [0, 1] in the x-direction and the
# y-direction so values are normalized

for r in range(n_row):
    # top and bottom bounds of the table
    y1 = 1 - (r + 1) / n_row  
    y2 = 1 - r / n_row
    for c in range(n_col):
        # left and right bounds of the table
        x1 = c / n_col
        x2 = (c+1) / n_col

        # plot the box for the table
        plt.plot([x1, x1, x2, x2, x1], [y1, y2, y2, y1, y1], 'k')

        # fill the box for the table, if you want
        # fillalpha is just if you want the boxes different shades
        fillalpha = maxalpha * np.random.random()
        plt.fill([x1, x1, x2, x2, x1], [y1, y2, y2, y1, y1], 'k', alpha = fillalpha)

        # Put the values in
        # center
        plt.text((x1 + x2) / 2, (y1 + y2) / 2, "%i" % m,
                 color = centerfontcol, fontsize = fontsize, va = 'center', ha = 'center')

        # left
        plt.text(x1 + xspace, (y1 + y2) / 2, "%.2f" % Q[(m, 'L')],
                 color = fontcol, fontsize = fontsize, va = 'center', ha = 'center')
        # right
        plt.text(x2 - xspace, (y1 + y2) / 2, "%.2f " % Q[(m, 'R')],
                 color = fontcol, fontsize = fontsize, va = 'center', ha = 'center')

        # up
        plt.text((x1 + x2) / 2, y2 - yspace, "%.2f" % Q[(m, 'U')],
                 color = fontcol, fontsize = fontsize, va = 'center', ha = 'center')

        # down
        plt.text((x1 + x2) / 2, y1 + yspace, "%.2f" % Q[(m, 'D')],
                 color = fontcol, fontsize = fontsize, va = 'center', ha = 'center')
        
        # augment the counter
        m += 1
ax.set_axis_off()
plt.savefig("q-table.png", bbox_inches = "tight")