如何在 matplotlib 中为一个图形设置本地 rcParams 或 rcParams?
matplotlibpythondata visualization更新于 2024/3/1 9:16:17
要在 matplotlib 中为一个图形设置本地 rcParams 或 rcParams,我们可以采取以下步骤 −
步骤
设置图形大小并调整子图之间和周围的填充。
初始化变量 N 以存储样本数据的数量。
使用 numpy 创建 x 和 y 数据点。
返回一个上下文管理器以临时更改 rcParams。
在索引 1 处向当前图形添加子图。
绘制x 和 y 数据点,使用 plot() 方法。
在索引 2 处向当前图形添加子图。
使用 plot() 方法绘制 x 和 y 数据点。
要显示图形,请使用 show() 方法。
示例
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
N = 10
x = np.random.rand(N)
y = np.random.rand(N)
with plt.rc_context({"axes.grid": True, "grid.linewidth": 0.75, "lines.linestyle": 'dashed'}):
plt.subplot(121)
plt.plot(x, y)
plt.subplot(122)
plt.plot(x, y)
plt.show()
输出
它将产生以下输出 −

相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

