英文:
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
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)
答案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')
英文:
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')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论