当前位置: 首页 > article >正文

python画图|在3D图上画2D直方图(作图平面移动)

前期我们已经学习过2D和3D的直方图绘制:

二维常规直方图绘制:python画图|水平直方图绘制_绘制水平直方图-CSDN博客

二维极坐标直方图绘制:python画图|极坐标中画直方图_ax1.plot()怎么画直方图-CSDN博客

三维直方图绘制:python画图|3D直方图基础教程-CSDN博客

三维直方图绘制进阶:python画图|3D bar进阶探索-CSDN博客

现在我们尝试在三维空间上画二维的直方图。

【1】官网教程

打开官网教程,看到漂亮的2D直方图位于不同的平面上:

Create 2D bar graphs in different planes — Matplotlib 3.9.2 documentation

代码非常简洁,因此尝试做一下解读。

【2】代码解读

首先是numpy计算模块和matplot画图模块的引入:

import matplotlib.pyplot as plt  #引入matplotlib模块画图
import numpy as np #引入numpy模块做数学计算

然后定义了随机数种子和要画图:

# Fixing random state for reproducibility
np.random.seed(19680801) #定义随机数种子


fig = plt.figure() #定义要画图
ax = fig.add_subplot(projection='3d') #定义要画3D图

之后设定了color、yticks矩阵,并使用for循环对其输出以画图:

colors = ['r', 'g', 'b', 'y'] #定义颜色矩阵
yticks = [3, 2, 1, 0] #定义Y轴显示的坐标值
for c, k in zip(colors, yticks): #定义for循环
    # Generate the random data for the y=k 'layer'.
    xs = np.arange(20) #xs取值0,1,2...20
    ys = np.random.rand(20) #ys为1行20列随机矩阵

    # You can provide either a single color or an array with the same length as
    # xs and ys. To demonstrate this, we color the first bar of each set cyan.
    cs = [c] * len(xs) #因变量定义
    cs[0] = 'c' #定义cs[0]

    # Plot the bar graph given by xs and ys on the plane y=k with 80% opacity.
      ax.bar(xs, ys, zs=k, zdir='y', color=cs, alpha=0.8) #输出直方图,zs表示画图平面

上述代码中:

【a】xs = np.arange(20) 表示xs取值0,1,2..,19;

【b】ys = np.random.rand(20) 表示ys为1行20列随机矩阵,这个随机矩阵已经提前用np.random.seed(19680801)定义了随机数种子。

【c】cs = [c] * len(xs) 表达的意思是:有多少个[c],[c]代表颜色,xs数组的长度是20,所以就是有20个[c],表明一共有20个直方图用了同一种颜色。

【d】cs[0] = 'c',约定了第一个直方图的颜色是cyan青绿色。

【e】zs=k, zdir='y'是指画图平面的位置,Z平面位于y=k轴。

最后设置了坐标,并输出图形:

ax.set_xlabel('X') #定义X轴
ax.set_ylabel('Y') #定义Y轴
ax.set_zlabel('Z') #定义Z轴

# On the y-axis let's only label the discrete values that we have data for.
ax.set_yticks(yticks) #将yticks输出

plt.show() #输出图形

fedf96773abb4cb4a4756d1e2bee1151.png

图1

由图1可见,所有平面上的直方图,第一个图形总是青绿色。

至此带注释的完整代码为:

import matplotlib.pyplot as plt  #引入matplotlib模块画图
import numpy as np #引入numpy模块做数学计算

# Fixing random state for reproducibility
np.random.seed(19680801) #定义随机数种子


fig = plt.figure() #定义要画图
ax = fig.add_subplot(projection='3d') #定义要画3D图

colors = ['r', 'g', 'b', 'y'] #定义颜色矩阵
yticks = [3, 2, 1, 0] #定义Y轴显示的坐标值
for c, k in zip(colors, yticks): #定义for循环
    # Generate the random data for the y=k 'layer'.
    xs = np.arange(20) #xs取值0,1,2...20
    ys = np.random.rand(20) #ys取值随机,范围[0,20)

    # You can provide either a single color or an array with the same length as
    # xs and ys. To demonstrate this, we color the first bar of each set cyan.
    cs = [c] * len(xs) #因变量定义
    cs[0] = 'c' #定义cs[0]

    # Plot the bar graph given by xs and ys on the plane y=k with 80% opacity.
    ax.bar(xs, ys, zs=k, zdir='y', color=cs, alpha=0.8) #输出直方图,zs表示画图平面

ax.set_xlabel('X') #定义X轴
ax.set_ylabel('Y') #定义Y轴
ax.set_zlabel('Z') #定义Z轴

# On the y-axis let's only label the discrete values that we have data for.
ax.set_yticks(yticks) #将yticks输出

plt.show() #输出图形

【3】代码修改

【3.1】直方图颜色设置

尝试不执行cs[0] = 'c',也就是不限定第一个矩形的值。将其改为注释:

#cs[0] = 'c' #定义cs[0]

输出图形为:

24e3ee9702e34ef4b1c9cd7fe227f183.png

图2

由图2可见,所有平面上的第一个直方图已经不再是青绿色,而是和同平面其他方块一致。

【3.2】直方图平面设置

在前述加注释过程中,已经发现直方图平面设置在Y轴,现在尝试将其转移到X轴:

ax.bar(xs, ys, zs=k, zdir='x', color=cs, alpha=0.8)

此时的输出结果为:

42dacab6a1d74edca997f840c3f60a8d.png

图3

转移到Z轴:

ax.bar(xs, ys, zs=k, zdir='z', color=cs, alpha=0.8)

1db9a5a9f365471aaef685159a4fc9df.png

图4

【4】代码优化

图形虽然展示了坐标轴,但是没有图名,因此尝试增加图名。

