Python matplotlib 获取 ylim 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26131607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:07:55  来源:igfitidea点击:

matplotlib get ylim values

pythonmatplotlibplot

提问by synaptik

I'm using matplotlibto plot data (using plotand errorbarfunctions) from Python. I have to plot a set of totally separate and independent plots, and then adjust their ylimvalues so they can be easily visually compared.

我正在使用Pythonmatplotlib绘制数据(使用ploterrorbar函数)。我必须绘制一组完全独立和独立的图,然后调整它们的ylim值,以便可以轻松地直观地比较它们。

How can I retrieve the ylimvalues from each plot, so that I can take the min and max of the lower and upper ylim values, respectively, and adjust the plots so they can be visually compared?

如何ylim从每个图中检索值,以便我可以分别取下 ylim 值和上限 ylim 值的最小值和最大值,并调整图以便可以直观地比较它们?

Of course, I could just analyze the data and come up with my own custom ylimvalues... but I'd like to use matplotlibto do that for me. Any suggestions on how to easily (and efficiently) do this?

当然,我可以只分析数据并提出我自己的自定义ylim值……但我想用它matplotlib来为我做这件事。关于如何轻松(和有效)做到这一点的任何建议?

Here's my Python function that plots using matplotlib:

这是我的 Python 函数,它使用matplotlib以下方式绘制:

import matplotlib.pyplot as plt

def myplotfunction(title, values, errors, plot_file_name):

    # plot errorbars
    indices = range(0, len(values))
    fig = plt.figure()
    plt.errorbar(tuple(indices), tuple(values), tuple(errors), marker='.')

    # axes
    axes = plt.gca()
    axes.set_xlim([-0.5, len(values) - 0.5])
    axes.set_xlabel('My x-axis title')
    axes.set_ylabel('My y-axis title')

    # title
    plt.title(title)

    # save as file
    plt.savefig(plot_file_name)

    # close figure
    plt.close(fig)

采纳答案by elyase

Just use axes.get_ylim(), it is very similar to set_ylim. From the docs:

刚用axes.get_ylim(),就很像了set_ylim。从文档

get_ylim()

Get the y-axis range [bottom, top]

get_ylim()

获取y轴范围[底部,顶部]

回答by Adam Hughes

 ymin, ymax = axes.get_ylim()

If you are using the pltapi directly, you can avoid calls to the axes altogether:

如果您plt直接使用api,则可以完全避免调用轴:

def myplotfunction(title, values, errors, plot_file_name):

    # plot errorbars
    indices = range(0, len(values))
    fig = plt.figure()
    plt.errorbar(tuple(indices), tuple(values), tuple(errors), marker='.')

    plt.xlim([-0.5, len(values) - 0.5])
    plt.xlabel('My x-axis title')
    plt.ylabel('My y-axis title')

    # title
    plt.title(title)

    # save as file
    plt.savefig(plot_file_name)

   # close figure
    plt.close(fig)

回答by Thom Ives

Leveraging from the good answers above and assuming you were only using plt as in

利用上面的好答案并假设您只使用 plt 作为

import matplotlib.pyplot as plt

then you can get all four plot limits using plt.axis()as in the following example.

然后您可以使用plt.axis()以下示例中的所有四个绘图限制。

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5, 6, 7, 8]  # fake data
y = [1, 2, 3, 4, 3, 2, 5, 6]

plt.plot(x, y, 'k')

xmin, xmax, ymin, ymax = plt.axis()

s = 'xmin = ' + str(round(xmin, 2)) + ', ' + \
    'xmax = ' + str(xmax) + '\n' + \
    'ymin = ' + str(ymin) + ', ' + \
    'ymax = ' + str(ymax) + ' '

plt.annotate(s, (1, 5))

plt.show()

The above code should produce the following output plot. enter image description here

上面的代码应该产生以下输出图。 在此处输入图片说明