迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Python >

使用 Matplotlib 中的 pyplot.figure()

作者:迹忆客 最近更新:2023/03/17 浏览次数:

本教程解释了我们如何使用 matplotlib.pyplot.fig() 来改变 Matplotlib 图的各种属性。一个 Matplotlib 图只是一个顶层的容器,它包含了一个图的所有轴和属性。要了解更多关于 Matplotlib 图的细节,可以参考官方文档页

使用 matplotlib.pyplot.fig() 来设置图的属性

matplotlib.pyplot.figure(num=None, 
                         figsize=None, 
                         dpi=None, 
                         facecolor=None, 
                         edgecolor=None, 
                         frameon=True, 
                         FigureClass=<class 'matplotlib.figure.Figure'>, 
                         clear=False, 
                         **kwargs)

我们可以使用 matplotlib.pyplot.figure() 来创建一个新的图形,并设置各种参数的值来定制图形,如 figsizedpi 等。

示例:使用 matplotlib.pyplot.figure() 设置图形属性

import matplotlib.pyplot as plt 

x=[1,2,3,4,5,6]
y=[4,3,5,6,7,4]

plt.figure(figsize=(8,6),
           facecolor='yellow')

plt.plot(x, y)
plt.xlabel("X")
plt.ylabel("Y")
plt.title("Plot with figsize (8,6)")
plt.show()

输出:

Matplotlib 用图形方法设置属性

它创建了图形对象,并将人物的宽度设置为 8 英寸,高度设置为 6 英寸。人脸颜色设置为黄色。

使用 matplotlib.pyplot.fig() 像图形添加子图

matplotlib.pyplot.figure() 返回一个图形对象,这个对象可以用来使用 add_subplot() 方法向图中添加子图。

import matplotlib.pyplot as plt 

x=[1,2,3,4,5,6]
y=[4,3,5,6,7,4]

fig=plt.figure()

subplot1=fig.add_subplot(2,1,1)
subplot1.plot(x, y)

subplot2=fig.add_subplot(2,1,2)
subplot2.text(0.3, 0.5, '2nd Subplot')

fig.suptitle("Add subplots to a figure")
plt.show()

输出:

使用 figure 方法在图中添加子图

它为使用 matplotlib.pyplot.fig() 方法创建的图对象 fig 添加了两个子图。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Pandas read_csv()函数

发布时间:2024/04/24 浏览次数:254 分类:Python

Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。

Pandas 追加数据到 CSV 中

发布时间:2024/04/24 浏览次数:352 分类:Python

本教程演示了如何在追加模式下使用 to_csv()向现有的 CSV 文件添加数据。

Pandas 多列合并

发布时间:2024/04/24 浏览次数:628 分类:Python

本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。

Pandas loc vs iloc

发布时间:2024/04/24 浏览次数:837 分类:Python

本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便