首页 > 解决方案 > jupyter notebook 缩放效果无法正常工作

问题描述

无法弄清楚代码有什么问题。我从 matplotlib 文档中获取它,似乎缩放效果出了点问题。看起来我的子图不在同一个图中,缩放功能无法以这种方式处理它。我很确定缩放功能本身没问题,但其余代码不行。

假设在同一范围内从顶部栏缩放到底部栏。

绘图图像

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.transforms as transforms
import matplotlib.axes as ax
import pandas as pd
from matplotlib.offsetbox import AnchoredText
from matplotlib.font_manager import FontProperties

from matplotlib.transforms import (
    Bbox, TransformedBbox, blended_transform_factory)
from mpl_toolkits.axes_grid1.inset_locator import (
    BboxPatch, BboxConnector, BboxConnectorPatch)

#
#ZOOM
#

def connect_bbox(bbox1, bbox2,
                 loc1a, loc2a, loc1b, loc2b,
                 prop_lines, prop_patches=None):
    if prop_patches is None:
        prop_patches = {
            **prop_lines,
            "alpha": prop_lines.get("alpha", 1) * 0.2,
            "clip_on": False,
        }

    c1 = BboxConnector(
        bbox1, bbox2, loc1=loc1a, loc2=loc2a, clip_on=False, **prop_lines)
    c2 = BboxConnector(
        bbox1, bbox2, loc1=loc1b, loc2=loc2b, clip_on=False, **prop_lines)

    bbox_patch1 = BboxPatch(bbox1, **prop_patches)
    bbox_patch2 = BboxPatch(bbox2, **prop_patches)

    p = BboxConnectorPatch(bbox1, bbox2,
                           # loc1a=3, loc2a=2, loc1b=4, loc2b=1,
                           loc1a=loc1a, loc2a=loc2a, loc1b=loc1b, loc2b=loc2b,
                           clip_on=False,
                           **prop_patches)

    return c1, c2, bbox_patch1, bbox_patch2, p


def zoom_effect(ax1, ax2, xmin, xmax, **kwargs):
    """
    Connect *ax1* and *ax2*. The *xmin*-to-*xmax* range in both axes will
    be marked.

    Parameters
    ----------
    ax1
        The main axes.
    ax2
        The zoomed axes.
    xmin, xmax
        The limits of the colored area in both plot axes.
    **kwargs
        Arguments passed to the patch constructor.
    """

    bbox = Bbox.from_extents(xmin, 0, xmax, 1)

    mybbox1 = TransformedBbox(bbox, ax1.get_xaxis_transform())
    mybbox2 = TransformedBbox(bbox, ax2.get_xaxis_transform())

    prop_patches = {**kwargs, "ec": "none", "alpha": 0.1}

    c1, c2, bbox_patch1, bbox_patch2, p = connect_bbox(
        mybbox1, mybbox2,
        loc1a=3, loc2a=2, loc1b=4, loc2b=1,
        prop_lines=kwargs, prop_patches=prop_patches)

    ax1.add_patch(bbox_patch1)
    ax2.add_patch(bbox_patch2)
    ax2.add_patch(c1)
    ax2.add_patch(c2)
    ax2.add_patch(p)

    return c1, c2, bbox_patch1, bbox_patch2, p

#
#ZOOM
#

fig, ax1=plt.subplots(figsize=(18, .5), tight_layout=False)
ax1.plot()

ticksx1=np.arange(0,1100,30.)
ticksy1=np.arange(0.,1,1)
ax1.xaxis.set_ticks(ticksx1)
ax1.yaxis.set_ticks(ticksy1)

plt.axis([0, 1100, 0.1, 1])
plt.show()

#2

fig, ax2=plt.subplots(figsize=(18,2.6), tight_layout=False)
ax2.plot()

ticksx2=np.arange(980,1080,7.)
ticksy2=np.arange(40,160,10)
ax2.xaxis.set_ticks(ticksx2)
ax2.yaxis.set_ticks(ticksy2)

plt.axis([979, 1083, 40, 160])
plt.grid(True)
zoom_effect(ax1, ax2, 980, 1080)

plt.show()

标签: pythonnumpymatplotlibplotzooming

解决方案


推荐阅读