基于DataFrame的热力图

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

Heatmap based on DataFrame

问题

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

  1. 数据框架
  2. import pandas as pd
  3. import plotly.express as px
  4. df = pd.DataFrame({"A":[1,2,3],"B":[4,5,6]})
  5. print(df)
  6. A B
  7. 0 1 4
  8. 1 2 5
  9. 2 3 6
  10. 使用以下代码将其转换为热力图
  11. fig = px.density_heatmap(df)
  12. fig.show()
  13. 结果是
  14. [![在此输入图像描述][1]][1]
  15. [1]: https://i.stack.imgur.com/KfnE7.png

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

英文:

The Dataframe

  1. import pandas as pd
  2. import plotly.express as px
  3. df = pd.DataFrame({"A":[1,2,3],"B":[4,5,6]})
  4. print(df)
  5. A B
  6. 0 1 4
  7. 1 2 5
  8. 2 3 6

transformed into a heatmap using

  1. fig = px.density_heatmap(df)
  2. 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 来显示图像:

  1. px.imshow(df)

基于DataFrame的热力图

英文:

You might want to use px.imshow:

  1. px.imshow(df)

基于DataFrame的热力图

答案2

得分: 1

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

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

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

英文:

You might use seaborn.heatmap for this task following way

  1. import pandas as pd
  2. import seaborn as sns
  3. df = pd.DataFrame({"A":[1,2,3],"B":[4,5,6]})
  4. 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:

确定