以图形形式表示python词典

2024-10-05 10:03:45 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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表。我在Jupyter笔记本上运行这个。 我正在运行SARSA,总共有10万集;希望每10公里一集都能看到Q表

我想matplotlib也许能做到这一点?但我对这种特殊的表现形式不太熟悉

如果有人知道更好的方法来表示Q表(相对于这种特殊的图形格式),我愿意接受建议。如果使用2D数组更好,我还可以将Q表表示为2D numpy数组而不是字典

提前感谢您的回复


Tags: 方法numpy图形字典matplotlib可视化table笔记本
1条回答
网友
1楼 · 发布于 2024-10-05 10:03:45

我真的不知道Q表是什么,但我确实花了很多时间来想象不同的东西

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

我已经创建了一个字典,其中列出了我认为Q表中可能存在的代表性值?希望我的假设足够接近,您可以使用下面的代码将问题推过终点线

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")

Example q-table

相关问题 更多 >

    热门问题