使用 Matplotlib 中的 pyplot.figure()
本教程解释了我们如何使用 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()
来创建一个新的图形,并设置各种参数的值来定制图形,如 figsize
,dpi
等。
示例:使用 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()
输出:
它创建了图形对象,并将人物的宽度设置为 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()
输出:
它为使用 matplotlib.pyplot.fig()
方法创建的图对象 fig
添加了两个子图。
相关文章
Pandas DataFrame DataFrame.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:Python
-
DataFrame.shift() 函数是将 DataFrame 的索引按指定的周期数进行移位。
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
Pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:Python
-
Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。
Pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:Python
-
本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。
Pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:Python
-
本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串