简体   繁体   English

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

[英]What is the necessity of plt.figure() in matplotlib?

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')

In the above snippet of code collected fromScatterplot in Matplotlib , what is the necessity of plt.figure() ?在上面从Matplotlib 中的 Scatterplot收集的代码片段中, plt.figure()的必要性是什么?

The purpose of using plt.figure() is to create a figure object.使用plt.figure()的目的是创建一个图形对象。

The whole figure is regarded as the figure object.整个图形被视为图形对象。 It is necessary to explicitly use plt.figure() when we want to tweak the size of the figure and when we want to add multiple Axes objects in a single 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 图的组成部分

It is not always necessary because a figure is implicitly created when you create a scatter plot;它并不总是必要的,因为一个figure ,当你创建一个是隐式创建scatter情节; however, in the case you have shown, the figure is being created explicitly using plt.figure so that the figure will be a specific size rather than the default size.但是,在您显示的情况下,图形是使用plt.figure显式创建的,因此图形将是特定大小而不是默认大小。

The other option would be to use gcf to get the current figure after creating the scatter plot and set the figure size retrospectively:另一种选择是在创建scatter后使用gcf获取当前图形并追溯设置图形大小:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM