问题描述
我正在使用matplotlib
从Python绘制数据(使用plot
和errorbar
函数).我必须绘制一组完全独立的图,然后调整它们的ylim
值,以便可以在视觉上轻松进行比较.
I'm using matplotlib
to plot data (using plot
and errorbar
functions) from Python. I have to plot a set of totally separate and independent plots, and then adjust their ylim
values so they can be easily visually compared.
如何从每个图上检索ylim
值,以便分别取下和上ylim值的最小值和最大值,并调整图以进行直观比较?
How can I retrieve the ylim
values 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
值...但是我想使用matplotlib
替我做.关于如何轻松(高效)做到这一点的任何建议?
Of course, I could just analyze the data and come up with my own custom ylim
values... but I'd like to use matplotlib
to do that for me. Any suggestions on how to easily (and efficiently) do this?
这是我的Python函数,它使用matplotlib
进行绘制:
Here's my Python function that plots using 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)
推荐答案
只需使用axes.get_ylim()
,它与set_ylim
非常相似.从文档:
Just use axes.get_ylim()
, it is very similar to set_ylim
. From the docs:
获取y轴范围[底部,顶部]
Get the y-axis range [bottom, top]
这篇关于matplotlib获取ylim值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!