简体   繁体   English

多个图的相同 bbox 大小

[英]Same bbox size for multiple plots

I have the issue that I am trying to make multiple plots that are supposed to have the same bbox size.我有一个问题,我试图制作多个应该具有相同 bbox 大小的图。 As some of my plots have an additional colorbar or wider yticklabels the bbox size varies within multiple plots.由于我的一些绘图具有额外的颜色条或更宽的 yticklabels,因此 bbox 大小在多个绘图中会有所不同。

As I would like to use these plots in a LaTex document underneath each other, I would like to set the bbox for all plots to the same value instead of defining the figure size.由于我想在彼此下方的 LaTex 文档中使用这些图,我想将所有图的 bbox 设置为相同的值,而不是定义图形大小。

If it is not clear yet what I mean, here's an example:如果还不清楚我的意思,这里有一个例子:

在此处输入图片说明

As you can see the bbox sizes vary, as the width of the ylabel + ylabelticks and additionally the cbar is added.正如您所看到的,bbox 的大小会有所不同,因为 ylabel + ylabelticks 的宽度以及 cbar 被添加。 I thought the easisest way to approach this would be to find the image of the smallest drawn bbox and use that as a standard for all figures and keep the figsize constant, or to just set the bbox size constant and just add the rest and have varying figsizes.. the later would need me to do additional positioning in latex/illustrator/power point or whatever, but just about any solution that works would be great (even though I belive that the later is likely not possible with matplotlib).我认为解决这个问题的最简单方法是找到最小绘制的 bbox 的图像并将其用作所有图形的标准并保持 figsize 不变,或者只是将 bbox 大小设置为常量,然后添加其余部分并具有不同的figsizes .. 后者需要我在 latex/illustrator/power point 或其他地方做额外的定位,但几乎任何有效的解决方案都会很棒(即使我相信后者可能无法使用 matplotlib)。 I tried changing the bbox size but unfortunately did not succeed.我尝试更改 bbox 大小,但不幸的是没有成功。 So I do not have some code to start from.所以我没有一些代码可以开始。 But any help or pointers where to look at or start would help a lot.但是,任何可以查看或开始的帮助或指示都会有很大帮助。

Here a short code snippet to reproduce.这里有一个简短的代码片段来重现。

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

np.random.seed(1)
mpl.rcParams['figure.figsize'] = (16.0, 12.0)
x = np.linspace(0, 100, 100)
y = np.random.randint(100, size=100)
z = np.random.randint(0, 1e6, size=100)/1e6


fig, ax = plt.subplots()
m = mpl.cm.ScalarMappable(cmap=mpl.cm.jet)
norm = plt.Normalize(min(z), max(z))
m.set_array(list(set(z)))
cbar = plt.colorbar(m, orientation="vertical", fraction=0.07, pad=0.02)
color = lambda c: m.cmap(norm(c))
ax.scatter(x, y, color=color(z))


fig, ax = plt.subplots()
ax.scatter(x, y)

pls see following code.请参阅以下代码。 I recommend you using ax1 and ax2 , which have more flexibility.我建议您使用ax1ax2 ,它们具有更大的灵活性。

Key points:关键点:

  • using get_position() to get bounds of axes.使用get_position()获取轴的边界。
  • using set_position() to set bounds of axes.使用set_position()设置轴的边界。

I highly recommend using ax1, ax2 ... instead of plt.stuff for multiple subplots.我强烈建议对多个子图使用 ax1、ax2 ... 而不是 plt.stuff。

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

np.random.seed(1)
x = np.linspace(0, 100, 100)
y = np.random.randint(100, size=100)
z = np.random.randint(0, 1e6, size=100)/1e6


fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(8, 4))
m = mpl.cm.ScalarMappable(cmap=mpl.cm.jet)
norm = plt.Normalize(min(z), max(z))
m.set_array(list(set(z)))
cbar = fig.colorbar(m, orientation="vertical", fraction=0.07, pad=0.02)
color = lambda c: m.cmap(norm(c))
ax2.scatter(x, y, color=color(z))
ax1.scatter(x, y)

# get the bounds of ax1 and ax2
x1, y1, w1, h1 = ax1.get_position().bounds
x2, y2, w2, h2 = ax2.get_position().bounds
# set ax1 width to width of ax2
ax1.set_position([x1, y1, w2, h1])

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM