Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 (Matplotlib)如何在复杂条形图(具有多个断开的轴)中删除记号并向多个图层添加标签_Python 2.7_Matplotlib - Fatal编程技术网

Python 2.7 (Matplotlib)如何在复杂条形图(具有多个断开的轴)中删除记号并向多个图层添加标签

Python 2.7 (Matplotlib)如何在复杂条形图(具有多个断开的轴)中删除记号并向多个图层添加标签,python-2.7,matplotlib,Python 2.7,Matplotlib,我有一些数据需要在一个条形图中绘制,但由于存在一些异常值,我需要将y轴打断两次,以便在一个图中很好地拟合它 到目前为止,我尝试按照说明进行操作,但在删除图形“内部”部分的记号、显示部分数据的ylabel以及在图像中匹配xlabel时遇到了困难 #!/usr/bin/env python2 import numpy as np import matplotlib.pyplot as plt from matplotlib2tikz import save as tikzsave N = 5 in

我有一些数据需要在一个条形图中绘制,但由于存在一些异常值,我需要将y轴打断两次,以便在一个图中很好地拟合它

到目前为止,我尝试按照说明进行操作,但在删除图形“内部”部分的记号、显示部分数据的ylabel以及在图像中匹配xlabel时遇到了困难

#!/usr/bin/env python2
import numpy as np
import matplotlib.pyplot as plt
from matplotlib2tikz import save as tikzsave

N = 5
ind = np.arange(N)
width = 0.27

fig = plt.figure()

f, (ax, ax2, ax3) = plt.subplots(3, 1, sharex=True)

yvals = [15, 15, 15, 14, 176]
rects11 = ax.bar(ind, yvals, width, color='r')
rects12 = ax2.bar(ind, yvals, width, color='r')
rects13 = ax3.bar(ind, yvals, width, color='r')

zvals = [146, 269, 269, 221, 21830]
rects21 = ax3.bar(ind+width, zvals, width, color='g')
rects22 = ax2.bar(ind+width, zvals, width, color='g')
rects23 = ax.bar(ind+width, zvals, width, color='g')

kvals = [390, 620, 620, 559, 134720]
rects31 = ax3.bar(ind+2*width, kvals, width, color='b')
rects32 = ax2.bar(ind+2*width, kvals, width, color='b')
rects33 = ax.bar(ind+width*2, kvals, width, color='b')

ax.set_ylim(134500, 135200)  # outliers 1
ax2.set_ylim(21500, 22200)  # outlier 2
ax3.set_ylim(0, 700)  # most of the data

ax3.spines['top'].set_visible(False)
ax3.tick_params(labeltop=False)

ax2.spines['top'].set_visible(False)
ax2.spines['bottom'].set_visible(False)
ax2.tick_params(labeltop=False)
ax2.tick_params(labelbottom=False)

ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.tick_params(labeltop='off')
ax.tick_params(labelbottom='off')

d = .015  # how big to make the diagonal lines in axes coordinates
kwargs = dict(transform=ax.transAxes, color='k', clip_on=False)
ax.plot((-d, +d), (-d, +d), **kwargs)
ax.plot((1 - d, 1 + d), (-d, +d), **kwargs)

kwargs.update(transform=ax2.transAxes)
ax2.plot((-d, +d), (1 - d, 1 + d), **kwargs)
ax2.plot((1 - d, 1 + d), (1 - d, 1 + d), **kwargs)

kwargs = dict(transform=ax2.transAxes, color='k', clip_on=False)
ax.plot((-d, +d), (-d, +d), **kwargs)
ax.plot((1 - d, 1 + d), (-d, +d), **kwargs)

kwargs.update(transform=ax3.transAxes)
ax2.plot((-d, +d), (1 - d, 1 + d), **kwargs)
ax2.plot((1 - d, 1 + d), (1 - d, 1 + d), **kwargs)

ax2.set_ylabel('YLABEL')

ax.set_xticks([])
ax2.set_xticks([])
ax3.set_xticks(ind+width)
ax3.set_xticklabels( ('SMALLNAME1', 'SMALLNAME2', 'SMALLNAME3', 'BIGNAME1\nTWOLINES', 'BIGNAME2\n3LINES\nANOTHERLINE'), rotation=45, ha='right' )
ax2.legend( (rects12[0], rects22[0], rects32[0]), ('Type1', 'Type2', 'Type3') )

def autolabel(rects):
    for rect in rects:
        h = rect.get_height()
        ax3.text(rect.get_x()+rect.get_width()/2., 1.05*h, '%d'%int(h), ha='center', va='bottom')
        ax2.text(rect.get_x()+rect.get_width()/2., 1.05*h, '%d'%int(h), ha='center', va='bottom')
        ax.text(rect.get_x()+rect.get_width()/2., 1.05*h, '%d'%int(h), ha='center', va='bottom')

rectsArray = [rects11, rects12, rects13,
              rects21, rects22, rects23,
              rects31, rects32, rects33]

for r in rectsArray:
    autolabel(r)

tikzsave("testplot.tex")

plt.show()
从下面的图中,我想我就快到了,正如我们在生成的图中所看到的:

但是,我很难:

