【问题标题】:Matplotlib rcparams (autolimit_mode) for single figure单图的 Matplotlib rcparams (autolimit_mode)
【发布时间】:2017-01-19 17:55:42
【问题描述】:

我对新的 Matplotlib 2.0.0 有疑问。

新的 autolimit_mode 值默认为框架添加填充。 我想避免它到一个数字。

如果我更改 rcParams,这些更改会影响任何生成的图形。

我可以为单个图形更改此参数而不影响其余图形的行为吗?


更新

我用一些代码更新问题并获得不同的结果。

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


def remove_frame_border(ax):
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.spines['left'].set_visible(False)
    ax.spines['bottom'].set_visible(False)


def draw1(xserie, yserie):
    fig = plt.figure(figsize=(8,3))
    ax = fig.add_subplot(111)

    ax.plot(xserie, yserie)
    ax.grid(True)
    remove_frame_border(ax)

    fig.savefig('out1.png', format='png', bbox_inches='tight', pad_inches=0)


def draw2(xserie, yserie):
    fig = plt.figure(figsize=(8,3))
    ax = fig.add_subplot(111)

    # Fixed autolimit padding in matplotlib 2.0.0
    ax.set_xmargin(0)
    ax.set_ymargin(0)
    ax.autoscale_view()

    ax.plot(xserie, yserie)
    ax.grid(True)
    remove_frame_border(ax)

    # Restore default matplotlib params
    matplotlib.rcParams.update(matplotlib.rcParamsDefault)

    fig.savefig('out2.png', format='png', bbox_inches='tight', pad_inches=0)


def draw3(xserie, yserie):
    # Fixed autolimit padding in matplotlib 2.0.0
    matplotlib.rcParams['axes.autolimit_mode'] = 'round_numbers'
    matplotlib.rcParams['axes.xmargin'] = 0
    matplotlib.rcParams['axes.ymargin'] = 0

    fig = plt.figure(figsize=(8,3))
    ax = fig.add_subplot(111)

    ax.plot(xserie, yserie)
    ax.grid(True)
    remove_frame_border(ax)

    # Restore default matplotlib params
    matplotlib.rcParams.update(matplotlib.rcParamsDefault)

    fig.savefig('out3.png', format='png', bbox_inches='tight', pad_inches=0)



xserie = np.array([1,2,3,4,5,6,7,8,9,10])
yserie = np.array([0, 22.2, 33.4, 55.6, 66.6, 77.7, 100.3, 123.3, 90.4, 93.3])

draw1(xserie, yserie)
draw2(xserie, yserie)
draw3(xserie, yserie)
  • draw1 是默认行为
  • draw2 调用 ax.set_xmargin 和 ax.set_ymargin
  • draw3更改rcParams并在生成图表后恢复

out1.png

out2.png

out3.png

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    Matplotlib 提供方法matplotlib.axes.Axes.set_xmargin

    在自动缩放之前设置 X 数据限制的填充。 m 乘以数据 间隔将在使用之前添加到该间隔的每一端 在自动缩放中。

    所以设置

    ax.set_xmargin(0) 
    

    删除填充。

    但是,这似乎有效果,还需要调用 matplotlib.axes.Axes.autoscale_view

    ax.autoscale_view() 
    

    我没有完全理解autoscale_view 的文档字符串,但这里的组合似乎有效。

    【讨论】:

    • 什么是“最后的网格线”?在您的问题中没有网格线。在不知道代码或图像或非常详细的问题描述的情况下,几乎不可能讨论一些不受欢迎的行为。
    • 我已经用一些代码和示例更新了我的第一篇文章。最后一条网格线是指网格中的最后一条刻度线。如果我删除框架边框,我会错过图表中的 y =140 和 x = 10 线
    • x=10 处没有网格线,因为它已经在轴之外。如果你不移除所有的刺,你就不会看到这一点。现在您当然可以根据需要设置限制,例如ax.set_xlim((1,10.01)) 或将边距设置得更高一点,以包括最右边的网格线,ax.set_xmargin(0.001)。然而,这些解决方案可能在很大程度上取决于您绘制的数据。
    • 谢谢!我会尝试做一个辅助功能来根据数据调整“额外边距”。我可以使用 ax.get_yticks() 中的刻度频率并将上限设置为:ticks = ax.get_yticks(); max_tick = np.max(ticks); freq = ticks[1] - ticks[0]; upper_limit = max_tick + freq if np.max(yserie) > max_tick else max_tick
    猜你喜欢
    • 2020-11-07
    • 2022-12-13
    • 1970-01-01
    • 1970-01-01
    • 2016-10-10
    • 2018-06-27
    • 1970-01-01
    • 2015-11-27
    • 1970-01-01
    相关资源
    最近更新 更多