Home > Software design >  how to create heatmap of dataframe in python
how to create heatmap of dataframe in python

Time:01-14

Im Trying to create a heatmap and am following the following question:

enter image description here

i tried the following code:

years = ["1860","1870", "1880","1890","1900","1910","1920","1930","1940","1950","1960","1970","1980","1990","2000"]
kantons = ["AG","AI","AR","BE","BL","BS","FR","GE","GL","GR","JU","LU","NE","NW","OW","SG","SH","SO","SZ","TG","TI","UR","VD","VS","ZG","ZH"]
   
df = pd(abs(dfYears), index=years, columns=kantons)

which give the exception that "AG" can not be used as float. So i thought if i need to drop the index column which is not possible.

Any suggestions?

CodePudding user response:

When replicating similar data, you can do:

import pandas as pd
import numpy as np

years = ["1860","1870", "1880","1890","1900","1910","1920","1930","1940","1950","1960","1970","1980","1990","2000"]
kantons = ["AG","AI","AR","BE","BL","BS","FR","GE","GL","GR","JU","LU","NE","NW","OW","SG","SH","SO","SZ","TG","TI","UR","VD","VS","ZG","ZH"]
   
df = pd.DataFrame(np.random.randint(low=10000, high=200000, size=(15, 26)), index=years, columns=kantons)
df.style.background_gradient(cmap='Reds')

Pandas has some Builtin Styles for the most common visualization needs. .background_gradient function is a simple way for highlighting cells based on their values. cmap parameter determines the color map based on the matplotlib colormaps.

  •  Tags:  
  • Related