python - 如何将图形(3x3 子图)插入 matplotlib python 的子图中
问题描述
我想在子图中插入一个 3x3 图形。这里和这里显示了一个类似的问题,但解决方案似乎不起作用(我认为)。
任何人都可以提供一些代码(尽可能简单)来产生这个:
如果有人可以提供帮助,我会很高兴,在此先感谢。任何答案或评论将不胜感激。
解决方案
我使用这个答案创建了一个解决方案。我添加的部分写在带有注释的行下方#
。我承认这并不普遍和完美,但在我看来仍然足以完成工作。
import matplotlib.pyplot as plt
import numpy as np
def add_subplot_axes(ax, rect): # This is the function in the linked answer
fig = plt.gcf()
box = ax.get_position()
width = box.width
height = box.height
inax_position = ax.transAxes.transform(rect[0:2])
transFigure = fig.transFigure.inverted()
infig_position = transFigure.transform(inax_position)
x = infig_position[0]
y = infig_position[1]
width *= rect[2]
height *= rect[3]
subax = fig.add_axes([x,y,width,height])
x_labelsize = subax.get_xticklabels()[0].get_size()
y_labelsize = subax.get_yticklabels()[0].get_size()
x_labelsize *= rect[2]**0.5
y_labelsize *= rect[3]**0.5
subax.xaxis.set_tick_params(labelsize=x_labelsize)
subax.yaxis.set_tick_params(labelsize=y_labelsize)
return subax
# Modified part below
fig = plt.figure(figsize=(8,6))
ax = fig.add_subplot(111)
x_start, y_start = 0.4, 0.4
for i in range(3):
for j in range(3):
rect = [x_start+0.2*i, y_start+0.2*j, 0.15, 0.15]
ax_ = add_subplot_axes(ax,rect)
ax_.tick_params(labelsize=6)
plt.show()
推荐阅读
- python - 熊猫默认参数
- linux - 我在 ubuntu 14.04 上构建 gcc4.4.0 时的一些问题
- python - XGBoost 前向验证 feature_names 不匹配
- python - 在 pandas 中查找部分匹配并将值提取到新列
- css - 如何防止第三方浏览器扩展在 Web 应用程序上注入样式
- android - Android Chrome CanvasRenderingContext2D 画布比例不正确
- php - 在 WooCommerce“我的帐户”订单表中添加产品 SKU
- leaflet - 在传单中打开全屏 div 而不是圆形或标记弹出窗口?
- java - `xz-java` 库:有没有办法使用 «Extreme» 预设?
- python - 使用 image_dataset_from_directory 时未找到图像