Figure()函数的参数及其用法
发布时间:2023-12-26 10:43:57
Figure()函数是matplotlib库中的一个函数,用于创建一个新的图形窗口或画布。它可以接受一些参数来定义图形的一些特性和属性。
参数及其用法如下:
1. num: 指定图形的编号。如果没有指定,则默认为None,表示自动分配一个编号。可以使用plt.figure(num)来获取该图形。
import matplotlib.pyplot as plt fig1 = plt.figure(1) ax1 = fig1.add_subplot(111) ax1.plot([1, 2, 3, 4, 5], [1, 2, 3, 4, 5]) fig2 = plt.figure(2) ax2 = fig2.add_subplot(111) ax2.plot([1, 2, 3, 4, 5], [5, 4, 3, 2, 1]) plt.show()
2. figsize: 指定图形的宽度和高度,以英寸为单位。它接受一个元组 (width, height) 或一个浮点数倍数。默认值为(6.4, 4.8)。
import matplotlib.pyplot as plt fig = plt.figure(figsize=(8, 6)) ax = fig.add_subplot(111) ax.plot([1, 2, 3, 4, 5], [1, 2, 3, 4, 5]) plt.show()
3. dpi: 指定图形的分辨率,即每英寸多少个像素。默认值为100。
import matplotlib.pyplot as plt fig = plt.figure(dpi=200) ax = fig.add_subplot(111) ax.plot([1, 2, 3, 4, 5], [1, 2, 3, 4, 5]) plt.show()
4. facecolor: 指定图形的背景色。可以使用颜色名称或RGB值表示。默认值为'white'。
import matplotlib.pyplot as plt fig = plt.figure(facecolor='lightgray') ax = fig.add_subplot(111) ax.plot([1, 2, 3, 4, 5], [1, 2, 3, 4, 5]) plt.show()
5. edgecolor: 指定图形的边框颜色。可以使用颜色名称或RGB值表示。默认值为'white'。
import matplotlib.pyplot as plt fig = plt.figure(edgecolor='black') ax = fig.add_subplot(111) ax.plot([1, 2, 3, 4, 5], [1, 2, 3, 4, 5]) plt.show()
6. linewidth: 指定边框的宽度。默认值为0.0。
import matplotlib.pyplot as plt fig = plt.figure(edgecolor='black', linewidth=1.0) ax = fig.add_subplot(111) ax.plot([1, 2, 3, 4, 5], [1, 2, 3, 4, 5]) plt.show()
7. frameon: 指定是否显示图形的边框。默认值为True。
import matplotlib.pyplot as plt fig = plt.figure(frameon=False) ax = fig.add_subplot(111) ax.plot([1, 2, 3, 4, 5], [1, 2, 3, 4, 5]) plt.show()
8. clear: 指定在创建新图形之前是否清空当前图形。默认值为False。
import matplotlib.pyplot as plt fig1 = plt.figure() ax1 = fig1.add_subplot(111) ax1.plot([1, 2, 3, 4, 5], [1, 2, 3, 4, 5]) fig2 = plt.figure(clear=True) ax2 = fig2.add_subplot(111) ax2.plot([1, 2, 3, 4, 5], [5, 4, 3, 2, 1]) plt.show()
这些参数可以根据需要进行调整,以定制化地创建图形。使用合适的参数值,可以使图形呈现出更好的效果和样式。
