Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

pandas - How can I plot a heatmap from my python dictionary?

I have a python dictionary whose keys are chess board positions (64 positions) and whose values are the number of captures taken place at the board position.

OrderedDict([('a1', 552),
             ('a2', 1128),
             ('a3', 1464),
             ('a4', 2328),
             ('a5', 2304),
             ('a6', 2088),
             ('a7', 1344),
             ('a8', 768),
             ('b1', 504),
             ('b2', 2544),
             ('b3', 3480),
             ('b4', 5016),
             ...
             ('h5', 2136),
             ('h6', 1968),
             ('h7', 816),
             ('h8', 336)])

To find the most "dangerous" positions of the chessboard, I wish to plot these values onto a heatmap using seaborn. To do this, the data should be converted to a pandas dataframe or numpy array where rows range from 1 to 8 and columns from a to h (see chessboard). I need a data frame with the exact same layout of the chessboard shown.

How do I transform this "linear" data into a 2-dimensional layout?

question from:https://stackoverflow.com/questions/66053317/how-can-i-plot-a-heatmap-from-my-python-dictionary

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can try:

# this would make `a`-`h` as rows
# transpose the data if you like
data = np.array(list(your_dict.values()) ).reshape(8,8)
plt.imshow(data, cmap='hot')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...