天天看点

python subplots_python fig,ax = plt.subplots()

python subplots_python fig,ax = plt.subplots()

fig,ax = plt.subplots()

使用该函数确定图的位置,掉用时要XXX=ax.(ax是位置)

等价于:

  • fig = plt.figure()
  • ax = fig.add_subplot(1,1,1)
  • fig 是图像对象,ax 是坐标轴对象

fig, ax = plt.subplots(1,3),其中参数1和3分别代表子图的行数和列数,一共有 1x3 个子图像。函数返回一个figure图像和子图ax的array列表。

  • fig, ax = plt.subplots(1,3,1),最后一个参数1代表第一个子图。

如果想要设置子图的宽度和高度可以在函数内加入figsize值

  • fig, ax = plt.subplots(1,3,figsize=(15,7)),这样就会有1行3个15x7大小的子图。
ax=plt.subplots(m,n,figsize=(a,b)) 画出m*n个字图size为a*b,fig为图片变量,ax为m*n的坐标变量(数组),分别指向相应生成字图的坐标

排列多个子图的步骤:

|.创建多维窗口

fig
           
||:设定各个透视子图在窗口的位置:
data.plot.bar(ax=axes[1,1], color='b', alpha=0.5)  # ax=[1,1] 即位置是第2行、第二列。(python从0开始计数,所以“1”代表第2的)
 
data.plot.barh(ax=axes[0,1], color='k', alpha=0.5) # alpha:设定图表的透明度;
           
|||:再添加画出多个图的类型的代码。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
 
fig, axes = plt.subplots(2, 2)
 
data = pd.Series(np.random.rand(16), index=list('abcdefghijklmnop'))
 
data.plot.bar(ax=axes[1,1], color='b', alpha = 0.5)
data.plot.barh(ax=axes[0,1], color='k', alpha=0.5)
 
plt.show()