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

news/2024/9/20 6:39:21 标签: python, 平面, 开发语言

前期我们已经学习过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可见,所有平面上的直方图,第一个图形总是青绿色。

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

python">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所示,同时绘制了散点图和直方图,且散点图位于直方图的顶端。

至此的完整代码为:

python">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.niftyadmin.cn/n/5666783.html

相关文章

@Override -----好像删掉以后运行也没有问题。一个可有可无的注解?

简介 在 Java 中,Override 注解是可选的,但它提供了一些重要的好处。 虽然加不加 Override 注解在运行时的效果是一样的,但使用 Override 注解可以提高代码的可读性和维护性,并且可以在编译时捕获一些潜在的错误。 使用 O…

有效安全计划评估的基本指标

衡量安全计划成功与否的最有效指标是什么? 最直接的指标是:您的组织是否遭到入侵?如果答案是肯定的,那么显然还有工作要做。如果答案是否定的,那么您的状况就更好了——但情况比这更复杂。 即使您没有遭到入侵&#…

CSS的三种基本选择器

使用CSS控制网页格式有行内法&#xff0c;内嵌式&#xff0c;链接式&#xff0c;导入式等方法 这里将采用内嵌式的方法书写 内嵌法就是通过<style>标记将样式定义在HTML的文件头部中 1.标记选择器 标记选择器特点&#xff1a;定义了标记选择器之后&#xff0c;网页中…

『功能项目』QFrameWorkBug修改器界面【65】

我们打开上一篇64QFrameWork道具栏物品生成的项目&#xff0c; 本章要做的事情是做一个道具bug调试面板&#xff0c;可以增加主角属性&#xff0c;可以增加道具的功能 首先创建一个空物体&#xff08;钉子&#xff09; 按住Alt键将空物体钉到左侧 重命名为Left 创建Button、Im…

【计算机基础题目】Linux系统中文件权限 字母权限和数字权限的相互转换

创作日志&#xff1a; 很久之前对这个略有了解&#xff0c;但是现在完全忘记了&#xff0c;看到这类题目一脸懵逼&#xff0c;现在系统复习下。 1、权限的数字表示&#xff08;3位&#xff09; 在Linux系统中&#xff0c;文件权限由一个三位的八进制数表示&#xff0c;每一位代…

Mac 上哪个剪切板增强工具比较好用? 好用剪切板工具推荐

在日常文字编辑中&#xff0c;我们经常需要重复使用复制的内容。然而&#xff0c;新内容一旦复制&#xff0c;旧内容就会被覆盖。因此&#xff0c;选择一款易用高效的剪贴板工具成为了许多人的需求。本文整理了一些适用于 macOS 系统的优秀剪贴板增强工具&#xff0c;欢迎大家下…

C语言中的一些小知识点

一、逗号表达式 说明 在C语言中&#xff0c;逗号表达式是一种特殊的表达式&#xff0c;它允许你将多个表达式串联起来&#xff0c;并且会按照从左到右的顺序依次计算每个表达式&#xff0c;但整个表达式的值是最后一个表达式的值。 一般形式如下&#xff1a; expression1, …

大文件编辑器(QT)

项目需要做一个大文件编辑器&#xff0c;并对文件中特定的字符串进行高亮显示&#xff0c;尝试过几种方式。这里的大文件是指>几百兆的文件。 一 综述实现方式 方式1 用普通的QTextEdit来分段加载显示文本&#xff0c;当单段文本显示完毕并且继续向下拖动滚动条时&#xf…