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()
  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瓜西皮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值