Python 2.7 (Matplotlib)如何在复杂条形图(具有多个断开的轴)中删除记号并向多个图层添加标签
我有一些数据需要在一个条形图中绘制,但由于存在一些异常值,我需要将y轴打断两次,以便在一个图中很好地拟合它 到目前为止,我尝试按照说明进行操作,但在删除图形“内部”部分的记号、显示部分数据的ylabel以及在图像中匹配xlabel时遇到了困难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
#!/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>
- 通过使用ax.setxticks([])可以禁用x轴上的刻度,但首先必须从子批次定义中删除
sharex=True
- 我使用了下面的一段代码,其中包括旋转x-ticks
f.autofmt_xdate()
- 这里的错误是您定义文本y位置的方式。使用
,通常是可以的,但是这里的值差异很大,因此,对于1.05*h
的大值,文本的y位置会突然上升,并且不可见。修正只是使用h
作为y位置h
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()