1

I have a dataframe z1 with entries as follows:

z1.ix[1:10,1:3]
            2017-04-01  2017-05-01
2017-01-04         NaN    0.993549
2017-01-05         NaN         NaN
2017-01-06    0.830973    0.978463
2017-01-09    0.926456         NaN
2017-01-10    0.998371    0.997590
2017-01-11    0.997539    0.999364
2017-01-12    NaN         0.989801
2017-01-13    0.999701    0.998526
2017-01-16    0.995119    0.998891

Both index and column names are datetime objects.

I am trying to create heat map for the above dataframe so I am doing the following:

from io import StringIO
import pandas as pd
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
from mpl_toolkits.mplot3d import Axes3D

s = StringIO("""
            2017-04-01  2017-05-01
2017-01-04         NaN    0.993549
2017-01-05         NaN         NaN
2017-01-06    0.830973    0.978463
2017-01-09    0.926456         NaN
2017-01-10    0.998371    0.997590
2017-01-11    0.997539    0.999364
2017-01-12    NaN         0.989801
2017-01-13    0.999701    0.998526
2017-01-16    0.995119    0.998891""")
z1 = pd.DataFrame.from_csv(s, sep='\s+')
sns.heatmap(z1, annot=True)

I have the following questions:

(1) By default it uses red and black scale, how do I use green and red scale?

(2) How do I control the display of x and y axes ticks. I want to tilt them at an angle as I have about 100 columns and hence I want to use only 10 ticks aligned at an angle.

(3) How do I control the display of y axis ticks? Right now I am not sure what it displays. I want it displayed in the format '%Y-%m-%d'

1 Answer 1

1

For your first question, you need to use the "cmap" parameter.

For your second question, you need to use set_xticklabels() with the rotation parameter or the set_yticklabels() depending on what axis you want.

For your third question you need to do z1.index.strftime('%Y-%m-%d')

from io import StringIO
import pandas as pd
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
from mpl_toolkits.mplot3d import Axes3D

s = StringIO("""
            2017-04-01  2017-05-01
2017-01-04         NaN    0.993549
2017-01-05         NaN         NaN
2017-01-06    0.830973    0.978463
2017-01-09    0.926456         NaN
2017-01-10    0.998371    0.997590
2017-01-11    0.997539    0.999364
2017-01-12    NaN         0.989801
2017-01-13    0.999701    0.998526
2017-01-16    0.995119    0.998891""")
z1 = pd.DataFrame.from_csv(s, sep='\s+')
# Change the date format of index column
z1.index =  z1.index.strftime('%Y-%m-%d')
g = sns.heatmap(z1, annot=True,cmap="RdYlGn")
g.set_xticklabels(g.get_xticklabels(),rotation=30)

enter image description here

You could try other parameters for cmap based on the colors you want like:

Possible values are: Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r, GnBu, GnBu_r, Greens, Greens_r, Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r, Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, Purples, Purples_r, RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, Set1, Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral, Spectral_r, Vega10, Vega10_r, Vega20, Vega20_r, Vega20b, Vega20b_r, Vega20c, Vega20c_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr, YlOrBr_r, YlOrRd, YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r, binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r, cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, cubehelix, cubehelix_r, flag, flag_r, gist_earth, gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, gist_rainbow, gist_rainbow_r, gist_stern, gist_stern_r, gist_yarg, gist_yarg_r, gnuplot, gnuplot2, gnuplot2_r, gnuplot_r, gray, gray_r, hot, hot_r, hsv, hsv_r, icefire, icefire_r, inferno, inferno_r, jet, jet_r, magma, magma_r, mako, mako_r, nipy_spectral, nipy_spectral_r, ocean, ocean_r, pink, pink_r, plasma, plasma_r, prism, prism_r, rainbow, rainbow_r, rocket, rocket_r, seismic, seismic_r, spectral, spectral_r, spring, spring_r, summer, summer_r, tab10, tab10_r, tab20, tab20_r, tab20b, tab20b_r, tab20c, tab20c_r, terrain, terrain_r, viridis, viridis_r, vlag, vlag_r, winter, winter_r

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.