如何在matplotlib中将ytics划分为一定数量?

Moj*_*aba 3 python matplotlib

我有一个简单的matplotlib直方图,我需要将ylabel划分为一定数量。例如,我有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)
Run Code Online (Sandbox Code Playgroud)

ber*_*nie 5

看来您不希望更改基础数据,而这仅仅是格式问题。在这种情况下,您可以使用在ticker模块中找到的formatter-function类的实例。

格式化程序功能-用于格式化程序功能类的实例-接受两个参数:刻度标签和刻度位置,并返回格式化的刻度标签。以下是您的目的之一:

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)
Run Code Online (Sandbox Code Playgroud)