0

我正在构建一个具有作为散点图的主轴和聚焦于主轴的特定区域的缩放轴的图形,两者都有网格线。当我将缩放轴作为插图放置时,它会“覆盖”一些主轴数据。我希望能够zorder=100通过缩放轴显示主轴数据( ),因此我将缩放轴设置为透明(alpha=0)。最后,我希望主轴网格线在与缩放轴 ( ) 相遇时“截断”,zorder=10但我想显示缩放轴网格线 ( zorder=50)。这可能吗?以下是我的尝试:

import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import mark_inset
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes
import numpy as np
fig = plt.figure(figsize=(10,7.5))
gs = matplotlib.gridspec.GridSpec(1, 2, width_ratios=[20,1], height_ratios=[1])
ax = plt.subplot(111)

## data
xx = np.linspace(1,100,num=100) + 20 * np.random.normal(0,1,100)
yy = np.linspace(1,100,num=100) + 10 * np.random.normal(0,1,100)

## scatter
sc = ax.scatter(xx, yy, s=250, alpha=0.35, zorder=100)
ax.plot(np.linspace(-100,200,301), np.linspace(-100,200,301),)
ax.set_xlim((0, 100))
ax.set_ylim((0, 100))
ax.grid(linestyle="--", zorder=10)

## zoom
axins = zoomed_inset_axes(ax, 2, loc="upper left")
scins = axins.scatter(xx, yy, s=100, alpha=0.35, zorder=50, marker=".", c="red")
axins.plot(np.linspace(-100,200,301), np.linspace(-100,200,301), c="red")
axins.set_xlim((70, 90))
axins.set_ylim((70, 90))
mark_inset(ax, axins, loc1=1, loc2=4, fc="none", ec="0.5")
axins.grid(linestyle="--", zorder=50)
plt.show()

在此处输入图像描述

特别是,附近的蓝色数据点之一x=80被切断。我可以设置axins.patch.set_alpha(0.0),但它不会删除主网格线。

4

2 回答 2

1

一种选择确实是在ax存在的位置放置一个白色补丁(白色矩形),axins并将该补丁的 zorder 设置为高于网格线的 zorder,但低于分散的 zorder。

# Set axins' background patch invisible
axins.patch.set_visible(False)
# Create a new patch at the position of the axins axes.
rect = matplotlib.patches.Rectangle((0,0), 1,1,
          fill=True, facecolor="white", edgecolor="red",zorder=25,
          transform=axins.transAxes)
ax.add_patch(rect)
于 2019-09-14T02:19:59.457 回答
0

感谢@ImportanceOfBeingErnest 的建议。它可以添加一个具有以下中间值的矩形zorderax我已经离开了矩形的红色轮廓):

import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import mark_inset
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes
import matplotlib.patches as patches
import numpy as np

## data
xx = np.linspace(1,100,num=100) + 20 * np.random.normal(0,1,100)
yy = np.linspace(1,100,num=100) + 10 * np.random.normal(0,1,100)

## fig
fig = plt.figure(figsize=(10,7.5))
gs = matplotlib.gridspec.GridSpec(1, 2, width_ratios=[20,1], height_ratios=[1])
ax = plt.subplot(111)

## scatter
sc = ax.scatter(xx, yy, s=250, alpha=0.35, zorder=100)
ax.plot(np.linspace(-100,200,301), np.linspace(-100,200,301))
ax.set_xlim((0, 100))
ax.set_ylim((0, 100))
ax.grid(linestyle="--", zorder=10)
ax.patches.extend([patches.Rectangle((0.2, 0.6), 0.4, 0.4,
                                  fill=True, facecolor="white", edgecolor="red",zorder=25,
                                  transform=ax.transAxes, figure=ax)])

## zoom
axins = zoomed_inset_axes(ax, 2,
                          bbox_to_anchor=(0.6, 1.0, 0.0, 0.0),
                          bbox_transform=ax.transAxes)
scins = axins.scatter(xx, yy, s=100, alpha=0.35, zorder=50, marker=".", c="red")
axins.plot(np.linspace(-100,200,301), np.linspace(-100,200,301), c="red")
axins.set_xlim((70, 90))
axins.set_ylim((70, 90))
axins.patch.set_alpha(0.0)
mark_inset(ax, axins, loc1=1, loc2=4, fc="none", ec="0.5")
axins.grid(linestyle="--", zorder=50)
plt.show()

在此处输入图像描述

于 2019-09-13T22:34:00.080 回答