python - Matplotlib: How to force integer tick labels? -
my python script uses matplotlib plot 2d "heat map" of x, y, z dataset. x- , y-values represent amino acid residues in protein , can therefore integers. when zoom plot, looks this:

as said, float values on x-y axes not make sense data , therefore want this: 
any ideas how achieve this? code generates plot:
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()
this should simpler:
(from https://scivision.co/matplotlib-force-integer-labeling-of-axis/)
import matplotlib.pyplot plt matplotlib.ticker import maxnlocator #... ax = plt.figure().gca() #... ax.xaxis.set_major_locator(maxnlocator(integer=true))
Comments
Post a Comment