基于DataFrame的热力图

huangapple go评论91阅读模式
英文:

Heatmap based on DataFrame

问题

以下是翻译好的代码部分:

数据框架

import pandas as pd
import plotly.express as px

df = pd.DataFrame({"A":[1,2,3],"B":[4,5,6]})
print(df)

   A  B
0  1  4
1  2  5
2  3  6

使用以下代码将其转换为热力图

fig = px.density_heatmap(df)
fig.show()

结果是

[![在此输入图像描述][1]][1]

  [1]: https://i.stack.imgur.com/KfnE7.png

请注意,我只翻译了代码部分,没有包括问题中的其他内容。

英文:

The Dataframe

import pandas as pd
import plotly.express as px

df = pd.DataFrame({"A":[1,2,3],"B":[4,5,6]})
print(df)

   A  B
0  1  4
1  2  5
2  3  6

transformed into a heatmap using

fig = px.density_heatmap(df)
fig.show()

results in

基于DataFrame的热力图

What I actually want, is the index (0,1,3) on the x-axis, column names (A,B) on the y axis and the actual cell values represented by colors, as a third dimension. How can I get that representation?

答案1

得分: 1

你可以使用 px.imshow 来显示图像:

px.imshow(df)

基于DataFrame的热力图

英文:

You might want to use px.imshow:

px.imshow(df)

基于DataFrame的热力图

答案2

得分: 1

你可以按照以下方式使用 seaborn.heatmap 完成这个任务:

import pandas as pd
import seaborn as sns
df = pd.DataFrame({"A":[1,2,3],"B":[4,5,6]})
sns.heatmap(df).get_figure().savefig('heatmap.png')

这会创建一个名为 heatmap.png 的文件。
基于DataFrame的热力图

英文:

You might use seaborn.heatmap for this task following way

import pandas as pd
import seaborn as sns
df = pd.DataFrame({"A":[1,2,3],"B":[4,5,6]})
sns.heatmap(df).get_figure().savefig('heatmap.png')

creates file heatmap.png
基于DataFrame的热力图

huangapple
  • 本文由 发表于 2023年5月24日 22:03:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76324393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定