Heatmap Seaborn fmt = 'd' error

In addition to the previous question

I can plan well the Heat map with Seaborn and the annotation can be obtained with the proposal. But now I see a new problem.

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 the float error and changed to fmt='f'

    AND get the count of the desired column.

But when the same axis value is repeated, it does not add the counter from the desired column. Any solution for this pls?

As seen from the input file

876,Japan,Bus,Beta,45,57
876,Japan,Bus,Beta,40,57
876,Japan,Bus,Beta,40,57

      

It has 3 lines in repetition, and their value should be displayed as the sum of the cell that represents Japan

and Beta

should annotate the value as 125

, instead, it shows 41.7

. How can I achieve this? Can I also specify two values ​​as annotation?

enter image description here

  1. The second doubt is what pivot

    I am giving value='00:00:00'

    , but I need it to dynamically read the last column from the file.
+3


source to share


1 answer


You can use the aggfunc keyword passing in a dict:

aggfunc:

default numpy.mean or list of functions If list of passed functions, then the resulting pivot table will have hierarchical columns, the top level of which is the names of the functions (derived from the function objects themselves)



sns.heatmap(df3.pivot_table(index='Place', columns='Name', 
values='00:00:00',aggfunc={'00:00:00':np.sum}), annot=True, fmt='.1f')

      

What are the outputs:

enter image description here

+7


source







All Articles