删除中间和顶部切割中的蜱痕< /LI>
  • 使XLabel完全显示在图像上
  • 在每个条的顶部显示y值(仅在底部效果良好)
  • 1。我试图改变利润率,但到目前为止运气不好

    最后,我不确定为什么y轴上的一些对角线切割没有完全显示。

    解决方案:

    • 通过使用ax.setxticks([])可以禁用x轴上的刻度,但首先必须从子批次定义中删除
      sharex=True
    • 我使用了下面的一段代码,其中包括旋转x-ticks
      f.autofmt_xdate()
    • 这里的错误是您定义文本y位置的方式。使用
      1.05*h
      ,通常是可以的,但是这里的值差异很大,因此,对于
      h
      的大值,文本的y位置会突然上升,并且不可见。修正只是使用
      h
      作为y位置
    另外,我建议您可以使用
    size=8
    或您认为合适的任何数字来调整文本的大小。至于为什么有些轴的不连续性会出现一半,我不确定我是否有答案,也许这与关闭轴和干扰动画有关

    图像输出:

    完整代码: 修改后的代码由注释标记:

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib2tikz import save as tikzsave
    
    N = 5
    ind = np.arange(N)
    width = 0.27
    
    # fig = plt.figure()    # code removed
    
    f, (ax, ax2, ax3) = plt.subplots(3, 1)
    
    yvals = [15, 15, 15, 14, 176]
    rects11 = ax.bar(ind, yvals, width, color='r')
    rects12 = ax2.bar(ind, yvals, width, color='r')
    rects13 = ax3.bar(ind, yvals, width, color='r')
    
    zvals = [146, 269, 269, 221, 21830]
    rects21 = ax3.bar(ind+width, zvals, width, color='g')
    rects22 = ax2.bar(ind+width, zvals, width, color='g')
    rects23 = ax.bar(ind+width, zvals, width, color='g')
    
    kvals = [390, 620, 620, 559, 134720]
    rects31 = ax3.bar(ind+2*width, kvals, width, color='b')
    rects32 = ax2.bar(ind+2*width, kvals, width, color='b')
    rects33 = ax.bar(ind+width*2, kvals, width, color='b')
    
    ax2.set_xticks([])  # code added
    ax.set_xticks([])   # code added
    
    ax.set_ylim(134500, 135200)  # outliers 1
    ax2.set_ylim(21500, 22200)  # outlier 2
    ax3.set_ylim(0, 700)  # most of the data
    
    ax3.spines['top'].set_visible(False)
    ax3.tick_params(labeltop=False)
    #
    ax2.spines['top'].set_visible(False)
    ax2.spines['bottom'].set_visible(False)
    ax2.tick_params(labeltop=False)
    ax2.tick_params(labelbottom=False)
    
    ax.spines['top'].set_visible(False)
    ax.spines['bottom'].set_visible(False)
    ax.tick_params(labeltop='off')
    ax.tick_params(labelbottom='off')
    
    d = .015  # how big to make the diagonal lines in axes coordinates
    kwargs = dict(transform=ax.transAxes, color='k', clip_on=False)
    ax.plot((-d, +d), (-d, +d), **kwargs)
    ax.plot((1 - d, 1 + d), (-d, +d), **kwargs)
    
    kwargs.update(transform=ax2.transAxes)
    ax2.plot((-d, +d), (1 - d, 1 + d), **kwargs)
    ax2.plot((1 - d, 1 + d), (1 - d, 1 + d), **kwargs)
    
    kwargs = dict(transform=ax2.transAxes, color='k', clip_on=False)
    ax.plot((-d, +d), (-d, +d), **kwargs)
    ax.plot((1 - d, 1 + d), (-d, +d), **kwargs)
    
    kwargs.update(transform=ax3.transAxes)
    ax2.plot((-d, +d), (1 - d, 1 + d), **kwargs)
    ax2.plot((1 - d, 1 + d), (1 - d, 1 + d), **kwargs)
    
    ax2.set_ylabel('YLABEL')
    
    ax.set_xticks([])
    ax2.set_xticks([])
    ax3.set_xticks(ind+width)
    ax3.set_xticklabels( ('SMALLNAME1', 'SMALLNAME2', 'SMALLNAME3', 'BIGNAME1\nTWOLINES', 'BIGNAME2\n3LINES\nANOTHERLINE'), ha='right')
    ax2.legend((rects12[0], rects22[0], rects32[0]), ('Type1', 'Type2', 'Type3'))
    f.autofmt_xdate()   # code added
    
    def autolabel(rects):
        for rect in rects:
            h = rect.get_height()
            ax3.text(rect.get_x()+rect.get_width()/2., h, '%d'%int(h), ha='center', va='bottom')    # height altered
            ax2.text(rect.get_x()+rect.get_width()/2., h, '%d'%int(h), ha='center', va='bottom')
            ax.text(rect.get_x()+rect.get_width()/2., h, '%d'%int(h), ha='center', va='bottom')
    
    rectsArray = [rects11, rects12, rects13,
                  rects21, rects22, rects23,
                  rects31, rects32, rects33]
    
    for r in rectsArray:
        autolabel(r)
    
    tikzsave("testplot.tex")
    
    plt.show()