matplotlib共享轴刻度具有不同的限制 [英] matplotlib shared axis scales with different limits

查看:41
本文介绍了matplotlib共享轴刻度具有不同的限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让两个子图共享它们的 x 轴比例,同时允许它们有不同的限制.

Is there a way to make two subplots that share their x axis scale while allowing them to have different limits.

这是我要实现的目标的图表:

Here is a diagram of what I'm trying to achieve:

(淡淡的竖线表示刻度线处于相同的水平位置,不会出现在最终图形中)

(the faint vertical lines are to indicate that the ticks are in the same horizontal position and will not be in the final graph)

右下角的额外空间将被另一个轴填充.我猜没有简单的方法,但如果有,我将不胜感激!

The extra space in the bottom right will be filled with another axis. I'm guessing there is no easy way, but if there is I'd greatly appreciate it!

推荐答案

好,所以我在matplotlib文档中发现了转换,并且可以正常工作.不完全是微不足道的,但是没关系.此处生成的图形与示例相反:顶部具有短 x 轴.这个概念还可以.

Ok, so I discovered the transforms in the matplotlib docs, and this works. Not exactly trivial, but that's ok. The graph produced here is reversed from the example: the top has the short x-axis. The concept ok though.

import matplotlib.pyplot as plt

short_ax_lim = 4.

f, (top_ax, bottom_ax) = plt.subplots(2,1,figsize=[9,5])

bottom_ax.set_xlim(0,5)
top_ax.set_xlim(0,short_ax_lim)

# first find the pixel where the bottom x-axis equals the desired limit.
short_ax_stop_pix = bottom_ax.transData.transform((short_ax_lim,0))  # (x,y) in pixels at data point (0,4)
short_ax_stop_fig = pix_to_fig.transform(short_ax_stop_pix)  # (x,y) in figure space (0,1)

top_orig_position_px = top_ax.bbox.corners()  # (ll, ul, lr, ur)
top_orig_anchor_px = top_orig_position_px[0]  # this is lower left corner.
top_orig_anchor_fig = pix_to_fig.transform(top_orig_anchor_px)  #convert to figure space
top_x_anchor, top_y_anchor = top_orig_anchor_fig

top_width = short_ax_stop_fig[0] - top_x_anchor
new_pos = (top_x_anchor, top_y_anchor,  top_width, top_ax.get_position().height)

top_ax.set_position(new_pos)
f.show()

这篇关于matplotlib共享轴刻度具有不同的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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