结果预览

开始绘制

利用python的Matplotlib包来绘制上面的立方体示意图,首先导入所需包。

导入包

import numpy as np
import matplotlib.pyplot as plt

创建画布和坐标系

fig = plt.figure(figsize=(10, 10)) # 绘制画布
ax = fig.add_subplot(111) # 在画布上添加一个坐标轴

设置坐标显示范围

置x、y坐标轴的范围的为0~10,方便后续绘制立方体。

#设置x、y坐标轴的范围
plt.xlim(0,10)
plt.ylim(0,10)

去掉边框和刻度

将右轴和定轴隐藏,刻度隐藏,并将左轴和下轴宽度设置为1.3

ax.spines['right'].set_color('none') # 设置右轴不显示
ax.spines['top'].set_color('none') # 设置顶轴不显示
ax.spines['left'].set_linewidth(1.3) # 设置左轴线宽度为1.3
ax.spines['bottom'].set_linewidth(1.3) # 设置下轴线宽度为1.3
ax.tick_params(which='major',labelbottom= False,bottom = False,labelleft= False,left= False,length=5,labelsize=0) # 设置刻度不显示
ax.tick_params(which='minor',bottom = False,length=4,labelsize=0) # 设置子刻度不显示

添加坐标轴箭头和标签

利用annotate()绘制坐标轴箭头,text()设置标签。设置y轴与x轴夹角为60度,这样就完成了坐标轴的绘制。

ax.annotate("", xytext=(9.0, 0.0),xy=(10.0, 0.0), arrowprops={'facecolor':"black", 'shrink':1.0, 'width':0.1,'headwidth':4.0,'headlength':6.0}) # 绘制x轴箭头,(shrink:控制箭炳长度)
ax.text(10.05, -0.05, r'$\mathcal{x}$', fontsize=16.0) # x轴标签ax.plot([0.0, 2.0,], [0.0, 2.0*np.tan(60*np.pi/180),], 'k-', linewidth=1.3) # 绘制y轴,夹角为60度
ax.annotate("", xytext=(0.0, 0.0),xy=(2.0, 2.0*np.tan(60*np.pi/180)), arrowprops={'facecolor':"black", 'shrink':1.0, 'width':0.1,'headwidth':4.0,'headlength':6.0}) # 绘制y轴箭头
ax.text(2.1, 2.0*np.tan(60*np.pi/180) + 0.1, r'$\mathcal{y}$', fontsize=16.0) # y轴标签ax.annotate("", xytext=(0.0, 9.0),xy=(0.0, 10.0), arrowprops={'facecolor':"black", 'shrink':1.0, 'width':0.1,'headwidth':4.0,'headlength':6.0})# 绘制z轴箭头
ax.text(-0.05, 10.1, r'$\mathcal{z}$', fontsize=16.0) # z轴标签

绘制立方体

接下来就是绘制立方体了。

设置正方体边长和左下角坐标

首先设置立方体边长和左下角坐标,以方便计算立方体节点坐标。

cube_side = 4.0 # 正方体边长
x0,y0 = 3.0,1.0  # 正方体左下角坐标

绘制横线

内测线根据60度的夹角计算。

# 横线
ax.plot([x0, x0 + cube_side], [y0, y0], 'b-', linewidth=2.0)
ax.plot([x0, x0 + cube_side], [y0 + cube_side, y0 + cube_side], 'b-', linewidth=2.0)
ax.plot([x0 + cube_side/2 * np.cos(60*np.pi/180), x0 + cube_side + cube_side/2 * np.cos(60*np.pi/180)],[y0 + cube_side/2 * np.sin(60*np.pi/180), y0  + cube_side/2 * np.sin(60*np.pi/180)], 'b--', linewidth=2.0)ax.plot([x0 + cube_side/2 * np.cos(60*np.pi/180), x0 + cube_side + cube_side/2 * np.cos(60*np.pi/180)],[y0 + cube_side/2 * np.sin(60*np.pi/180)+ cube_side, y0  + cube_side/2 * np.sin(60*np.pi/180)+ cube_side], 'b-', linewidth=2.0)

绘制纵向线

