gpt4 book ai didi

python - 如何强制整数刻度标签

转载 作者:IT老高 更新时间:2023-10-28 21:56:53 26 4
gpt4 key购买 nike

我的 python 脚本使用 matplotlib 绘制 x、y、z 数据集的二维“热图”。我的 x 和 y 值代表蛋白质中的氨基酸残基,因此只能是整数。当我放大情节时,它看起来像这样:

2D heat map with float tick marks

正如我所说,x-y 轴上的浮点值对我的数据没有意义,因此我希望它看起来像这样: enter image description here

任何想法如何实现这一目标?这是生成情节的代码:

def plotDistanceMap(self):
# Read on x,y,z
x = self.currentGraph['xData']
y = self.currentGraph['yData']
X, Y = numpy.meshgrid(x, y)
Z = self.currentGraph['zData']
# Define colormap
cmap = colors.ListedColormap(['blue', 'green', 'orange', 'red'])
cmap.set_under('white')
cmap.set_over('white')
bounds = [1,15,50,80,100]
norm = colors.BoundaryNorm(bounds, cmap.N)
# Draw surface plot
img = self.axes.pcolor(X, Y, Z, cmap=cmap, norm=norm)
self.axes.set_xlim(x.min(), x.max())
self.axes.set_ylim(y.min(), y.max())
self.axes.set_xlabel(self.currentGraph['xTitle'])
self.axes.set_ylabel(self.currentGraph['yTitle'])
# Cosmetics
#matplotlib.rcParams.update({'font.size': 12})
xminorLocator = MultipleLocator(10)
yminorLocator = MultipleLocator(10)
self.axes.xaxis.set_minor_locator(xminorLocator)
self.axes.yaxis.set_minor_locator(yminorLocator)
self.axes.tick_params(direction='out', length=6, width=1)
self.axes.tick_params(which='minor', direction='out', length=3, width=1)
self.axes.xaxis.labelpad = 15
self.axes.yaxis.labelpad = 15
# Draw colorbar
colorbar = self.figure.colorbar(img, boundaries = [0,1,15,50,80,100],
spacing = 'proportional',
ticks = [15,50,80,100],
extend = 'both')
colorbar.ax.set_xlabel('Angstrom')
colorbar.ax.xaxis.set_label_position('top')
colorbar.ax.xaxis.labelpad = 20
self.figure.tight_layout()
self.canvas.draw()

最佳答案

这应该更简单:

(来自 https://scivision.co/matplotlib-force-integer-labeling-of-axis/)

import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
#...
ax = plt.figure().gca()
#...
ax.xaxis.set_major_locator(MaxNLocator(integer=True))

关于python - 如何强制整数刻度标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30914462/

26 4 0