【问题标题】:How to round connection for Matplotlib axis spines如何对 Matplotlib 轴脊进行舍入连接
【发布时间】:2021-11-19 22:58:54
【问题描述】:

这是一个相当简单的问题。我正在创建一个具有多个插入轴的 Matplotlib 图。

我已删除刻度线,以便每个插入轴的插入脊以 90 度角相交。但是,当您向该图形添加图例时,它会添加一个图例对象,该图例对象的每个“边缘”都有略微圆形的脊椎连接。

有没有一种简单的方法可以为插入轴或更一般的轴的刺获得类似的效果?

我的代码:

fig, e = plt.subplots(figsize=(10, 5))
    
a = e.inset_axes([.1, .1, .1, .2])
b = e.inset_axes([.3, .1, .1, .2])

for ax in [a, b]:
    
    ax.set_yticks([])
    ax.set_xticks([])

当前输出:

期望的输出:

【问题讨论】:

  • 你能显示你用来添加图例的代码吗?
  • @HåkonHægland 图例的代码只是ax.legend()的默认值
  • 图例对象没有刺。它有类似FancyBboxPatch 的东西。 previous question 一直没有得到答复,这可能意味着没有简单的方法。
  • @JohanC 有趣。感谢您的澄清。

标签: python matplotlib plot


【解决方案1】:

一个想法是隐藏刺,并在同一位置绘制FancyBboxPatch。设置clip_on=False 可以避免框被不恰当地剪裁。

将圆角框分配给ax_a.patch 使得主斧和插入斧内的对象的剪切都起作用。

这里是修改左插入轴的示例代码。

import matplotlib.pyplot as plt
from matplotlib.patches import FancyBboxPatch
import numpy as np

fig, ax_e = plt.subplots(figsize=(10, 5))

ax_a = ax_e.inset_axes([.1, .1, .1, .2])
ax_b = ax_e.inset_axes([.3, .1, .1, .2])

for ax in [ax_a, ax_b]:
    ax.set_yticks([])
    ax.set_xticks([])
for s in ax_a.spines:
    ax_a.spines[s].set_visible(False)
p_bbox = FancyBboxPatch((0, 0), 1, 1,
                        boxstyle="round,pad=-0.0040,rounding_size=0.2",
                        ec="black", fc="white", clip_on=False, lw=1,
                        mutation_aspect=1,
                        transform=ax_a.transAxes)
ax_a.add_patch(p_bbox)
ax_a.patch = p_bbox

t = np.linspace(0, 2 * np.pi, 100)
ax_a.plot(np.sin(3 * t), np.sin(4 * t))  # drawing some curve
ax_a.plot([-2, 2], [-2, 2], 'g', lw=10, alpha=0.3)  # diagonal line to check the clipping in the corners
ax_a.set_xlim(-1.1, 1.1)
ax_a.set_ylim(-1.1, 1.1)

ax_e.set_facecolor('tomato')  # show the clipping around the inset axes

plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-02
    • 2020-03-27
    • 1970-01-01
    相关资源
    最近更新 更多