matplotlib中plt.figure()的必要性是什么?

问题描述:

plt.figure(figsize=(10,8)) 

plt.scatter(df['attacker_size'][df['year'] == 298], 
     # attacker size in year 298 as the y axis 
     df['defender_size'][df['year'] == 298], 
     # the marker as 
     marker='x', 
     # the color 
     color='b', 
     # the alpha 
     alpha=0.7, 
     # with size 
     s = 124, 
     # labelled this 
     label='Year 298') 

在上述从Scatterplot in Matplotlib收集的代码片段中,plt.figure()的必要性是什么?matplotlib中plt.figure()的必要性是什么?

使用plt.figure()的目的是创建一个人物对象。

整个图形被视为图形对象。当我们想要调整图形的大小以及何时想在一个图中添加多个Axes对象时,需要明确使用plt.figure()

# in order to modify the size 
fig = plt.figure(figsize=(12,8)) 
# adding multiple Axes objects 
fig, ax_lst = plt.subplots(2, 2) # a figure with a 2x2 grid of Axes 

Parts of a Figure

这并不总是必要的,因为在创建scatter图时隐含创建figure;但是,在您显示的情况下,将使用plt.figure明确创建该数字,以便该数字为特定大小而非默认大小。

另一种选择是使用gcf创建scatter剧情后获得当前的数字和追溯设置数字大小:

# Create scatter plot here 
plt.gcf().set_size_inches(10, 8)