2011-04-13 116 views
55

我试图创建一个使用pyplot具有不连续的x轴的情节。这是画的常用方法是,轴将是这样的:Python/Matplotlib - 有没有办法让一个不连续的轴?

(值)---- ---- //(后值)

其中//表示你跳过(值)和(后面的值)之间的所有内容。

我一直没能找到任何这样的例子,所以我想知道如果它甚至是可能的。我知道你可以通过不连续的方式加入数据,例如财务数据,但是我想让这个轴的跳转更加明确。目前我只是使用子图,但我最终希望最终能够在同一张图上显示所有内容。

回答

56

保罗的回答是这样的一个完全正常的方法。

但是,如果您不想进行自定义转换,则可以使用两个子图创建相同的效果。

matplotlib示例中只有一个an excellent example of this written by Paul Ivanov(仅在当前的git提示中,因为它仅在几个月前提交,尚未在网页上提供),而不是从头开始构建示例。

这只是本示例的一个简单修改,它具有不连续的x轴而不是y轴。 (这就是为什么我在做这个职位的CW)

基本上,你只是做这样的事情:

import matplotlib.pylab as plt 
import numpy as np 

# If you're not familiar with np.r_, don't worry too much about this. It's just 
# a series with points from 0 to 1 spaced at 0.1, and 9 to 10 with the same spacing. 
x = np.r_[0:1:0.1, 9:10:0.1] 
y = np.sin(x) 

fig,(ax,ax2) = plt.subplots(1, 2, sharey=True) 

# plot the same data on both axes 
ax.plot(x, y, 'bo') 
ax2.plot(x, y, 'bo') 

# zoom-in/limit the view to different portions of the data 
ax.set_xlim(0,1) # most of the data 
ax2.set_xlim(9,10) # outliers only 

# hide the spines between ax and ax2 
ax.spines['right'].set_visible(False) 
ax2.spines['left'].set_visible(False) 
ax.yaxis.tick_left() 
ax.tick_params(labeltop='off') # don't put tick labels at the top 
ax2.yaxis.tick_right() 

# Make the spacing between the two axes a bit smaller 
plt.subplots_adjust(wspace=0.15) 

plt.show() 

enter image description here

要添加破轴线//效果,我们可以做这(再次,从保罗·伊万诺夫的例子修改):

import matplotlib.pylab as plt 
import numpy as np 

# If you're not familiar with np.r_, don't worry too much about this. It's just 
# a series with points from 0 to 1 spaced at 0.1, and 9 to 10 with the same spacing. 
x = np.r_[0:1:0.1, 9:10:0.1] 
y = np.sin(x) 

fig,(ax,ax2) = plt.subplots(1, 2, sharey=True) 

# plot the same data on both axes 
ax.plot(x, y, 'bo') 
ax2.plot(x, y, 'bo') 

# zoom-in/limit the view to different portions of the data 
ax.set_xlim(0,1) # most of the data 
ax2.set_xlim(9,10) # outliers only 

# hide the spines between ax and ax2 
ax.spines['right'].set_visible(False) 
ax2.spines['left'].set_visible(False) 
ax.yaxis.tick_left() 
ax.tick_params(labeltop='off') # don't put tick labels at the top 
ax2.yaxis.tick_right() 

# Make the spacing between the two axes a bit smaller 
plt.subplots_adjust(wspace=0.15) 

# This looks pretty good, and was fairly painless, but you can get that 
# cut-out diagonal lines look with just a bit more work. The important 
# thing to know here is that in axes coordinates, which are always 
# between 0-1, spine endpoints are at these locations (0,0), (0,1), 
# (1,0), and (1,1). Thus, we just need to put the diagonals in the 
# appropriate corners of each of our axes, and so long as we use the 
# right transform and disable clipping. 

d = .015 # how big to make the diagonal lines in axes coordinates 
# arguments to pass plot, just so we don't keep repeating them 
kwargs = dict(transform=ax.transAxes, color='k', clip_on=False) 
ax.plot((1-d,1+d),(-d,+d), **kwargs) # top-left diagonal 
ax.plot((1-d,1+d),(1-d,1+d), **kwargs) # bottom-left diagonal 

kwargs.update(transform=ax2.transAxes) # switch to the bottom axes 
ax2.plot((-d,d),(-d,+d), **kwargs) # top-right diagonal 
ax2.plot((-d,d),(1-d,1+d), **kwargs) # bottom-right diagonal 

# What's cool about this is that now if we vary the distance between 
# ax and ax2 via f.subplots_adjust(hspace=...) or plt.subplot_tool(), 
# the diagonal lines will move accordingly, and stay right at the tips 
# of the spines they are 'breaking' 

plt.show() 

enter image description here

+5

我自己不能说得更好;) – 2011-08-02 18:13:57

+1

只有子图的比例为1:1时,才会使'//效果的方法好用。你知道如何使它以任何比例工作,例如'GridSpec(width_ratio = [N,M])'? – 2014-03-17 23:40:46

23

我看到这个功能的许多建议,但没有表明它已经实施。这是一个可行的解决方案。它对x轴应用了阶跃函数变换。这是很多代码,但它很简单,因为它大部分都是样板自定义比例。我没有添加任何图形来指示休息的位置,因为这是一个风格问题。祝你好运完成这份工作。

from matplotlib import pyplot as plt 
from matplotlib import scale as mscale 
from matplotlib import transforms as mtransforms 
import numpy as np 

