如何在matplotlib中将y-ticks除以一定数量? [英] How to divide y-ticks by a certain number in matplotlib?

查看:45
本文介绍了如何在matplotlib中将y-ticks除以一定数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的matplotlib直方图,我需要将ylabel划分为一定数量.例如,我有100 200和300,而我需要有1,2和3.有什么建议吗?

I have a simple matplotlib histogram and I need to divide ylabels to a certain number. For example I have 100 200 and 300 while I need to have 1,2, and 3. Any suggestion?

这是我的代码:

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)

推荐答案

您似乎不想更改基础数据,这只是格式问题.在这种情况下,您可以使用 formatter-function class 的实例在 ticker 模块中.

It appears that you don't wish to alter the underlying data, and that this is simply a formatting issue. In that case you can use an instance of the formatter-function class found in the ticker module.

格式化程序函数(用于格式化程序功能类的实例)具有两个参数:刻度标签和刻度位置,并返回格式化的刻度标签.以下是您的目的之一:

A formatter function -- for use with an instance of the formatter-function class -- takes two arguments: the tick-label and the tick-position, and returns the formatted tick-label. Below is one for your purpose:

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)

这篇关于如何在matplotlib中将y-ticks除以一定数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