12.子图布局,嵌套

import numpy as np
import matplotlib.pyplot as plt
# 不用打imshow也能显示
%matplotlib inline 

子图布局

# 3行3列,第二个参数是在什么位置
ax1 = plt.subplot2grid((3,3), (0,0))
ax2 = plt.subplot2grid((3,3), (0,1))
ax3 = plt.subplot2grid((3,3), (1,0))
ax4 = plt.subplot2grid((3,3), (1,1))
ax5 = plt.subplot2grid((3,3), (0,2), rowspan=2) # 占用多行
ax6 = plt.subplot2grid((3,3), (2,0), colspan=3) # 占用多列

请添加图片描述

子图嵌套

x = np.linspace(0,10,1000)
y2 = np.sin(x**2)
y1 = x**2

# 主轴
fig, ax1 = plt.subplots()
left, bottom, width, height = [0.22,0.45,0.3,0.35]
# 嵌套一个子轴
ax2 = fig.add_axes([left,bottom,width,height])

ax1.plot(x,y1)
ax2.plot(x,y2)
[<matplotlib.lines.Line2D at 0x26272f307b8>]

请添加图片描述

  • 第二种子图嵌套
from mpl_toolkits.axes_grid.inset_locator import inset_axes

fig, ax1 = plt.subplots(figsize=(20,20))
ax1.bar(range(10),np.random.randn(10))
ax2 = inset_axes(ax1, width=6, height=6, loc=5)
ax2.plot([1,2,3],[2,3,4])
[<matplotlib.lines.Line2D at 0x26273030748>]

请添加图片描述


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要利用 Python 绘制仓库布局图,可以使用 Python 中的 matplotlib 库进行绘图。下面是一个简单的示例代码,可以绘制一个仓库布局图: ```python import matplotlib.pyplot as plt # 设置画布大小 plt.figure(figsize=(8, 6)) # 绘制仓库格子 for i in range(10): for j in range(10): rect = plt.Rectangle((i, j), 1, 1, linewidth=1, edgecolor='black', facecolor='none') plt.gca().add_patch(rect) # 标注货架位置 plt.text(1.5, 1.5, 'A1') plt.text(8.5, 1.5, 'A2') plt.text(1.5, 8.5, 'B1') plt.text(8.5, 8.5, 'B2') # 设置坐标轴范围 plt.xlim(0, 10) plt.ylim(0, 10) # 隐藏坐标轴 plt.axis('off') # 显示图像 plt.show() ``` 在这个示例中,我们首先通过 `matplotlib.pyplot` 模块创建一个画布,然后通过两个嵌套的 `for` 循环绘制了一个 10x10 的格子图。接着,我们使用 `plt.text()` 函数在图中标注了四个货架的位置,并设置了坐标轴范围和隐藏了坐标轴。最后,我们使用 `plt.show()` 函数显示图像。 ### 回答2: 使用Python绘制仓库布局图可以使用matplotlib库来实现。首先需要安装matplotlib库,可以使用pip命令进行安装。 安装完成后,导入matplotlib库和numpy库,并创建一个画布和一个子图: ``` import matplotlib.pyplot as plt import numpy as np fig, ax = plt.subplots() ``` 然后,根据仓库的布局情况,使用矩阵或者数组来表示仓库的不同区域,可以使用numpy库来方便地创建和操作矩阵。例如,可以使用以下代码创建一个6行8列的矩阵,表示仓库的布局: ``` layout = np.array([[0, 0, 1, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]) ``` 接下来,可以使用imshow函数将矩阵显示在子图中,0表示空位,1表示货物所在的位置: ``` ax.imshow(layout, cmap='gray_r') # 可选设置坐标轴 ax.set_xticks(np.arange(-0.5, 8, 1)) ax.set_yticks(np.arange(-0.5, 6, 1)) # 可选设置网格线 ax.grid(color='black', linewidth=0.5) ``` 最后使用show函数显示绘制好的仓库布局图: ``` plt.show() ``` 以上就是使用Python绘制仓库布局图的基本步骤,可以根据实际需求进行进一步的美化和调整。 ### 回答3: 利用Python 绘制仓库布局图可以使用各种图形绘制库,比如Matplotlib、Seaborn、Plotly等等。以下是使用Matplotlib库绘制仓库布局图的示例代码: ```Python import matplotlib.pyplot as plt # 仓库布局数据 locations = { "A": (0, 0), "B": (0, 5), "C": (3, 2), "D": (7, 3), "E": (9, 6), "F": (12, 1), "G": (12, 6) } # 初始化图形 fig, ax = plt.subplots() # 绘制仓库布局 for location, coord in locations.items(): ax.plot(coord[0], coord[1], 'o', markersize=10) ax.annotate(location, (coord[0]+0.2, coord[1]), fontsize=12) # 绘制仓库之间的路径 ax.plot([locations['A'][0], locations['B'][0]], [locations['A'][1], locations['B'][1]], 'k--') ax.plot([locations['B'][0], locations['C'][0]], [locations['B'][1], locations['C'][1]], 'k--') ax.plot([locations['C'][0], locations['D'][0]], [locations['C'][1], locations['D'][1]], 'k--') ax.plot([locations['D'][0], locations['E'][0]], [locations['D'][1], locations['E'][1]], 'k--') ax.plot([locations['D'][0], locations['F'][0]], [locations['D'][1], locations['F'][1]], 'k--') ax.plot([locations['E'][0], locations['G'][0]], [locations['E'][1], locations['G'][1]], 'k--') # 设置图形属性 ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_title('仓库布局图') # 显示图形 plt.show() ``` 除了以上示例代码,我们还可以根据具体的需求,结合Python绘图库的各种功能,定制出更加精美和符合实际情况的仓库布局图。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值