Python 如何使用子图更改图形大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14770735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I change the figure size with subplots?
提问by Brian
I came across this examplein the Matplotlib website. I was wondering if it was possible to increase the figure size.
我在 Matplotlib 网站上遇到了这个例子。我想知道是否可以增加图形大小。
I tried with
我试过
f.figsize(15,15)
but it does nothing.
但它什么也不做。
采纳答案by Rutger Kassies
If you already have the figure object use:
如果您已经拥有图形对象,请使用:
f.set_figheight(15)
f.set_figwidth(15)
But if you use the .subplots() command (as in the examples you're showing) to create a new figure you can also use:
但是,如果您使用 .subplots() 命令(如您展示的示例中所示)创建一个新图形,您还可以使用:
f, axs = plt.subplots(2,2,figsize=(15,15))
回答by aquirdturtle
Alternatively, create a figure()object using the figsizeargument and then use add_subplotto add your subplots. E.g.
或者,figure()使用figsize参数创建一个对象,然后用于add_subplot添加子图。例如
import matplotlib.pyplot as plt
import numpy as np
f = plt.figure(figsize=(10,3))
ax = f.add_subplot(121)
ax2 = f.add_subplot(122)
x = np.linspace(0,4,1000)
ax.plot(x, np.sin(x))
ax2.plot(x, np.cos(x), 'r:')
Benefits of this method are that the syntax is closer to calls of subplot()instead of subplots(). E.g. subplots doesn't seem to support using a GridSpecfor controlling the spacing of the subplots, but both subplot()and add_subplot()do.
这种方法的好处是语法更接近于调用subplot()而不是subplots()。例如,次要情节似乎没有使用支持GridSpec用于控制次要情节的间距,但都subplot()和add_subplot()做的。


