2017-08-05 139 views
0

我正在使用Julia v.0.6.0和Juno + Atom IDE,我正在尝试使用PyPlot包v.2.3.2创建子图。 (漂亮的新本)通过缩放缩小pyplot子图的大小

考虑以下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) 

这将产生我:

subplots

注二等次要情节是怎么过大,使得它的标题太接近极地情节。

我想要实现的是让子图222仍然占据网格中空间量的相同,但要使极坐标图的大小缩小,或许达到当前大小的0.9。 请注意,这不应该影响子图221中矩形网格的大小。

有没有从matplotlib文档中找不到的说法?

+0

相关:https://stackoverflow.com/a/40665391/4183191 –

+0

也是:https://stackoverflow.com/a/40856517/4183191 –

+0

理想情况下,我更喜欢'subplot'解决方案。也就是说,例如,我可以在* subplot(222)'之后传递一些选项。我不是很喜欢单独指定坐标轴,但是如果没有用于该子图的“缩放”选项,那么我想我必须解决这个问题。我不清楚如何继续用'axes'绘制图表,你能写出一个答案,在附加图像中创建2个子图,但是使用'axes'? – Troy

回答

1

这里最重要的是捕获任何子图轴,标题对象等'处理',以便您可以轻松地单独操纵其属性。因此,改变你的初始代码如下所示:

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]将其设置为“中心”,“底”,“顶”或“基线”之一(根据matplotlib documentation)。例如。

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

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

或者,更精细的控制,可以检查或更改分别绝对定位(和隐含的大小)S,通过他们的[:get_position][:set_position]方法T,或ST

这些方法通过普通阵列表示[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])