数据可视化(六)多个子图及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()
相关推荐
Gyoku Mint1 天前
机器学习×第二卷:概念下篇——她不再只是模仿,而是开始决定怎么靠近你
人工智能·python·算法·机器学习·pandas·ai编程·matplotlib
搏博3 天前
将图形可视化工具的 Python 脚本打包为 Windows 应用程序
开发语言·windows·python·matplotlib·数据可视化
Code_流苏6 天前
Python趣学篇:交互式词云生成器(jieba + Tkinter + WordCloud等)
python·pillow·matplotlib·tkinter·wordcloud·jieba分词·词云生成器
AndrewHZ8 天前
【图像处理入门】2. Python中OpenCV与Matplotlib的图像操作指南
图像处理·python·opencv·计算机视觉·matplotlib·图像操作
无闻墨客9 天前
数据可视化--使用matplotlib绘制高级图表
python·机器学习·信息可视化·matplotlib·可视化·数据可视化
Echo-J9 天前
数据可视化(第4、5、6次课)
python·信息可视化·matplotlib·数据可视化
ayas1231912 天前
numpy与matplotlib学习——数据可视化入门
学习·numpy·matplotlib
zhangfeng113312 天前
Python 和 matplotlib 保存图像时,确保图像的分辨率和像素符合特定要求(如 64x64),批量保存 不溢出内存
开发语言·python·matplotlib
李昊哲小课15 天前
matplotlib基本绘图
人工智能·数据分析·matplotlib·数据可视化·pyecharts·seaborn
云攀登者-望正茂16 天前
如何在Mac 上使用Python Matplotlib
python·macos·matplotlib