2016-01-24 40 views
0

我的目标是创建一些matplotlib图,然后将它们排列成一个大阵列。为此,我需要确保matplotlib图的输出图像是方形的。如何才能做到这一点?在Python中,如何使用matplotlib保存方形图像?

这里是我的代码段:

figure = matplotlib.pyplot.figure() 
figure.suptitle(label, fontsize = 20) 
#figure.set_size_inches(19.7, 19.7) 
matplotlib.pyplot.scatter(
    variable_1_values, 
    variable_2_values, 
    s   = marker_size, 
    c   = "#000000", 
    edgecolors = "none", 
    label  = label, 
) 
matplotlib.pyplot.xlabel(label_x) 
matplotlib.pyplot.ylabel(label_y) 
legend = matplotlib.pyplot.legend(
    loc   = "center left", 
    bbox_to_anchor = (1, 0.5), 
    fontsize  = 10 
) 
print("save {filename}".format(filename = filename)) 
matplotlib.pyplot.savefig(
    filename, 
    bbox_extra_artists = (legend,), 
    bbox_inches  = "tight", 
    dpi    = 700 
) 

我知道如何设置的身影广场,我需要的是设置方输出图像。这在概念上与采用默认输出图像一样简单,然后根据需要扩展背景以使图像平滑。

+0

不使用'bbox_inches ='tight''这个'收缩包裹'保存的图形到图中艺术家的边界框。 – tacaswell

+0

@tcaswell嘿,谢谢你的建议。我试了一下,看起来似乎有帮助(图像看起来与图形的长宽比相匹配),但后来我遇到了轴标签超出图像边界(http:// i。 imgur.com/Obw8Hfg.png)。你会对如何解决这个问题有任何想法吗?我正在处理大量图像,所以我无法单独更改轴号码的格式。 – d3pd

+0

'figure.tight_layout()'(重新排列/调整轴的大小) – tacaswell

回答

0

不知道这是你在找什么,但它为我产生一个600x600像素的PNG图像。

import numpy as np 
import matplotlib.pyplot as plt 

a = np.random.normal(size=1000) 
b = np.random.normal(size=1000) 

fig = plt.figure(figsize=(6,6)) 

plt.plot(a,b,'g.') 
plt.xlabel("x axis label") 
plt.ylabel("y axis label") 
plt.title("Random numbers in a scatter plot") 

fig.savefig("test.png") 
相关问题