当前位置:   article > 正文

python plt.subplot_Python Matplotlib.pyplot.subplot()用法及代码示例

plt.subplot

subplot()功能将子图添加到指定网格位置处的当前图形。它类似于subplots()函数,但是与subplots()不同,它一次添加一个子图。因此,要创建多个图,您将需要使用subplot()函数的几行代码。子图功能的另一个缺点是,它会删除图形上先前存在的图。请参考示例1。

它是Figure.add_subplot的包装。

用法:

子图(nrows,ncols,index,** kwargs)

子图(pos,** kwargs)

子图(ax)

Parameters:

args:3位整数或三个单独的整数描述子图的位置。

pos是一个three-digit整数,其中第一个,第二个和第三个整数是nrows,ncols,index。

projection:[{None,“ aito ff”,“ hammer”,“ lambert”,“ mollweide”,“ polar”,“ rectalinear”,str},可选)。子图的projection-type(轴)。默认值为“无”会导致“直线”投影。

label:[str]返回轴的标签。

**kwargs:此方法还为返回的轴基类采用关键字参数。

除了数字参数,例如facecolor。

返回值:轴的axes.SubplotBase子类或轴的子类。返回的轴基类取决于所使用的投影。

该功能的实现如下:

范例1:subplot()将删除先前存在的绘图。

Python3

# importing hte module

import matplotlib.pyplot as plt

# Data to display on plot

x = [1, 2, 3, 4, 5]

y = [1, 2, 1, 2, 1]

# plot() will create new figure and will add axes object (plot) of above data

plt.plot(x, y, marker="x", color="green")

# subplot() will add plot to current figure deleting existing plot

plt.subplot(121)

输出:我们可以看到,第一个绘图被subplot()功能搁置了。

96bb371600b8204f8f44437e25bdf5f3.png

subplot_gfg

如果要查看第一幅图,请注释出plt.subplot()行,您将看到以下图

a6f64e853cde08d30e34b122e1166d83.png

plot_gfg

范例2:

Python3

import matplotlib.pyplot as plt

# data to display on plots

x = [3, 1, 3]

y = [3, 2, 1]

z = [1, 3, 1]

# Creating figure object

plt.figure()

# addind first subplot

plt.subplot(121)

plt.plot(x, y, color="orange", marker="*")

# addding second subplot

plt.subplot(122)

plt.plot(z, y, color="yellow", marker="*")

输出:

5c16686d1a48242fcafd41f433cd5e16.png

multiple_subplots

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/896967
推荐阅读
相关标签
  

闽ICP备14008679号