Python 在 matplotlib 中更改图形大小和图形格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17109608/
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
change figure size and figure format in matplotlib
提问by golay
I want to obtain fig1 exactly of 4 by 3 inch sized, and in tiff format correcting the program below:
我想获得 fig1 正好是 4 x 3 英寸大小,并以 tiff 格式更正以下程序:
import matplotlib.pyplot as plt
list1 = [3,4,5,6,9,12]
list2 = [8,12,14,15,17,20]
plt.plot(list1, list2)
plt.savefig('fig1.png', dpi = 300)
plt.close()
Any help?
有什么帮助吗?
采纳答案by Francesco Montesano
You can set the figure size if you explicitly create the figure with
如果您使用以下命令明确创建图形,则可以设置图形大小
plt.figure(figsize=(3,4))
You need to set figure size before calling plt.plot()To change the format of the saved figure just change the extension in the file name. However, I don't know if any of matplotlib backends support tiff
您需要在调用之前设置图形大小plt.plot()要更改保存图形的格式,只需更改文件名中的扩展名。但是,我不知道是否有任何 matplotlib 后端支持 tiff
回答by mgilson
The first part (setting the output size explictly) isn't too hard:
第一部分(明确设置输出大小)并不太难:
import matplotlib.pyplot as plt
list1 = [3,4,5,6,9,12]
list2 = [8,12,14,15,17,20]
fig = plt.figure(figsize=(4,3))
ax = fig.add_subplot(111)
ax.plot(list1, list2)
fig.savefig('fig1.png', dpi = 300)
fig.close()
But after a quick google search on matplotlib + tiff, I'm not convinced that matplotlib can make tiffplots. There is some mention of the GDK backendbeing able to do it.
但是在对 matplotlib + tiff 进行快速谷歌搜索之后,我不相信 matplotlib 可以制作tiff绘图。有人提到GDK 后端能够做到这一点。
One option would be to convert the output with a tool like imagemagick's convert.
一种选择是使用像 imagemagick 的convert.
(Another option is to wait around here until a real matplotlib expertshows up and proves me wrong ;-)
(另一种选择是在这里等待,直到真正的 matplotlib 专家出现并证明我错了;-)
回答by ghosh'.
You can change the size of the plot by adding this
您可以通过添加此更改图的大小
plt.rcParams["figure.figsize"] = [16,9]
回答by Antoni
If you need to change the figure size afteryou have created it, use the methods
如果在创建图形后需要更改图形大小,请使用方法
fig = plt.figure()
fig.set_figheight(value_height)
fig.set_figwidth(value_width)
where value_heightand value_widthare in inches. For me this is the most practical way.
wherevalue_height和value_widthare in 英寸。对我来说,这是最实用的方法。

