Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

python - making colorbar with scientific notation in seaborn

I am plotting the following heatmap in seaborn. the dataframe is read from the foll. csv file: https://www.dropbox.com/s/mb3wc8mmis0m7g6/df_trans.csv?dl=0

ax = sns.heatmap(df, linewidths=.1, linecolor='gray', cmap=sns.cubehelix_palette(light=1, as_cmap=True))
locs, labels = plt.xticks()
plt.setp(labels, rotation=0)
locs, labels = plt.yticks()
plt.setp(labels, rotation=0)

How can I modify the colorbar numbers so that they 160000 shows up as 1.6 with a 10^5 on top of colorbar. I know hot to do this in matplotlib but not in seaborn:

import matplotlib.ticker as tkr
formatter = tkr.ScalarFormatter(useMathText=True)
formatter.set_scientific(True)
ax.yaxis.set_major_formatter(formatter)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Pass your formatter object through the cbar_kws:

import numpy as np
import seaborn as sns
import matplotlib.ticker as tkr

formatter = tkr.ScalarFormatter(useMathText=True)
formatter.set_scientific(True)
formatter.set_powerlimits((-2, 2))

x = np.exp(np.random.uniform(size=(10, 10)) * 10)
sns.heatmap(x, cbar_kws={"format": formatter})

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...