简体   繁体   English

Seaborn:如何在热图中设置 x 轴的最小值和最大值

[英]Seaborn: how to set min and max of the x-axis in a heatmap

this is the code to build my colormap这是构建我的颜色图的代码

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()

The result is nice结果很好在此处输入图片说明 but I would like the x axis range from -10.0 to 10.0.但我希望 x 轴的范围从 -10.0 到 10.0。 I have tried many things from matplotlib, like "plt.xlim(-10.0, 10.0)" but it does not work properly.我从 matplotlib 中尝试了很多东西,比如“plt.xlim(-10.0, 10.0)”,但它不能正常工作。 Also I have tried to write down an hard coded list to pass to the xlabel, but the result is quite ugly.我也尝试写下一个硬编码列表以传递给 xlabel,但结果非常难看。 How can I do it?我该怎么做? Thank you.谢谢你。

Maybe you can try xticklabels = 21 ?也许你可以试试xticklabels = 21

-10 to 10 with 0 in the middle adds to 21 ticks. -10 到 10 中间为 0 增加了 21 个刻度。

I am assuming your data goes to +10 as it seems to from the plot.我假设您的数据从图中看起来是 +10。 Uploading the input file with the data would help.上传带有数据的输入文件会有所帮助。

For example, if the code例如,如果代码

plt.ylim(-10, 10) is used the map is deeply alter as reported [![enter image description here][1]][1] [1]: https://i.stack.imgur.com/zusLA.png 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM