如何在matplotlib中将y轴刻度除以一个特定数字?

5
我是一名能够翻译文本的助手。
我有一个简单的matplotlib直方图,需要将y轴标签分成特定数量。例如,我有100、200和300,而我需要得到1、2和3。您有什么建议吗?
以下是我的代码:
import numpy
import matplotlib
# Turn off DISPLAY
matplotlib.use('Agg')
import pylab

# Figure aspect ratio, font size, and quality
matplotlib.pyplot.figure(figsize=(100,50),dpi=400)
matplotlib.rcParams.update({'font.size': 150})

matplotlib.rcParams['xtick.major.pad']='68'
matplotlib.rcParams['ytick.major.pad']='68'


# Read data from file
data=pylab.loadtxt("data.txt")

# Plot a histogram
n, bins, patches = pylab.hist(data, 50, normed=False, histtype='bar')
#matplotlib.pyplot.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)

# Axis labels
pylab.xlabel('# of Occurence')
pylab.ylabel('Signal Probability')

# Save in PDF file
pylab.savefig("Output.pdf", dpi=400, bbox_inches='tight', pad_inches=1)

请问您能否解释一下您的问题是什么? - Elias Benevedes
1个回答

11

看起来您不希望更改基础数据,这只是一个格式问题。在这种情况下,您可以使用在ticker模块中找到的格式化程序函数类的实例。

用于格式化函数的格式化程序函数类实例需要两个参数:刻度标签和刻度位置,并返回格式化后的刻度标签。以下是适合您目的的格式化程序函数:

def numfmt(x, pos): # your custom formatter function: divide by 100.0
    s = '{}'.format(x / 100.0)
    return s

import matplotlib.ticker as tkr     # has classes for tick-locating and -formatting
yfmt = tkr.FuncFormatter(numfmt)    # create your custom formatter function

# your existing code can be inserted here

pylab.gca().yaxis.set_major_formatter(yfmt)

# final step
pylab.savefig("Output.pdf", dpi=400, bbox_inches='tight', pad_inches=1)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接