用dict的x和y标签在python matplotlib中创建热图{元组:浮点}形式

2024-05-19 18:18:38 发布

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

我有一个dict,它由电影标题对作为键和相似性分数作为值组成:

{('Source Code ', 'Hobo with a Shotgun '): 1.0, ('Adjustment Bureau, The ', 'Just Go with It '): 1.0, ('Limitless ', 'Arthur '): 1.0, ('Adjustment Bureau, The ', 'Kung Fu Panda 2 '): 1.0, ('Rise of the Planet of the Apes ', 'Scream 4 '): 1.0, ('Source Code ', 'Take Me Home Tonight '): 1.0, ('Midnight in Paris ', 'Take Me Home Tonight '): 1.0, ('Harry Potter and the Deathly Hallows: Part 2 ', 'Pina '): 1.0, ('Avengers, The ', 'Drive Angry '): 1.0, ('Limitless ', 'Super 8 '): 1.0, ('Harry Potter and the Deathly Hallows: Part 2 ', 'Arthur '): 1.0, ('Source Code ', 'Melancholia '): 0.6666666666666666, ('Harry Potter and the Deathly Hallows: Part 2 ', 'Jane Eyre '): 1.0, ('Avengers, The ', 'Arthur '): 0.6666666666666666, ('The Artist ', 'Attack the Block '): 1.0, ('Midnight in Paris ', 'Priest '): 1.0, ('Adjustment Bureau, The ', 'Hanna '): 1.0, ('The Artist ', 'Thor '): 1.0, ('The Artist ', 'Zeitgeist: Moving Forward '): 1.0, ('The Artist ', 'Green Hornet, The '): 1.0, ('X-Men: First Class ', 'Sanctum '): 1.0, ('Source Code ', 'Green Hornet, The '): 1.0, ('Harry Potter and the Deathly Hallows: Part 2 ', 'Something Borrowed '): 1.0, ('Adjustment Bureau, The ', 'Rio '): 1.0, ('Avengers, The ', 'Mechanic, The '): 1.0, ('Rise of the Planet of the Apes ', 'Something Borrowed '): 0.6666666666666666, ('Captain America: The First Avenger ', 'Attack the Block '): 0.6666666666666666, ('Avengers, The ', 'Zeitgeist: Moving Forward '): 1.0, ('Midnight in Paris ', 'Arthur '): 1.0, ('Source Code ', 'Arthur '): 1.0, ('Limitless ', 'Take Me Home Tonight '): 1.0, ('Midnight in Paris ', 'Win Win '): 1.0, ('X-Men: First Class ', 'Something Borrowed '): 1.0, ('Avengers, The ', 'Dilemma, The '): 1.0, ('X-Men: First Class ', 'Green Hornet, The '): 1.0, ('The Artist ', 'Just Go with It '): 1.0, ('Rise of the Planet of the Apes ', 'Arthur '): 1.0, ('Captain America: The First Avenger ', 'Lincoln Lawyer, The '): 1.0, ('X-Men: First Class ', 'Hobo with a Shotgun '): 1.0, ('Limitless ', 'Mechanic, The '): 0.6666666666666666, ('Captain America: The First Avenger ', 'Green Hornet, The '): 1.0, ('Captain America: The First Avenger ', 'Hangover Part II, The '): 1.0, ('X-Men: First Class ', 'Hanna '): 1.0, ('Rise of the Planet of the Apes ', 'Priest '): 1.0, ('Midnight in Paris ', 'I Am Number Four '): 1.0, ('Rise of the Planet of the Apes ', 'Tree of Life, The '): 1.0, ('Captain America: The First Avenger ', 'Hanna '): 1.0, ('Harry Potter and the Deathly Hallows: Part 2 ', 'Win Win '): 1.0, ('Limitless ', 'Drive Angry '): 0.6666666666666666, ('Adjustment Bureau, The ', 'Hangover Part II, The '): 1.0}

我想用matplotlib创建一个热图,每个键中的第一个电影作为y标签,每个键中的第二个电影作为x标签,相似性分数作为z轴。在

{I)分布图(见下图)。在

enter image description here

我现在的代码创建纽比.ndarrayz轴如下所示:

^{pr2}$

有什么办法来创造这种形象?在


Tags: ofthesourcecodefirstrisepartbureau
1条回答
网友
1楼 · 发布于 2024-05-19 18:18:38

我将使用^{}来取消数据的堆叠,然后使用^{}来绘制结果。下面是一个使用您提供的词典的示例:

import pandas as pd
ser = pd.Series(list(dict_sim_scores.values()),
                  index=pd.MultiIndex.from_tuples(dict_sim_scores.keys()))
df = ser.unstack().fillna(0)
df.shape
# (10, 27)

现在使用seaborn热图函数绘制结果:

^{2}$

enter image description here

相关问题 更多 >