def CustomScaleFactory(l, u): 
    class CustomScale(mscale.ScaleBase): 
     name = 'custom' 

     def __init__(self, axis, **kwargs): 
      mscale.ScaleBase.__init__(self) 
      self.thresh = None #thresh 

     def get_transform(self): 
      return self.CustomTransform(self.thresh) 

     def set_default_locators_and_formatters(self, axis): 
      pass 

     class CustomTransform(mtransforms.Transform): 
      input_dims = 1 
      output_dims = 1 
      is_separable = True 
      lower = l 
      upper = u 
      def __init__(self, thresh): 
       mtransforms.Transform.__init__(self) 
       self.thresh = thresh 

      def transform(self, a): 
       aa = a.copy() 
       aa[a>self.lower] = a[a>self.lower]-(self.upper-self.lower) 
       aa[(a>self.lower)&(a<self.upper)] = self.lower 
       return aa 

      def inverted(self): 
       return CustomScale.InvertedCustomTransform(self.thresh) 

     class InvertedCustomTransform(mtransforms.Transform): 
      input_dims = 1 
      output_dims = 1 
      is_separable = True 
      lower = l 
      upper = u 

      def __init__(self, thresh): 
       mtransforms.Transform.__init__(self) 
       self.thresh = thresh 

      def transform(self, a): 
       aa = a.copy() 
       aa[a>self.lower] = a[a>self.lower]+(self.upper-self.lower) 
       return aa 

      def inverted(self): 
       return CustomScale.CustomTransform(self.thresh) 

    return CustomScale 

mscale.register_scale(CustomScaleFactory(1.12, 8.88)) 

x = np.concatenate((np.linspace(0,1,10), np.linspace(9,10,10))) 
xticks = np.concatenate((np.linspace(0,1,6), np.linspace(9,10,6))) 
y = np.sin(x) 
plt.plot(x, y, '.') 
ax = plt.gca() 
ax.set_xscale('custom') 
ax.set_xticks(xticks) 
plt.show() 

enter image description here

+0

我想这将只是现在做的事。这将是我第一次搞乱自定义轴,所以我们只需要看看它是如何发展的。 – 2011-04-14 19:35:40

+0

'InvertedCustomTransform'的'def transform'中有一个小错字,它应该读取'self.upper'而不是'upper'。尽管感谢这个伟大的例子! – 2012-01-10 17:11:22

+0

你可以添加几行来演示如何使用你的类吗? – 2015-04-23 13:34:35

0

关于弗雷德里克诺德的问题如何在使用比例不等于1:1的网格时允许对角“断裂”线的平行定向,根据保罗伊万诺夫和乔金顿的建议进行以下更改可能会有帮助。宽度比率可以使用变量n和m来改变。

import matplotlib.pylab as plt 
import numpy as np 
import matplotlib.gridspec as gridspec 

x = np.r_[0:1:0.1, 9:10:0.1] 
y = np.sin(x) 

n = 5; m = 1; 
gs = gridspec.GridSpec(1,2, width_ratios = [n,m]) 

plt.figure(figsize=(10,8)) 

ax = plt.subplot(gs[0,0]) 
ax2 = plt.subplot(gs[0,1], sharey = ax) 
plt.setp(ax2.get_yticklabels(), visible=False) 
plt.subplots_adjust(wspace = 0.1) 

ax.plot(x, y, 'bo') 
ax2.plot(x, y, 'bo') 

ax.set_xlim(0,1) 
ax2.set_xlim(10,8) 

# hide the spines between ax and ax2 
ax.spines['right'].set_visible(False) 
ax2.spines['left'].set_visible(False) 
ax.yaxis.tick_left() 
ax.tick_params(labeltop='off') # don't put tick labels at the top 
ax2.yaxis.tick_right() 

d = .015 # how big to make the diagonal lines in axes coordinates 
# arguments to pass plot, just so we don't keep repeating them 
kwargs = dict(transform=ax.transAxes, color='k', clip_on=False) 

on = (n+m)/n; om = (n+m)/m; 
ax.plot((1-d*on,1+d*on),(-d,d), **kwargs) # bottom-left diagonal 
ax.plot((1-d*on,1+d*on),(1-d,1+d), **kwargs) # top-left diagonal 
kwargs.update(transform=ax2.transAxes) # switch to the bottom axes 
ax2.plot((-d*om,d*om),(-d,d), **kwargs) # bottom-right diagonal 
ax2.plot((-d*om,d*om),(1-d,1+d), **kwargs) # top-right diagonal 

plt.show() 
6

检查brokenaxes包:

import matplotlib.pyplot as plt 
from brokenaxes import brokenaxes 
import numpy as np 

fig = plt.figure(figsize=(5,2)) 
bax = brokenaxes(xlims=((0, .1), (.4, .7)), ylims=((-1, .7), (.79, 1)), hspace=.05) 
x = np.linspace(0, 1, 100) 
bax.plot(x, np.sin(10 * x), label='sin') 
bax.plot(x, np.cos(10 * x), label='cos') 
bax.legend(loc=3) 
bax.set_xlabel('time') 
bax.set_ylabel('value') 

example from brokenaxes

+0

安装完毕后,Pycharm Community 2016.3.2不能从'brokenaxes导入brokenaxes''。 @ ben.dichter – 2017-08-21 07:24:41

+0

有一个错误。我修好了它。请运行'pip install brokenaxes == 0.2'来安装固定版本的代码。 – 2017-08-22 13:35:52

+0

似乎与ax.grid交互不良(True) – innisfree 2017-12-12 00:09:15

相关问题