随笔 - 384  文章 - 0  评论 - 35  阅读 - 142万

plt.figure() 和plt.subplot() 的用法

一、plt.figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True) 

方便连续画几个图片

参数说明:

1.num:图像编码或者名称,数字是编码,字符串是名称

2.figsize:宽和高,单位是英尺

3.dpi:指定绘图对象的分辨率,即每英寸多少个像素,缺省值为80 

4.facecolor:背景颜色

5.edgecolor:边框颜色

6.frameon:是否显示边框

例子:

复制代码
import warnings
warnings.filterwarnings('ignore')

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import scipy.stats as st
Train_data =pd.read_csv('F:\\python\\天池_二手车交易价格预测\\used_car_train_20200313.csv',sep=' ')
y = Train_data['price']
plt.figure(1); plt.title('Johnson SU')
sns.distplot(y, kde=False, fit=st.johnsonsu)
plt.figure(2); plt.title('Normal')
sns.distplot(y, kde=False, fit=st.norm)
plt.figure(3); plt.title('Log Normal')
sns.distplot(y, kde=False, fit=st.lognorm)
复制代码

结果就可以一下子跑几个图片了

 

 

 

 二、plt.subplots(nrows,ncols,sharex,sharey,subplot_kw,**fig_kw)

subplot可以规划figure划分为n个子图,但每条subplot命令只会创建一个子图

参数说明:

1.nrows:行数

2.ncols:列数

3.sharex:和谁共享x轴

4.sharey:和谁共享y轴

5.subplot_kw:关键字字典

6.**fig_kw:其他关键字

plt.subplot(ijn)形式,其中ij是行列数,n是第几个图,比如(221)则是一个有四个图,该图位于第一个

复制代码
#1.plt.subplot(ijn)形式,其中ij是行列数,n是第几个图,比如(221)则是一个有四个图,该图位于第一个

import numpy as np  
import matplotlib.pyplot as plt  
x = np.arange(0, 100)  
#作图1
plt.subplot(221)  
plt.plot(x, x)  
#作图2
plt.subplot(222)  
plt.plot(x, -x)  
 #作图3
plt.subplot(223)  
plt.plot(x, x ** 2)  
plt.grid(color='r', linestyle='--', linewidth=1,alpha=0.3)
#作图4
plt.subplot(224)  
plt.plot(x, np.log(x))  
plt.show()  


#又或者是这样
import numpy as np  
import matplotlib.pyplot as plt  
x = np.arange(0, 100)  
#作图1
plt.subplot(331)  
plt.plot(x, x)  
#作图2
plt.subplot(332)  
plt.plot(x, -x)  
 #作图3
plt.subplot(333)  
plt.plot(x, x ** 2)  
plt.grid(color='r', linestyle='--', linewidth=1,alpha=0.3)
复制代码

fig,axes=plt.subplots(n,n)

复制代码
#2.fig,axes=plt.subplots(n,n)
import numpy as np  
import matplotlib.pyplot as plt

x = np.arange(0, 100)  
#划分子图
fig,axes=plt.subplots(2,2)
ax1=axes[0,0]
ax2=axes[0,1]
ax3=axes[1,0]
ax4=axes[1,1]


#作图1
ax1.plot(x, x)  
#作图2
ax2.plot(x, -x)
 #作图3
ax3.plot(x, x ** 2)
ax3.grid(color='r', linestyle='--', linewidth=1,alpha=0.3)
#作图4
ax4.plot(x, np.log(x))  
plt.show() 

复制代码

fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2, figsize=(10, 10))

还可以是这种

复制代码
import numpy as np  
import matplotlib.pyplot as plt

x = np.arange(0, 100)  
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2, figsize=(10, 10))
#作图1
ax1.plot(x, x)  
#作图2
ax2.plot(x, -x)
 #作图3
ax3.plot(x, x ** 2)
ax3.grid(color='r', linestyle='--', linewidth=1,alpha=0.3)
#作图4
ax4.plot(x, np.log(x))  
plt.show() 
复制代码

 

复制代码
#或者是这种:用sns中的ax=ax1
import numpy as np  
import matplotlib.pyplot as plt
import seaborn as sns
x = np.arange(0, 100)  
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2, figsize=(10, 10))

#作图1
sns.regplot(x, x,ax=ax1)  
#作图2
sns.regplot(x, -x,ax=ax2)
 #作图3
sns.regplot(x, x ** 2,ax=ax3);
#作图4
sns.regplot(x, np.log(x),ax=ax4)  
plt.show() 
复制代码

 

使用for循环画图

#还是使用前面的Train_data,实现对v_的字段画条形图
n_cols=[i for i in Train_data.columns if i[:2]== 'v_']
n_cols=['v_0','v_1','v2'] #简单跑一下算了
for i ,col in enumerate(n_cols): plt.subplot(3,1,i+1) sns.barplot(x=col,y='price',data=Train_data) plt.title(('%s') % col)

 

posted on   小小喽啰  阅读(50913)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示