Python Matplotlib.pyplot.tight

您所在的位置:网站首页 fig函数是什么意思 Python Matplotlib.pyplot.tight

Python Matplotlib.pyplot.tight

2024-07-01 14:20| 来源: 网络整理| 查看: 265

Matplotlib是Python中的一个库,它是数字的-NumPy库的数学扩展。 Pyplot是Matplotlib模块的基于状态的接口,该模块提供了MATLAB-like接口。在Pyplot中可以使用各种图,例如线图,轮廓图,直方图,散点图,3D图等。

样例代码

# sample code import matplotlib.pyplot as plt       plt.plot([1, 2, 3, 4], [16, 4, 1, 8])  plt.show() 

输出:

matplotlib.pyplot.tight_layout()函数

matplotlib库的pyplot模块中的tight_layout()函数用于自动调整子图参数以提供指定的填充。

用法: matplotlib.pyplot.tight_layout(pad=1.08, h_pad=None, w_pad=None, rect=None)

参数:此方法接受以下描述的参数:

pad:此参数用于在图形边和子图的边之间进行填充,以字体大小的一部分表示。 h_pad,w_pad:这些参数用于相邻子图的边之间的填充(高度/宽度),作为字体大小的一部分。 rect:此参数是整个子图区域将适合的归一化图形坐标中的矩形。

返回值:此方法不返回任何值。

以下示例说明了matplotlib.pyplot.tight_layout()matplotlib.pyplot中的函数:

示例1:

import numpy as np import matplotlib.pyplot as plt    fig, axs = plt.subplots(1, 2)    x = np.arange(0.0, 2.0, 0.02) y1 = np.sin(2 * np.pi * x) y2 = np.exp(-x) l1, = axs[0].plot(x, y1) l2, = axs[0].plot(x, y2, marker ='o')    y3 = np.sin(4 * np.pi * x) y4 = np.exp(-2 * x) l3, = axs[1].plot(x, y3, color ='tab:green') l4, = axs[1].plot(x, y4, color ='tab:red', marker ='o')    fig.legend((l1, l2), ('Line 1', 'Line 2'), 'upper left') fig.legend((l3, l4), ('Line 3', 'Line 4'), 'upper right')       fig.suptitle('matplotlib.pyplot.tight_layout() Example') plt.tight_layout() plt.show()

输出:

示例2:

import matplotlib.pyplot as plt import numpy as np from matplotlib.ticker import EngFormatter    prng = np.random.RandomState(19680801)    xs = np.logspace(1, 9, 100) ys = (0.8 + 0.4 * prng.uniform(size = 100)) * np.log10(xs)**2    plt.xscale('log')    formatter0 = EngFormatter(unit ='Hz') plt.plot(xs, ys) plt.xlabel('Frequency')    plt.title('matplotlib.pyplot.tight_layout() Example') plt.tight_layout() plt.show()

输出:



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3