如何在Matplotlib中强制Y轴仅使用整数? [英] How to force the Y axis to only use integers in Matplotlib?

查看:118
本文介绍了如何在Matplotlib中强制Y轴仅使用整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用matplotlib.pyplot模块绘制直方图,我想知道如何强制y轴标签仅显示整数(例如0、1、2、3等)而不显示小数(例如0) .,0.5、1,.1.5、2等).

I'm plotting a histogram using the matplotlib.pyplot module and I am wondering how I can force the y-axis labels to only show integers (e.g. 0, 1, 2, 3 etc.) and not decimals (e.g. 0., 0.5, 1., 1.5, 2. etc.).

我正在查看指导说明,怀疑答案在 matplotlib.pyplot周围. ylim ,但到目前为止,我只能找到设置最小和最大y轴值的内容.

I'm looking at the guidance notes and suspect the answer lies somewhere around matplotlib.pyplot.ylim but so far I can only find stuff that sets the minimum and maximum y-axis values.

def doMakeChart(item, x):
    if len(x)==1:
        return
    filename = "C:\Users\me\maxbyte3\charts\\"
    bins=logspace(0.1, 10, 100)
    plt.hist(x, bins=bins, facecolor='green', alpha=0.75)
    plt.gca().set_xscale("log")
    plt.xlabel('Size (Bytes)')
    plt.ylabel('Count')
    plt.suptitle(r'Normal Distribution for Set of Files')
    plt.title('Reference PUID: %s' % item)
    plt.grid(True)
    plt.savefig(filename + item + '.png')
    plt.clf()

推荐答案

如果您有y数据

y = [0., 0.5, 1., 1.5, 2., 2.5]

您可以使用此数据的最大值和最小值来创建此范围内的自然数列表.例如,

You can use the maximum and minimum values of this data to create a list of natural numbers in this range. For example,

import math
print range(math.floor(min(y)), math.ceil(max(y))+1)

收益

[0, 1, 2, 3]

然后您可以使用 matplotlib.pyplot.yticks :

yint = range(min(y), math.ceil(max(y))+1)

matplotlib.pyplot.yticks(yint)

这篇关于如何在Matplotlib中强制Y轴仅使用整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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