# 纵线
ax.plot([x0, x0], [y0, y0 + cube_side], 'b-', linewidth=2.0)
ax.plot([x0 + cube_side, x0 + cube_side], [y0, y0 + cube_side], 'b-', linewidth=2.0)
ax.plot([x0 + cube_side/2 * np.cos(60*np.pi/180), x0 + cube_side/2 * np.cos(60*np.pi/180)], [y0 + cube_side/2 * np.sin(60*np.pi/180), y0 + cube_side + + cube_side/2 * np.sin(60*np.pi/180)], 'b--', linewidth=2.0)
ax.plot([x0 + cube_side/2 * np.cos(60*np.pi/180) + cube_side, x0 + cube_side/2 * np.cos(60*np.pi/180) + cube_side], [y0 + cube_side/2 * np.sin(60*np.pi/180), y0 + cube_side + + cube_side/2 * np.sin(60*np.pi/180)], 'b-', linewidth=2.0)

绘制斜向线

# 斜线
ax.plot([x0, x0 + cube_side/2 * np.cos(60*np.pi/180)], [y0, y0 + cube_side/2 * np.sin(60*np.pi/180)], 'b--', linewidth=2.0)
ax.plot([x0, x0 + cube_side/2 * np.cos(60*np.pi/180)], [y0 + cube_side, y0 + cube_side/2 * np.sin(60*np.pi/180) + cube_side], 'b-', linewidth=2.0)
ax.plot([x0 + cube_side, x0 + cube_side/2 * np.cos(60*np.pi/180) + cube_side], [y0, y0 + cube_side/2 * np.sin(60*np.pi/180)], 'b-', linewidth=2.0)
ax.plot([x0 + cube_side, x0 + cube_side/2 * np.cos(60*np.pi/180) + cube_side], [y0 + cube_side, y0 + cube_side/2 * np.sin(60*np.pi/180) + cube_side], 'b-', linewidth=2.0)

图片保存

完成绘图后,进行图片的保存。

plt.savefig('cube.png',dpi=300)
plt.show()

完整代码

