python matplotlib 2.x轴自动限制 [英] python matplotlib 2.x axis autolimit

查看:239
本文介绍了python matplotlib 2.x轴自动限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在选择轴上的自动限制时,matplotlib 2.x代码出现问题,无法重现1.x行为.根据 https://matplotlib.org/users/dflt_style_changes.html 的版本1.x.轴倒圆应使用以下命令来复制:

I've got a problem with matplotlib 2.x code not reproducing 1.x behaviour, when it comes to choosing the auto-limits on the axes. According to https://matplotlib.org/users/dflt_style_changes.html , version 1.x axis rounding should be reproduced by the using the commands:

mpl.rcParams['axes.autolimit_mode'] = 'round_numbers'
mpl.rcParams['axes.xmargin'] = 0
mpl.rcParams['axes.ymargin'] = 0

但事实并非如此.请参见下面的代码.我的y值范围从刚好高于5到刚好低于50.运行代码时:

But it doesn't. See the code below. My y values range from just above 5 to just below 50. When I run the code:

  • 具有经典(1.x版)行为(sys.argv [1] =="1"),y轴刻度分别为5和50.
  • 具有2.x版行为(sys.argv [1] =="2"),y轴刻度为0和60.

2.x版的四舍五入看起来更粗糙.如何复制1.x版本的舍入?

Version 2.x rounding appears to be coarser. How do I reproduce version 1.x rounding?

import sys
import matplotlib.pyplot as plt
import matplotlib as mpl

if sys.argv[1]=="1":
    import matplotlib.style
    matplotlib.style.use('classic')
elif sys.argv[1]=="2":
    mpl.rcParams['axes.autolimit_mode'] = 'round_numbers'
    mpl.rcParams['axes.xmargin'] = 0
    mpl.rcParams['axes.ymargin'] = 0
else:
    raise Exception("param must be 1 or 2")

z = [49.0, 14.5, 6.0, 5.8]
steps = len(z)

plt.clf()
plt.figure(figsize=(8,6)) # Matplotlib 1.x default
plt.subplot(311)
plt.plot(range(steps), z)

plt.xlim(xmin=0, xmax=steps-1)
plt.xticks(range(steps))
ymin, ymax = plt.ylim()
plt.yticks([ymin, ymax])

plt.savefig("mpltest%s.pdf" % sys.argv[1])

推荐答案

首先,请注意,经典模式和改编的rcParams之间的坐标轴刻度数不同.

First, note that the number of ticks on the axes is different between the classic mode and the adapted rcParams.

import matplotlib.pyplot as plt

mode = "classic" #"classic" #"modern"
if mode == "classic":
    plt.style.use('classic')
else:
    plt.rcParams['axes.autolimit_mode'] = 'round_numbers'
    plt.rcParams['axes.xmargin'] = 0
    plt.rcParams['axes.ymargin'] = 0

z = [49.0, 14.5, 6.0, 5.8]

plt.figure(figsize=(3,6))
plt.subplot(311)
plt.title("{} mode".format(mode))
plt.plot(range(len(z)), z)

由于滴答声的数量不同,因此下一个整数"也不同.在经典模式下,它是550,在现代"模式下是060.

Because of that different number of ticks, also the next "round number" is different. In classic mode it is 5 and 50, in "modern" mode 0 and 60.

对默认指南的更改在数字的滴答声"部分:

The changes to the defaults guide states in the "number of ticks" section:

现在,定位器包括一种算法,可估计将为刻度标签留出空间的最大刻度数. [...]除了使用mpl.style.use('classic')之外,没有其他方法可以将以前的行为恢复为默认值. [...] MaxNLocator使用的算法已得到改进,在某些情况下这可能会更改刻度位置的选择 .这也会影响AutoLocator,它在内部使用MaxNLocator.

The locator now includes an algorithm to estimate the maximum number of ticks that will leave room for the tick labels. [...] There is no way, other than using mpl.style.use('classic'), to restore the previous behavior as the default. [...] The algorithm used by MaxNLocator has been improved, and this may change the choice of tick locations in some cases. This also affects AutoLocator, which uses MaxNLocator internally.

在这里,您碰巧"遇到了其中一种.

Here, you have run into exactly one of those "in some cases".

这篇关于python matplotlib 2.x轴自动限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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