Python 热图 Seaborn fmt='d' 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31087613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Heat Map Seaborn fmt='d' error
提问by
In extension to my previous question
扩展我之前的问题
I can plot the Heat map with Seaborn very well and with suggestion can get annotation. But I see a new problem now.
我可以用 Seaborn 很好地绘制热图,并通过建议可以获得注释。但是我现在看到了一个新问题。
Input File
输入文件
Nos,Place,Way,Name,00:00:00,12:00:00
123,London,Air,Apollo,342,972
123,London,Rail,Beta,2352,342
123,Paris,Bus,Beta,545,353
345,Paris,Bus,Rava,652,974
345,Rome,Bus,Rava,2325,56
345,London,Air,Rava,2532,9853
567,Paris,Air,Apollo,545,544
567,Rome,Rail,Apollo,5454,5
876,Japan,Rail,Apollo,644,54
876,Japan,Bus,Beta,45,57
876,Japan,Bus,Beta,40,57
876,Japan,Bus,Beta,40,57
Program:
程序:
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
sns.set()
df = pd.read_csv('heat_map_data.csv')
df3 = df.copy()
for c in ['Place','Name']:
df3[c] = df3[c].astype('category')
sns.heatmap(df3.pivot_table(index='Place', columns='Name', values='00:00:00' ),annot=True, fmt='.1f' )
plt.show()
- If I take
fmt='d'
then I get error of float value and changed tofmt='f'
And I get the count of the desired column.
- 如果我接受,
fmt='d'
那么我会得到浮点值错误并更改为fmt='f'
我得到所需列的计数。
But When the same axis value repeats it does not add the count from desired column. Any solution for that pls ?
但是当相同的轴值重复时,它不会添加所需列的计数。请问有什么解决办法吗?
As it is seen in the input file
正如在输入文件中看到的那样
876,Japan,Bus,Beta,45,57
876,Japan,Bus,Beta,40,57
876,Japan,Bus,Beta,40,57
It has 3 rows in repeat and the value of them should be shown as sum
the cell which represents Japan
and Beta
should annot value as 125
instead it shows 41.7
. How do I achieve that? Also is it possible to give two values as annotation ?
它有 3 行重复,它们的值应显示为表示的单元格的总和,Japan
并且Beta
不应将其值125
改为它显示的值41.7
。我如何做到这一点?也可以给出两个值作为注释吗?
- Second doubt is now that in
pivot
I am givingvalue='00:00:00'
but I need it to dynamically read the last column from the file.
- 第二个疑问是现在
pivot
我正在提供,value='00:00:00'
但我需要它来动态读取文件中的最后一列。
采纳答案by Padraic Cunningham
You can use the aggfunckeyword passing in a dict:
您可以使用aggfunc关键字传入字典:
aggfunc :
aggfunc :
function, default numpy.mean, or list of functions If list of functions passed, the resulting pivot table will have hierarchical columns whose top level are the function names (inferred from the function objects themselves)
函数、默认 numpy.mean 或函数列表如果传递了函数列表,则生成的数据透视表将具有分层列,其顶层是函数名称(从函数对象本身推断)
sns.heatmap(df3.pivot_table(index='Place', columns='Name',
values='00:00:00',aggfunc={'00:00:00':np.sum}), annot=True, fmt='.1f')
Which outputs:
哪些输出: