如何强制 matplotlib 在 Y 轴上仅显示整数 [英] how to force matplotlib to display only whole numbers on the Y axis

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

问题描述

我正在可视化的数据只有在它是整数时才有意义.

The data I am visualising only makes sense if it is whole numbers.

即就我正在分析的信息的上下文而言,记录的 0.2 没有意义.

I.e. 0.2 of a record doesn't make sense in terms of the context of the information I am analysing.

如何强制 matplotlib 在 Y 轴上仅使用整数.IE.1、100、5 等?不是 0.1、0.2 等

How do I force matplotlib to only use whole numbers on the Y axis. I.e. 1, 100, 5 etc? not 0.1, 0.2 etc

for a in account_list:
    f = plt.figure()
    f.set_figheight(20)
    f.set_figwidth(20)
    f.sharex = True
    f.sharey=True

    left  = 0.125  # the left side of the subplots of the figure
    right = 0.9    # the right side of the subplots of the figure
    bottom = 0.1   # the bottom of the subplots of the figure
    top = 0.9      # the top of the subplots of the figure
    wspace = 0.2   # the amount of width reserved for blank space between subplots
    hspace = .8  # the amount of height reserved for white space between subplots
    subplots_adjust(left=left, right=right, bottom=bottom, top=top, wspace=wspace, hspace=hspace)

    count = 1
    for h in headings:
        sorted_data[sorted_data.account == a].ix[0:,['month_date',h]].plot(ax=f.add_subplot(7,3,count),legend=True,subplots=True,x='month_date',y=h)

        #set bottom Y axis limit to 0 and change number format to 1 dec place.
        axis_data = f.gca()
        axis_data.set_ylim(bottom=0.)
        from matplotlib.ticker import FormatStrFormatter
        axis_data.yaxis.set_major_formatter(FormatStrFormatter('%.0f'))

        #This was meant to set Y axis to integer???
        y_formatter = matplotlib.ticker.ScalarFormatter(useOffset=False)
        axis_data.yaxis.set_major_formatter(y_formatter)

        import matplotlib.patches as mpatches

        legend_name = mpatches.Patch(color='none', label=h)
        plt.xlabel("")
        ppl.legend(handles=[legend_name],bbox_to_anchor=(0.,1.2,1.0,.10), loc="center",ncol=2, mode="expand", borderaxespad=0.)
        count = count + 1
        savefig(a + '.png', bbox_inches='tight')

推荐答案

最灵活的方式是将 integer=True 指定为默认刻度定位器(MaxNLocator) 做类似的事情:

The most flexible way is to specify integer=True to the default tick locator (MaxNLocator) do something similar to this:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

fig, ax = plt.subplots()

# Be sure to only pick integer tick locations.
for axis in [ax.xaxis, ax.yaxis]:
    axis.set_major_locator(ticker.MaxNLocator(integer=True))

# Plot anything (note the non-integer min-max values)...
x = np.linspace(-0.1, np.pi, 100)
ax.plot(0.5 * x, 22.8 * np.cos(3 * x), color='black')

# Just for appearance's sake
ax.margins(0.05)
ax.axis('tight')
fig.tight_layout()

plt.show()

或者,您可以按照 Marcin 和 Joel 的建议手动设置刻度位置/标签(或使用 MultipleLocator).这样做的缺点是您需要确定哪些刻度位置有意义,而不是让 matplotlib 根据轴限制选择合理的整数刻度间隔.

Alternatively, you can manually set the tick locations/labels as Marcin and Joel suggest (or use a MultipleLocator). The downside to this is that you need to work out what tick positions make sense, rather than having matplotlib pick a reasonable integer tick interval based on the axis limits.

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

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