[英]Seaborn: how to set min and max of the x-axis in a heatmap
這是構建我的顏色圖的代碼
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import StrMethodFormatter
import seaborn as sns
import pandas as pd
sns.set(font_scale=5.0, rc={'text.usetex' : True})
sns.set(rc={'figure.figsize':(10.5,9.)})
font = {'family' : 'normal',
'weight' : 'bold',
'size' : 22}
matplotlib.rc('font', **font)
colormap = pd.read_csv('m_colormap_5.csv')
print(colormap.info())
colormapBetaAlpha = colormap[['dihedral', 'scaled_momentum', 'mBetaAlpha']].pivot(index = 'dihedral', columns = 'scaled_momentum', values = 'mBetaAlpha')
colormapBetaAlpha.to_csv('pivot_table_beta_alpha.csv')
g = sns.heatmap(colormapBetaAlpha, xticklabels = 20, yticklabels= 20, cbar_kws={'label': r'$\log(m/$ps$^{-2})$'})
xlabels = ['{:,.2f}'.format(float(x.get_text())) for x in g.get_xticklabels()]
xlabels = ["$" + x + "$" for x in xlabels]
ylabels = ['{:,.2f}'.format(float(x.get_text())) for x in g.get_yticklabels()]
ylabels = ["$" + x + "$" for x in ylabels]
# Setting the font on the colorbar label
g.figure.axes[-1].yaxis.label.set_size(14)
# Setting the font on the colormap ticks
cmap_labels = [x.get_text() for x in g.figure.axes[-1].get_yticklabels()]
g.figure.axes[-1].set_yticklabels(cmap_labels, fontsize = 14)
g.set_xticklabels(xlabels, fontsize = 14)
g.set_yticklabels(ylabels, fontsize = 14)
plt.xlabel(r'$p_{\varphi}/ \sqrt{k_B T g^\prime}$', fontsize = 14)
plt.ylabel(r"$\varphi$", fontsize = 14)
plt.savefig("mDownToUp_height_5.png")
plt.show()
結果很好 但我希望 x 軸的范圍從 -10.0 到 10.0。 我從 matplotlib 中嘗試了很多東西,比如“plt.xlim(-10.0, 10.0)”,但它不能正常工作。 我也嘗試寫下一個硬編碼列表以傳遞給 xlabel,但結果非常難看。 我該怎么做? 謝謝你。
也許你可以試試xticklabels = 21
?
-10 到 10 中間為 0 增加了 21 個刻度。
我假設您的數據從圖中看起來是 +10。 上傳帶有數據的輸入文件會有所幫助。
例如,如果代碼
plt.ylim(-10, 10) 用於地圖被深度更改 [![enter image description here][1]][1] [1]: https://i.stack.imgur.com/zusLA .png
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.