使 matplotlib 自动缩放忽略一些图 [英] Make matplotlib autoscaling ignore some of the plots

查看:27
本文介绍了使 matplotlib 自动缩放忽略一些图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 matplotib 的 Axes API 来绘制一些图形.我画的线之一代表理论上的预期线.它没有原始y和x限制的含义.我想要的是让matlplotlib在自动缩放限制时忽略它.我以前做的是检查当前的限制是什么,然后绘制并重置限制.问题在于,当我绘制第三张图时,极限值与理论线一起被重新计算,这确实扩大了图形.

I use matplotib's Axes API to plot some figures. One of the lines I plot represents the theoretical expected line. It has no meaning outside of the original y and x limits. What I want, is for matlplotlib to ignore it when autoscaling the limits. What I used to do, is to check what are the current limits, then plot, and reset the limits. The problem is that when I plot a third plot, the limits get recalculated together with the theoretical line, and that really expands the graph.

# Boilerplate
from matplotlib.figure import Figure
from matplotlib.backends.backend_pdf import FigureCanvasPdf
from numpy import sin, linspace


fig = Figure()
ax = fig.add_subplot(1,1,1)

x1 = linspace(-1,1,100)
ax.plot(x1, sin(x1))
ax.plot(x1, 3*sin(x1))
# I wish matplotlib would not consider the second plot when rescaling
ax.plot(x1, sin(x1/2.0))
# But would consider the first and last

canvas_pdf = FigureCanvasPdf(fig)
canvas_pdf.print_figure("test.pdf")

推荐答案

显而易见的方法是手动将限制设置为您想要的.(例如 ax.axis([xmin,xmax,ymin,ymax]))

The obvious way is to just manually set the limits to what you want. (e.g. ax.axis([xmin, xmax, ymin, ymax]))

如果您不想费心手动找出限制,您有几个选择...

If you don't want to bother with finding out the limits manually, you have a couple of options...

正如一些人(tilsten、Yann 和 Vorticity)所提到的,如果您可以绘制最后要忽略的函数,那么您可以在绘制之前禁用自动缩放或通过 scaley=Falsekwarg 到 plot

As several people (tillsten, Yann, and Vorticity) have mentioned, if you can plot the function you want to ignore last, then you can disable autoscaling before plotting it or pass the scaley=False kwarg to plot

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
x1 = np.linspace(-1,1,100)

ax.plot(x1, np.sin(x1))
ax.plot(x1, np.sin(x1 / 2.0))
ax.autoscale(False)         #You could skip this line and use scalex=False on
ax.plot(x1, 3 * np.sin(x1)) #the "theoretical" plot. It has to be last either way

fig.savefig('test.pdf')

请注意,如果您想控制最后一个绘图,可以调整它的 zorder ,使其绘制在中间".

Note that you can adjust the zorder of the last plot so that it's drawn in the "middle", if you want control over that.

如果您不想依赖订单,并且您只想指定要基于自动缩放的行列表,那么您可以执行以下操作:(注意:这是一个简化版本,假设您'重新处理 Line2D 对象,而不是一般的 matplotlib 艺术家.)

If you don't want to depend on the order, and you do want to just specify a list of lines to autoscale based on, then you could do something like this: (Note: This is a simplified version assuming you're dealing with Line2D objects, rather than matplotlib artists in general.)

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

def main():
    fig, ax = plt.subplots()
    x1 = np.linspace(-1,1,100)

    line1, = ax.plot(x1, np.sin(x1))
    line2, = ax.plot(x1, 3 * np.sin(x1))
    line3, = ax.plot(x1, np.sin(x1 / 2.0))
    autoscale_based_on(ax, [line1, line3])

    plt.show()

def autoscale_based_on(ax, lines):
    ax.dataLim = mtransforms.Bbox.unit()
    for line in lines:
        xy = np.vstack(line.get_data()).T
        ax.dataLim.update_from_data_xy(xy, ignore=False)
    ax.autoscale_view()

if __name__ == '__main__':
    main()

这篇关于使 matplotlib 自动缩放忽略一些图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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