# -*- coding: utf-8 -*-
"""
Created on Sat Oct  9 16:54:42 2021@author: 瓜西皮
"""# 基本工具
import numpy as np
import matplotlib.pyplot as plt
# # 设置支持中文、符号等
# import matplotlib as mpl
# mpl.rcParams['font.sans-serif'] = ['SimHei'] # 设置中文
# mpl.rcParams['axes.unicode_minus'] = False # 设置支持负号显示fig = plt.figure(figsize=(10, 10)) # 绘制画布
ax = fig.add_subplot(111) # 在画布上添加一个坐标轴
#设置x、y坐标轴的范围
plt.xlim(0,10)
plt.ylim(0,10)ax.spines['right'].set_color('none') # 设置右轴不显示
ax.spines['top'].set_color('none') # 设置顶轴不显示
ax.spines['left'].set_linewidth(1.3) # 设置左轴线宽度为1.3
ax.spines['bottom'].set_linewidth(1.3) # 设置下轴线宽度为1.3
ax.tick_params(which='major',labelbottom= False,bottom = False,labelleft= False,left= False,length=5,labelsize=0) # 设置刻度不显示
ax.tick_params(which='minor',bottom = False,length=4,labelsize=0) # 设置子刻度不显示ax.annotate("", xytext=(9.0, 0.0),xy=(10.0, 0.0), arrowprops={'facecolor':"black", 'shrink':1.0, 'width':0.1,'headwidth':4.0,'headlength':6.0}) # 绘制x轴箭头,(shrink:控制箭炳长度)
ax.text(10.05, -0.05, r'$\mathcal{x}$', fontsize=16.0) # x轴标签ax.plot([0.0, 2.0,], [0.0, 2.0*np.tan(60*np.pi/180),], 'k-', linewidth=1.3) # 绘制y轴,夹角为60度
ax.annotate("", xytext=(0.0, 0.0),xy=(2.0, 2.0*np.tan(60*np.pi/180)), arrowprops={'facecolor':"black", 'shrink':1.0, 'width':0.1,'headwidth':4.0,'headlength':6.0}) # 绘制y轴箭头
ax.text(2.1, 2.0*np.tan(60*np.pi/180) + 0.1, r'$\mathcal{y}$', fontsize=16.0) # y轴标签ax.annotate("", xytext=(0.0, 9.0),xy=(0.0, 10.0), arrowprops={'facecolor':"black", 'shrink':1.0, 'width':0.1,'headwidth':4.0,'headlength':6.0})# 绘制z轴箭头
ax.text(-0.05, 10.1, r'$\mathcal{z}$', fontsize=16.0) # z轴标签# 绘制正方体图形
cube_side = 4.0 # 正方体边长
x0,y0 = 3.0,1.0  # 正方体左下角坐标# 横线
ax.plot([x0, x0 + cube_side], [y0, y0], 'b-', linewidth=2.0)
ax.plot([x0, x0 + cube_side], [y0 + cube_side, y0 + cube_side], 'b-', linewidth=2.0)
ax.plot([x0 + cube_side/2 * np.cos(60*np.pi/180), x0 + cube_side + cube_side/2 * np.cos(60*np.pi/180)],[y0 + cube_side/2 * np.sin(60*np.pi/180), y0  + cube_side/2 * np.sin(60*np.pi/180)], 'b--', linewidth=2.0)ax.plot([x0 + cube_side/2 * np.cos(60*np.pi/180), x0 + cube_side + cube_side/2 * np.cos(60*np.pi/180)],[y0 + cube_side/2 * np.sin(60*np.pi/180)+ cube_side, y0  + cube_side/2 * np.sin(60*np.pi/180)+ cube_side], 'b-', linewidth=2.0)# 纵线
ax.plot([x0, x0], [y0, y0 + cube_side], 'b-', linewidth=2.0)
ax.plot([x0 + cube_side, x0 + cube_side], [y0, y0 + cube_side], 'b-', linewidth=2.0)
ax.plot([x0 + cube_side/2 * np.cos(60*np.pi/180), x0 + cube_side/2 * np.cos(60*np.pi/180)], [y0 + cube_side/2 * np.sin(60*np.pi/180), y0 + cube_side + + cube_side/2 * np.sin(60*np.pi/180)], 'b--', linewidth=2.0)
ax.plot([x0 + cube_side/2 * np.cos(60*np.pi/180) + cube_side, x0 + cube_side/2 * np.cos(60*np.pi/180) + cube_side], [y0 + cube_side/2 * np.sin(60*np.pi/180), y0 + cube_side + + cube_side/2 * np.sin(60*np.pi/180)], 'b-', linewidth=2.0)
# 斜线
ax.plot([x0, x0 + cube_side/2 * np.cos(60*np.pi/180)], [y0, y0 + cube_side/2 * np.sin(60*np.pi/180)], 'b--', linewidth=2.0)ax.plot([x0, x0 + cube_side/2 * np.cos(60*np.pi/180)], [y0 + cube_side, y0 + cube_side/2 * np.sin(60*np.pi/180) + cube_side], 'b-', linewidth=2.0)ax.plot([x0 + cube_side, x0 + cube_side/2 * np.cos(60*np.pi/180) + cube_side], [y0, y0 + cube_side/2 * np.sin(60*np.pi/180)], 'b-', linewidth=2.0)ax.plot([x0 + cube_side, x0 + cube_side/2 * np.cos(60*np.pi/180) + cube_side], [y0 + cube_side, y0 + cube_side/2 * np.sin(60*np.pi/180) + cube_side], 'b-', linewidth=2.0)# 保存图片
plt.savefig('cube.png',dpi=300)
plt.show()

