数据可视化(六)多个子图及seaborn使用

1.多个子图绘制

python 复制代码
#绘制多个子图
#subplot(*args,**kwargs)  每个subplot函数只能绘制一个子图
#subplots(nrows,ncols)
#fig_add_subplot(行,列,区域)
#绘制子图第一种方式
plt.subplot(2,2,1)#第一个绘图区域两行两列
#plt.subplot(221)简写方式
plt.plot([1,2,3,4,5],[random.randint(1,10) for i in range(5)])
plt.subplot(2,2,2)#两行两列绘图区的第二个绘图区
plt.plot([1,2,3,4,5],[random.randint(1,10) for i in range(5)],'ro')
plt.subplot(2,1,2)#两行一列  ,第二行绘制
x=[1,2,3,4,5]
y=[random.randint(10,50) for i in range(5)]
plt.bar(x,y)
plt.show()
python 复制代码
#绘制的第二种方式
#两行三列的画图区域
#figure画布,axes坐标轴对象
figure,axes=plt.subplots(2,3)
plt.show()

figure,axes=plt.subplots(2,2)
axes[0,0].plot([1,2,3,4,5],[random.randint(1,10) for i in range(5)])
axes[0,1].plot([1,2,3,4,5],[random.randint(1,10) for i in range(5)],'ro')
x=[1,2,3,4,5]
y=[random.randint(10,50) for i in range(5)]
axes[1,0].bar(x,y)
x=[random.randint(10,50) for i in range(5)]
axes[1,1].pie(x,autopct='%1.1f%%')
plt.show()
python 复制代码
#绘制子图的第三种方式
#绘制画布
fig=plt.figure()
ax1=fig.add_subplot(2,2,1)  #两行两列第一个绘图区域
ax1.plot([1,2,3,4,5],[random.randint(1,10) for i in range(5)])
ax2=fig.add_subplot(2,2,2)  #两行两列第二个绘图区域
ax2.plot([1,2,3,4,5],[random.randint(1,10) for i in range(5)],'ro')
ax3=fig.add_subplot(2,2,3)
ax3.bar([1,2,3,4,5],[random.randint(1,10) for i in range(5)])
ax4=fig.add_subplot(2,2,4)
ax4.pie([random.randint(1,10) for i in range(5)],autopct='%1.1f%%')
plt.show()
python 复制代码
#图表的保存
#图表保存格式jpeg,tiff,png
#plt.savefig(图名)

3.seaborn使用,首先安装。如果在pycharm中安装报错,先安装Scipy

python 复制代码
import matplotlib.pyplot as plt
import seaborn as sns
#seaborn绘图
#绘制简单柱状图
sns.set_style('darkgrid')#设置风格样式
x=[1,2,3,4,5]
y=[20,6,50,9,56]
sns.barplot(x,y)
plt.show()
相关推荐
小白学大数据5 天前
Pandas与Matplotlib:Python中的动态数据可视化
开发语言·爬虫·python·pandas·matplotlib
yu矞10 天前
时间序列预测学习方向总概括
python·学习·numpy·pandas·pillow·matplotlib
问道飞鱼11 天前
python常用库学习-Matplotlib使用
python·学习·matplotlib
知识在于积累11 天前
Python中matplotlib-legend图例水平排列
matplotlib·legend·图例水平排列
Trouvaille ~12 天前
【Python篇】matplotlib超详细教程-由入门到精通(上篇)
开发语言·python·数据分析·matplotlib·数据可视化·绘图·python3.11
西猫雷婶13 天前
python画图|3D图基础教程
开发语言·笔记·python·学习·3d·matplotlib
异构算力老群群15 天前
使用Python读取Excel数据
python·excel·numpy·pandas·matplotlib·ipython
flex_university16 天前
Matplotlib入门笔记
笔记·matplotlib
fydw_71518 天前
Matplotlib 详解
matplotlib
毛飞龙19 天前
Mac/Linux系统matplotlib中文支持问题
linux·mac·matplotlib·中文显示