数据可视化2

图形窗口

from matplotlib import pyplot as plt
plt.figure("title1",facecolor="lightblue")
plt.figure("title2",facecolor="lightgreen")
#名字相同时,只创建一个窗口
#创建两个图形窗口
#代码在谁下面,就在哪个图中画
#plt.title("xxx",fontsize=24)
#plt.xlabel("xxx",fontsize=24)
#plt.ylabel("xxx",fontsize=24)
#plt.grid(linestyle="--")
#plt.tight_layout() 紧凑布局

#再次调用plt.figure("title1",facecolor="lightblue")
#激活当前title1窗口
#继续在里面画图

在这里插入图片描述

子图布局

矩阵式

from matplotlib import pyplot as plt

plt.figure("subplot",facecolor="lightgreen")

for i in range(1,10):
    
    plt.subplot(3,3,i)
    
    plt.text(0.5,0.5,str(i),size=24,ha="center",va="center",alpha=0.7)
    
    plt.xticks([])
    plt.yticks([])
    
    plt.tight_layout()
    
plt.show()

在这里插入图片描述

网格式布局

from matplotlib import pyplot as plt
from matplotlib import gridspec as gs
plt.figure("GridSpec",facecolor="lightgreen")

gs = gs.GridSpec(3,3)

plt.subplot(gs[0,:2])
plt.text(0.5,0.5,"test",size=24,ha="center",va="center",alpha=0.7)
plt.xticks([])
plt.yticks([])
plt.tight_layout()

plt.subplot(gs[1:,0])
plt.text(0.5,0.5,"test",size=24,ha="center",va="center",alpha=0.7)
plt.xticks([])
plt.yticks([])
plt.tight_layout()

plt.subplot(gs[1,1])
plt.text(0.5,0.5,"test",size=24,ha="center",va="center",alpha=0.7)
plt.xticks([])
plt.yticks([])
plt.tight_layout()

plt.subplot(gs[:2,2])
plt.text(0.5,0.5,"test",size=24,ha="center",va="center",alpha=0.7)
plt.xticks([])
plt.yticks([])
plt.tight_layout()

plt.subplot(gs[2,1:])
plt.text(0.5,0.5,"test",size=24,ha="center",va="center",alpha=0.7)
plt.xticks([])
plt.yticks([])
plt.tight_layout()

plt.show()

在这里插入图片描述

自由式布局

from matplotlib import pyplot as plt
plt.figure("ziyou",facecolor="lightblue")

plt.axes([0.02,0.3,0.98,0.74]) #距离窗口左,下的比例,宽,高的比例  of 窗口
plt.text(0.5,0.5,"test",size=24,ha="center",va="center",alpha=0.7)

plt.show()

猜你喜欢

转载自blog.csdn.net/weixin_45228198/article/details/114407323
今日推荐