栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Matplotlib绘制立方体示意图-伪三维

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Matplotlib绘制立方体示意图-伪三维

结果预览

开始绘制

利用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()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/307351.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号