Reduce the size of pyplot subplot by scaling

时间:2017-08-05 10:43:05

标签: matplotlib julia

I am using Julia v.0.6.0, with Juno+Atom IDE, and I am trying to make subplots with PyPlot package, v2.3.2. (Pretty new to this)

Consider the following MWE:

using PyPlot

fig = figure("Test subplots",figsize=(9,9)) 
subplot(2,2,1) 
title("Plot 221")
fig[:add_subplot](2,2,2,polar="true")
title("Plot 222") 
fig[:canvas][:draw]() # Update the figure
suptitle("2x2 Subplot",fontsize=15)
tight_layout(pad=2) 

which yields me this:

subplots

Note how the second subplot is too big such that its title is too close to the polar plot.

What I want to achieve is to have the subplot 222 to still take up the same amount of space in the grid, but to have the polar plot scaled down in size, perhaps to 0.9 of its current size. Note that this should not affect the size of rectangular grid in subplot 221 as well.

Is there an argument that I am missing from the matplotlib documentation?

1 个答案:

答案 0 :(得分:1)

这里的主要功能是将任何子绘图轴,标题对象等捕获到' handle'中,以便您可以轻松地单独操作其属性。所以改变你的初始代码是这样的:

using PyPlot
fig = figure("Test subplots",figsize=(9,9)) 
subplot(2,2,1) 
title("Plot 221")
S = subplot(2,2,2,polar="true")               ## captured
T = title("Plot 222")                         ## captured
fig[:canvas][:draw]() # Update the figure
ST = suptitle("2x2 Subplot",fontsize=15)      ## captured
tight_layout(pad=2) 

现在,您可以使用T[:get_verticalalignment]等属性进行检查,并T[:set_verticalalignment]将其设置为" center"," bottom",&# 34;顶部"或"基线" (根据matplotlib documentation)。 E.g。

T[:set_verticalalignment]("bottom")
ST[:set_verticalalignment]("center")

似乎得到了你可能期望的分离量。

或者,为了实现更精细的控制,您可以通过S检查或更改TST[:get_position]的绝对定位(以及隐式大小)和[:set_position]方法。

这些方法通过表示[start_x, start_y, width_x, width_y]的正常数组或Bbox接受,这是上述get方法返回的形式,因此您可能希望从中获取该对象matplotlib:

Bbox = PyPlot.matplotlib[:transforms][:Bbox]

现在你可以做这样的事情:

# inspect the object's position
S[:get_position]()
#> PyObject Bbox([[0.544201388889, 0.517261679293], 
#>                [0.943952721661, 0.917013012065]])

# absolute positioning using 'width' notation
S[:set_position]([0.51, 0.51, 0.4, 0.4])   

# absolute positioning using a 'Bbox' (note 2D array input, not a vector!)
S[:set_position](Bbox([0.51 0.51; 0.89 0.89]))

# 'relative' by adjusting current position, and wrapping back in a Bbox
S[:set_position](Bbox( S[:get_position]()[:get_points]() + [0.1 0.1; -0.1 -0.1]))

# inspect title position
T[:get_position]()
#> (0.5, 1.05)

# raise title a bit higher (manually)
T[:set_position]([0.5, 1.10])