数据可视化(六)多个子图及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()
相关推荐
YiSLWLL1 天前
使用Tauri 2.3.1+Leptos 0.7.8开发桌面小程序汇总
python·rust·sqlite·matplotlib·visual studio code
满怀10154 天前
【Python进阶】数据可视化:Matplotlib从入门到实战
python·信息可视化·数据分析·matplotlib·数据可视化
是一只努力的小菜鸡啦5 天前
Matplotlib的应用
matplotlib
失去妙妙屋的米奇10 天前
matplotlib数据展示
开发语言·图像处理·python·计算机视觉·matplotlib
小旺不正经12 天前
人工智能基础-matplotlib基础
人工智能·matplotlib
Thanks_ks12 天前
利用 Python 进行股票数据可视化分析
python·matplotlib·热力图·seaborn·可视化分析·股票数据·yfinance
拾荒的小海螺15 天前
【数据分析实战】使用 Matplotlib 绘制折线图
信息可视化·数据分析·matplotlib
灵均66617 天前
机器学习-线性回归模型
人工智能·机器学习·线性回归·numpy·pandas·scikit-learn·matplotlib
懒羊羊不进村17 天前
Python基础——Matplotlib库
开发语言·python·matplotlib
月小水长21 天前
Django 使用 matplotlib 遇到 RuntimeError: main thread is not in main loop 解决办法
python·django·matplotlib·thread·anr