[英]Plot something with matplotlib/pyplot and save the plot only (centered, with equal aspect ratio and without axes or borders)
I want to save just the resulting plot image to a file and wrote the following MCVE: 我只想将生成的绘图图像保存到文件中,并编写以下MCVE:
import matplotlib.pyplot as plt
plt.rcParams["axes.facecolor"] = "red"
plt.rcParams["savefig.facecolor"] = "red"
plt.rcParams["savefig.bbox"] = "tight"
plt.rcParams["savefig.pad_inches"] = 0
plt.plot([0, 1], [0, 0], "b-", linewidth="10")
plt.plot([1, 1], [0, 1], "b-", linewidth="10")
plt.plot([1, 0], [1, 1], "b-", linewidth="10")
plt.plot([0, 0], [1, 0], "b-", linewidth="10")
#plt.axis("equal")
#plt.axis("off")
plt.savefig("test.png")
plt.axis("equal")
#plt.axis("off")
#plt.axis("equal")
plt.axis("off")
plt.axis("equal")
plt.axis("off")
I found out about bbox
and pad_inches
. 我发现了有关bbox
和pad_inches
。 However, it's still not perfect. 但是,它仍然不完美。 The axes seem to be hidden, not off completely - so the plot won't be centered. 轴似乎是隐藏的,不是完全关闭的-因此图不会居中。 Additionally, I think I even need the axes to set a equal ratio or the rectangle won't be a square? 另外,我想我什至需要轴设置相等的比例,否则矩形将不是正方形?
What I want is 我想要的是
or 要么
You can define a figure in a square shape and use tight_layout()
to make the padding compact automatically. 您可以定义一个正方形的图形,并使用tight_layout()
使填充自动紧凑。
import matplotlib.pyplot as plt
# Creates a figure object of size 6x6 inches
fig = plt.figure(figsize=(6,6))
plt.rcParams["axes.facecolor"] = "red"
plt.rcParams["savefig.facecolor"] = "red"
plt.rcParams["savefig.bbox"] = "tight"
plt.rcParams["savefig.pad_inches"] = 0
plt.plot([0, 1], [0, 0], "b-", linewidth="10")
plt.plot([1, 1], [0, 1], "b-", linewidth="10")
plt.plot([1, 0], [1, 1], "b-", linewidth="10")
plt.plot([0, 0], [1, 0], "b-", linewidth="10")
plt.axis("equal")
plt.axis("off")
fig.tight_layout()
fig.savefig("test.png")
plt.rcParams["savefig.bbox"] = "tight"
adjusts the padding around the figure automatically. plt.rcParams["savefig.bbox"] = "tight"
自动调整图形周围的填充。 This seems to be undesired here. 这似乎是不希望的。 Leaving that out and creating a square figure with no margins ( plt.subplots_adjust(0,0,1,1)
) will allow to have the axes take all the space in the figure. plt.subplots_adjust(0,0,1,1)
这一点并创建没有边距的方形图形( plt.subplots_adjust(0,0,1,1)
)将允许轴占据图形中的所有空间。 There is then no need to set the aspect specifically any more, but of course for different cases that may still make sense. 这样就无需再专门设置方面了,但是当然对于可能仍然有意义的不同情况。
import matplotlib.pyplot as plt
plt.rcParams["axes.facecolor"] = "red"
plt.rcParams["savefig.facecolor"] = "red"
plt.figure(figsize=(4,4))
plt.subplots_adjust(0,0,1,1)
plt.plot([0, 1, 1, 0,0], [0, 0,1,1,0], "b-", linewidth="10", solid_joinstyle="miter")
#plt.gca().set_aspect("equal",adjustable="box" ) # <- not needed
plt.axis("off")
plt.savefig("test.png")
plt.show()
To obtain the figure without red border, you could set the data margins to 0% ( plt.margins(0)
); 要获得没有红色边框的图形,可以将数据边距设置为0%( plt.margins(0)
); however, because the line is half cut, it makes sense to double its linewidth to obtain the same blue border. 但是,由于该行是半切线,因此将其线宽加倍以获得相同的蓝色边框是有意义的。
import matplotlib.pyplot as plt
plt.rcParams["axes.facecolor"] = "red"
plt.rcParams["savefig.facecolor"] = "red"
plt.figure(figsize=(4,4))
plt.subplots_adjust(0,0,1,1)
plt.plot([0, 1, 1, 0,0], [0, 0,1,1,0], "b-", linewidth=20, solid_joinstyle="miter")
#plt.gca().set_aspect("equal",adjustable="box" ) # <- not needed
plt.axis("off")
plt.margins(0.0)
plt.savefig("test.png")
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.