数据可视化之多表显示

多表显示subplot(),subplots()
#
使用 pyplot 中的 subplot() subplots() 方法来绘制多个子图


# 导入库,和调用中文
import matplotlib.pyplot as plt
import numpy as np

# 作用:解决坐标轴为负时 负号显示为方框的问题
# axes:坐标轴
# Unicode:编码标准
# minus:负号,减号
plt.rcParams['axes.unicode_minus'] = False
# 解决图上汉字显示为方框的问题 在图上显示汉字
plt.rcParams['font.family'] = ['SimHei']

# subplot()函数使用
#  subplot(numRows,numCols,plotNum)
#  numRows
1numCols 2
就是将图表绘制成 1x2 的图片区域, 对应的坐标为:
#               (1, 1), (1, 2)
# plotNum
1, 表示的坐标为(1, 1), 即第一行第一列的子图。
# plotNum 2, 表示的坐标为(1, 2), 即第一行第二列的子图。
#
#
#
准备数据

# plot 1:
xpoints = np.array([0, 6])
ypoints = np.array([
0, 100])

# plot 2:
x = np.array([1, 2, 3, 4])
y = np.array([
1, 4, 9, 16])

# 准备画布
plt.subplot(2,2,1)
# 绘图
plt.plot(xpoints,ypoints)
plt.title(
'图一')
plt.subplot(
2,2,3)
plt.plot(x,y)
plt.title(
"图二")

# 总的大标题
plt.suptitle("多张图绘制")

# matplotlib.pyplot.subplots(
#           nrows=1,        //
默认为1,设置图表的的行数
#           ncols=1,        //默认为1,设置图标列数
#           *,
#           sharex=False,   //
设置x轴是否共享,默认不共享
#                             可设置为 'none''all''row' 'col'
#           sharey=False,   //设置y轴是否共享,默认不共享。
#                             False none 每个子图的 x 轴或 y 轴都是独立的,
#                             True 'all':所有子图共享 x 轴或 y 轴,
#                             'row' 设置每个子图行共享一个 x y 轴,
#                             'col':设置每个子图列共享一个 x y 轴。
#           squeeze=True,
#           subplot_kw=None,   //
可选,字典类型。
#                               把字典的关键字传递给 add_subplot() 来创建每个子图。
#           gridspec_kw=None,   //可选,字典类型。
#                               把字典的关键字传递给 GridSpec 构造函数
#                               创建子图放在网格里(grid)
#           **fig_kw)        /把详细的关键字参数传给 figure() 函数
#
#
准备数据2
np.linspace(start = 0, stop = 100, num = 5) # 平均分布着的数据
x = np.linspace(0,2*np.pi,50)
y = np.sin(x)


# 原图
plt.figure()
plt.plot(x,y)

yticks =
range(-2,2,1)
plt.yticks(yticks)
plt.xlabel(
'x')
plt.ylabel(
'y')
plt.grid(
linestyle='--',alpha=0.5)
plt.title(
'原图')


# 创建一个画像和一个子图
figure1,plot1 = plt.subplots()
plot1.plot(x,y)
plot1.set_title(
'一个画像和一个子图')

# 创建一个画像,两个子图,并共享y
figure2,(plot1,plot2) = plt.subplots(1,2,sharey=True)
plot1.plot(x,y)           
# 折线图
plot1.set_title('子图1')   # 子图1的标题
plot1.grid(linestyle='-',alpha = 0.5)   # 网格线
plot2.scatter(x,y)         # 散点图
plot2.set_title('子图2')   # 子图1的标题
plot2.grid(linestyle='-',alpha = 0.5)   # 网格线
plt.suptitle('2个子图')     总标题

# 在调用subplot()创建子图时通过设置projection='polar',
#
便可创建一个极坐标子图,
# 然后调用plot()在极坐标子图中绘图。
fig=plt.figure()
ax1 = plt.subplot(
1,2,1, projection='polar'# 极坐标轴
ax2 = plt.subplot(122)      # 数据类型相同时,可以省略
fig.subplots_adjust(wspace=0.4) # 设置子图间的间距,为子图宽度的40%
theta=np.arange(0,2*np.pi,0.02)
ax1.plot(theta,theta/
6,'-.',lw=2)
ax2.plot(theta,theta/
6,'-.',lw=2)

# 创建四个子图
fig, axs = plt.subplots(2, 2, subplot_kw=dict(projection="polar"))
axs[
0, 0].plot(x, y)
axs[
1, 1].scatter(x, y)

# 共享 x
fig,axs = plt.subplots(2, 2, sharex='col')
axs[
0, 0].plot(x, y)
axs[
1, 1].scatter(x, y)
# 共享 y
fig2,axs2 = plt.subplots(2, 2, sharey='row')
axs2[
0, 0].plot(x, y)
axs2[
1, 1].scatter(x, y)
# 共享 x 轴和 y
fig3,axs3 = plt.subplots(2, 2, sharex='all', sharey='all')
axs3[
0, 0].plot(x, y)
axs3[
1, 1].scatter(x, y)
# 这个也是共享 x 轴和 y
fig4,ax = plt.subplots(2, 2, num=10,sharex=True, sharey=True)
# 创建标识为 10 的图,已经存在的则删除
fig5, axs= plt.subplots(2,2,num=10, clear=True)

# 展示
plt.show(block=True)

 

相关推荐

  1. 数据显示

    2024-03-31 18:42:01       20 阅读
  2. 数据折线图plot

    2024-03-31 18:42:01       18 阅读
  3. 数据极坐标

    2024-03-31 18:42:01       17 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-31 18:42:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-31 18:42:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-31 18:42:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-31 18:42:01       20 阅读

热门阅读

  1. 软件之禅(十一) 消息

    2024-03-31 18:42:01       18 阅读
  2. vim的缓冲区管理技能

    2024-03-31 18:42:01       16 阅读
  3. ChatGPT:学术界必备的写作利器

    2024-03-31 18:42:01       17 阅读
  4. C 语言练习分享

    2024-03-31 18:42:01       16 阅读
  5. leetcode 64.最小路径和

    2024-03-31 18:42:01       14 阅读
  6. vue组件的select怎么赋值?

    2024-03-31 18:42:01       15 阅读
  7. Leetcode-2952-需要添加的硬币的最小数量-c++

    2024-03-31 18:42:01       21 阅读
  8. C++多线程:unique_lock源码分析与使用详解(六)

    2024-03-31 18:42:01       16 阅读
  9. 为什么Redis设计成单线程

    2024-03-31 18:42:01       19 阅读
  10. 2952. 需要添加的硬币的最小数量

    2024-03-31 18:42:01       19 阅读