Matplotlib绘制立方体示意图-伪三维相关推荐

  1. Matplotlib绘制雷达图和三维图

    1.雷达图 程序示例 '''1.空白极坐标图''' import matplotlib.pyplot as pltplt.polar() plt.show()'''2.绘制一个极坐标点''' impo ...

  2. python绘制雷达图代码实例-Matplotlib绘制雷达图和三维图的示例代码

    '''1.空白极坐标图''' import matplotlib.pyplot as plt plt.polar() plt.show() '''2.绘制一个极坐标点''' import numpy ...

  3. python绘制三维图像球_python matplotlib:绘制具有周长的三维球体

    在你展示的例子中,我不认为圆可以相互垂直(即一个是赤道,一个穿过北极和南极).如果水平圆是赤道,那么北极一定在一条垂直线上的某个地方,这条垂直线穿过代表球体的黄色圆的中心.否则,赤道的右边看起来会比左 ...

  4. 圆形和多边形雷达图python-Matplotlib绘制雷达图和三维图的示例代码

    1.雷达图 程序示例 '''1.空白极坐标图''' import matplotlib.pyplot as plt plt.polar() plt.show() '''2.绘制一个极坐标点''' im ...

  5. python绘制三维轨迹_Python学习(一) —— matplotlib绘制三维轨迹图

    在研究SLAM时常常需要对其输出的位姿进行复现以检测算法效果,在ubuntu系统中使用Python可以很好的完成相关的工作. 一. Ubuntu下Python的使用 在Ubuntu下使用Python有 ...

  6. Python可视化matplotlib绘制三维可视化图形(Three-Dimensional)

    Python可视化matplotlib绘制三维可视化图形(Three-Dimensional) Matplotlib最初设计时只考虑二维可视化绘图.大约在1.0版本发布的时候,在Matplotlib的 ...

  7. python绘制三维曲面图-Python中使用Matplotlib绘制3D图形示例

    原标题:Python中使用Matplotlib绘制3D图形示例 3D图形能给我们对数据带来更加深入地理解.python的matplotlib库就包含了丰富的3D绘图工具.3D图形在数据分析.数据建模. ...

  8. python画图三维-Python使用matplotlib绘制三维图形示例

    本文实例讲述了Python使用matplotlib绘制三维图形.分享给大家供大家参考,具体如下: 用二维泡泡图表示三维数据 泡泡的坐标2维,泡泡的大小三维,使用到的函数 plt.scatter(P[: ...

  9. python代码示例图形-Python使用matplotlib绘制三维图形示例

    本文实例讲述了Python使用matplotlib绘制三维图形.分享给大家供大家参考,具体如下: 用二维泡泡图表示三维数据 泡泡的坐标2维,泡泡的大小三维,使用到的函数 plt.scatter(P[: ...

  10. Python使用matplotlib绘制三维曲线

    本文主要演示如何使用matplotlib绘制三维图形.直接上代码,关键语句配有注释方便理解. import matplotlib as mpl from mpl_toolkits.mplot3d im ...

最新文章

  1. 北航计算机学院编译原理,1 北航本科编译原理课件课本 张莉.pdf
  2. 中国鱼胶原蛋白行业应用前景与十四五投资建议报告2022年
  3. 【机器视觉】 Halcon批量加载图像
  4. 如何测试一个财务软件系统,对比测试工具平台让财务测试飞起来
  5. IIS负载均衡-Application Request Route详解第三篇:使用ARR进行Http请求的负载均衡
  6. nginx mozilla_我发现Mozilla的私人浏览模式存在重大缺陷。
  7. asp+ajax菜单,AJAX_基于asp+ajax和数据库驱动的二级联动菜单,index.asp 页面代码 复制代码 代 - phpStudy...
  8. 新版 Chrome 等主流浏览器将不再允许关闭点击跟踪
  9. 推流式搅拌器选型功率计算方法_QSJ-1000
  10. c语言编译器IDE有iOS,C语言编译器和IDE的选择
  11. 360云服务器合作,360云主机速度(云服务器)
  12. vim 设置配色方案
  13. 浅谈5G和4G有哪些区别?
  14. JS动态添加HTML元素
  15. 画洗碗机器人的思维导图_赞!三年级小学生画出这样的思维导图
  16. esxi - 加装vmware titan xp显卡配置
  17. 消防产品在酒店行业的应用
  18. android 对话框大全,Android 对话框(Dialog)大全
  19. 华硕(ASUS)M50S81VN-SL外接 Dell 2209wa出现水波纹
  20. Python + AI 微信朋友圈的故事

热门文章

  1. python中如何画图中图
  2. 360儿童手表显示服务器错误,360儿童卫士刷机失败怎么办 刷机方法
  3. 高等数学:对向量及其线性运算和数量积、向量积的见解
  4. dci odbc mysql_人大金仓-
  5. 大表哥在csdn的第一篇博客
  6. oracle中both,ORACLE:scope=both|memery|spfile
  7. nca算法_NCA告诉英国公民,立即寻找有史以来最恶劣的网络攻击的保护
  8. 江苏南通20多万个QQ账号被盗,涉案金额200余万
  9. java take,Java DelayQueue take()用法及代码示例
  10. HTB-Sequel