ax.set_title('3D plot which has 2D bar graphs')

为了让坐标轴等突出,设置坐标轴的颜色:

ax.set_xlabel('X',color="g") #定义X轴
ax.set_ylabel('Y',color="g") #定义Y轴
ax.set_zlabel('Z',color="g") #定义Z轴

 对比不同形式的图形,增加散点图绘制:

# Plot the bar graph given by xs and ys on the plane y=k with 80% opacity.
ax.bar(xs, ys, zs=k, zdir='y', color=cs, alpha=0.8)  # 输出直方图,zs表示画图平面
ax.scatter(xs, ys, zs=k, zdir='y', color=cs1, alpha=0.8) #输出直方图,zs表示画图平面

在散点图中出现color1,需要往前追溯,增加cs1定义和输出:

colors = ['r', 'g', 'b', 'y'] #定义颜色矩阵
colors1 = ['b', 'y', 'r', 'g'] #定义颜色矩阵
yticks = [3, 2, 1, 0] #定义Y轴显示的坐标值
for c, k ,c1 in zip(colors, yticks,colors1): #定义for循环
    # Generate the random data for the y=k 'layer'.
    xs = np.arange(20) #xs取值0,1,2...20
    ys = np.random.rand(20) #ys取值随机,范围[0,20)

    # You can provide either a single color or an array with the same length as
    # xs and ys. To demonstrate this, we color the first bar of each set cyan.
    cs = [c] * len(xs) #因变量定义
    cs1= [c1] * len(xs) #因变量定义

输出图形为:

25f75f3d4630409aa12b25f95aabad31.png

图5

输出结果如图5所示,同时绘制了散点图和直方图,且散点图位于直方图的顶端。

至此的完整代码为:

import matplotlib.pyplot as plt  #引入matplotlib模块画图
import numpy as np #引入numpy模块做数学计算

# Fixing random state for reproducibility
np.random.seed(19680801) #定义随机数种子


fig = plt.figure() #定义要画图
ax = fig.add_subplot(projection='3d') #定义要画3D图

colors = ['r', 'g', 'b', 'y'] #定义颜色矩阵
colors1 = ['b', 'y', 'r', 'g'] #定义颜色矩阵
yticks = [3, 2, 1, 0] #定义Y轴显示的坐标值
for c, k ,c1 in zip(colors, yticks,colors1): #定义for循环
    # Generate the random data for the y=k 'layer'.
    xs = np.arange(20) #xs取值0,1,2...20
    ys = np.random.rand(20) #ys取值随机,范围[0,20)

    # You can provide either a single color or an array with the same length as
    # xs and ys. To demonstrate this, we color the first bar of each set cyan.
    cs = [c] * len(xs) #因变量定义
    cs1= [c1] * len(xs) #因变量定义
    #cs[0] = 'c' #定义cs[0]

    # Plot the bar graph given by xs and ys on the plane y=k with 80% opacity.
    ax.bar(xs, ys, zs=k, zdir='y', color=cs, alpha=0.8)  # 输出直方图,zs表示画图平面
    ax.scatter(xs, ys, zs=k, zdir='y', color=cs1, alpha=0.8) #输出直方图,zs表示画图平面

ax.set_xlabel('X',color="g") #定义X轴
ax.set_ylabel('Y',color="g") #定义Y轴
ax.set_zlabel('Z',color="g") #定义Z轴
ax.set_title('3D plot which has 2D bar graphs')
# On the y-axis let's only label the discrete values that we have data for.
ax.set_yticks(yticks) #将yticks输出
ax.set_zlim(-0.1, 1.5) #设置Z轴
plt.show() #输出图形

【5】总结

学习了在三维坐标上绘制二维直方图,设置了坐标,修改了第一个直方图的颜色,并尝试了散点图和直方图的同时输出。

 

 


http://www.kler.cn/news/310326.html

相关文章:

  • 828华为云征文|华为Flexus云服务器打造《我的世界》游戏服务器
  • maven pom文件中的变量定义
  • MacOS Safari浏览器按ESC就退出全屏模式的去除办法
  • 机器狗与无人机空地协调技术分析
  • 如何快速解决程序中的BUG
  • LeetCode 每日一题 求出最多标记下标
  • Kubernetes从零到精通(12-Ingress、Gateway API)
  • camtasia2024绿色免费安装包win+mac下载含2024最新激活密钥
  • 662. 二叉树最大宽度 BFS 力扣
  • 层次聚类(Hierarchical Cluster)—无监督学习方法、非概率模型、判别模型、线性模型、非参数化模型、批量学习
  • 【原创 架构设计】多级缓存的应用、常见问题与解决方式
  • 【MATLAB源码-第170期】基于matlab的BP神经网络股票价格预测GUI界面附带详细文档说明。
  • svg与css关联
  • Spring Boot-Bean注入问题
  • JAVA对象、List、Map和JSON之间的相互转换
  • 电脑端视频剪辑软件哪个好用,十多款剪辑软件分享
  • 制造业的智能化革命:工业物联网(IIoT)的优势、层级应用及挑战解析
  • ArcGIS Pro SDK (十五)共享
  • python 实现average median平均中位数算法
  • Quartus sdc UI界面设置(二)
  • DockerLinux安装DockerDocker基础
  • Python PyQt5 定时器
  • kafka消息发送几种方式
  • 系统架构设计师 数据库篇
  • superset 解决在 mac 电脑上发送 slack 通知的问题
  • 如何准备教师资格证科目三“学科知识与教学能力”的考试与面试?(理科导向:数学/物理)
  • 基于Springboot+vue的音乐网站
  • 深度学习速通系列:TextCNN介绍
  • Koa (下一代web框架) 【Node.js进阶】
  • 遥感图像目标检测数据集-DOTA数据集