在matplotlib中绘制具有固定限制的自动缩放子图 [英] plotting autoscaled subplots with fixed limits in matplotlib

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

问题描述

在matplotlib中制作一系列具有相同X和Y比例尺的子图的最佳方法是什么,但是这些方法是根据具有最极端数据的子图的最小/最大范围来计算的?例如.如果您要绘制一系列直方图,则:

What's the best way in matplotlib to make a series of subplots that all have the same X and Y scales, but where these are computed based on the min/max ranges of the subplot with the most extreme data? E.g. if you have a series of histograms you want to plot:

# my_data is a list of lists
for n, data in enumerate(my_data):
  # data is to be histogram plotted
  subplot(numplots, 1, n+1)
  # make histogram
  hist(data, bins=10)

每个直方图将在X和Y轴上具有不同的范围/刻度.我希望这些设置都相同,并根据绘制的直方图的最极端直方图限制进行设置.一种笨拙的方法是记录每个图的X/Y轴的最小值/最大值,然后在绘制每个子图后遍历每个子图,并在绘制后再遍历它们的轴,但是必须有更好的方法在matplotlib中.

Each histogram will then have different ranges/ticks for the X and Y axis. I'd like these to be all the same and set based on the most extreme histogram limits of the histograms plotted. One clunky way to do it is to record the min/max of X/Y axes for each plot, and then iterate through each subplot once they're plotted and just their axes after they're plotted, but there must be a better way in matplotlib.

是否可以通过一些共享轴的变体来实现?

Can this be achieved by some variant of axes sharing perhaps?

推荐答案

Matplotlib /Pyplot:如何一起放大子图?

http://matplotlib.org/examples/pylab_examples/shared_axis_demo.html

http://matplotlib.org/users/recipes.html

引用最后一个链接:

Fernando Perez提供了一个不错的顶级方法来创建 subplots()(在末尾注明"s")一次全部关闭,然后关闭 x和y共享整个一堆.您可以打开轴的包装 单独地:

Fernando Perez has provided a nice top level method to create in subplots() (note the "s" at the end) everything at once, and turn off x and y sharing for the whole bunch. You can either unpack the axes individually:

# new style method 1; unpack the axes
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex=True, sharey=True)
ax1.plot(x)

或将它们作为支持的numrow x numcolumns对象数组取回 numpy索引:

or get them back as a numrows x numcolumns object array which supports numpy indexing:

# new style method 2; use an axes array
fig, axs = plt.subplots(2, 2, sharex=True, sharey=True)
axs[0,0].plot(x)

如果您使用的是旧版本的matplotlib,则可以使用以下方法(也引用最后一个链接)

If you have an old version of matplotlib the following method should work (also quoting from the last link)

轻松创建子图在matplotlib的早期版本中,如果您 想使用pythonic API并创建一个图形实例,并从 创建可能包含共享轴的子图网格 大量的样板代码.例如

Easily creating subplots In early versions of matplotlib, if you wanted to use the pythonic API and create a figure instance and from that create a grid of subplots, possibly with shared axes, it involved a fair amount of boilerplate code. Eg

# old style
fig = plt.figure()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222, sharex=ax1, sharey=ax1)
ax3 = fig.add_subplot(223, sharex=ax1, sharey=ax1)
ax3 = fig.add_subplot(224, sharex=ax1, sharey=ax1)

这篇关于在matplotlib中绘制具有固定限制的自动缩放子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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