当值“包装"时防止图连接.在matplotlib图中 [英] Preventing plot joining when values "wrap" in matplotlib plots

查看:84
本文介绍了当值“包装"时防止图连接.在matplotlib图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在密谋右提升

I'm plotting right ascension ephemerides for planets, which have the property that they are cyclical: they hit a maximum value, 24, and then start again at 0. When I plot these using matplotlib, the "jump" from 24 to zero is joined so that I get horizontal lines running across my figure:

如何消除这些行? matplotlib中是否有一种方法,或者是一种在发生跳转的点之间拆分列表的方法.

How can I eliminate these lines? Is there an approach in matplotlib, or perhaps a way to split the lists at between the points where the jump occurs.

生成上图的代码:

from __future__ import division

import ephem
import matplotlib
import matplotlib.pyplot
import math

fig, ax = matplotlib.pyplot.subplots()

ax.set(xlim=[0, 24])
ax.set(ylim=[min(date_range), max(date_range)])

ax.plot([12*ep.ra/math.pi for ep in [ephem.Jupiter(base_date + d) for d in date_range]], date_range,
        ls='-', color='g', lw=2)
ax.plot([12*ep.ra/math.pi for ep in [ephem.Venus(base_date + d) for d in date_range]], date_range,
        ls='-', color='r', lw=1)
ax.plot([12*ep.ra/math.pi for ep in [ephem.Sun(base_date + d) for d in date_range]], date_range,
        ls='-', color='y', lw=3)

推荐答案

以下是一个生成器函数,用于查找包装"数据的连续区域:

Here is a generator function that finds the contiguous regions of 'wrapped' data:

import numpy as np

def unlink_wrap(dat, lims=[-np.pi, np.pi], thresh = 0.95):
    """
    Iterate over contiguous regions of `dat` (i.e. where it does not
    jump from near one limit to the other).

    This function returns an iterator object that yields slice
    objects, which index the contiguous portions of `dat`.

    This function implicitly assumes that all points in `dat` fall
    within `lims`.

    """
    jump = np.nonzero(np.abs(np.diff(dat)) > ((lims[1] - lims[0]) * thresh))[0]
    lasti = 0
    for ind in jump:
        yield slice(lasti, ind + 1)
        lasti = ind + 1
    yield slice(lasti, len(dat))

一个示例用法是

x = np.arange(0, 100, .1)
y = x.copy()

lims = [0, 24]

x = (x % lims[1])

fig, ax = matplotlib.pyplot.subplots()

for slc in unlink_wrap(x, lims):
    ax.plot(x[slc], y[slc], 'b-', linewidth=2)

ax.plot(x, y, 'r-', zorder=-10)
ax.set_xlim(lims)

哪个给出下图.请注意,蓝线(利用unlink_wrap)是折断的,并且显示了标绘的红线以供参考.

Which gives the figure below. Note that the blue lines (which utilize unlink_wrap) are broken and the standard-plotted red lines are shown for reference.

这篇关于当值“包装"时防止图连接.在matplotlib图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