This repository has been archived by the owner on Aug 20, 2018. It is now read-only.
forked from alexras/boomslang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BrokenAxisPlot.py
181 lines (131 loc) · 5.41 KB
/
BrokenAxisPlot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import matplotlib.axes
import matplotlib.transforms
import matplotlib.ticker as mticker
from boomslang import Plot
import numpy as np
class BrokenAxesPiece(matplotlib.axes.Axes):
def __init__(self, fig, rect, axisbg=None, frameon=True,
sharex=None, sharey=None, label='', xscale=None,
yscale=None,
xform=None,
**kwargs):
if xform is None:
self.xform = lambda x: x
else:
self.xform = xform
super(BrokenAxesPiece,self).__init__(fig, rect, axisbg, frameon,
sharex, sharey, label, xscale, yscale, **kwargs)
def set_position(self, pos, which='both'):
if which == 'both' or which == 'original':
super(BrokenAxesPiece, self).set_position(pos, 'both')
elif which == 'active':
l,b,w,h = pos.get_points().flatten().tolist()
l,b,w,h = self.xform(l,b,w,h)
new_pos = matplotlib.transforms.Bbox([[l,b],[w,h]])
super(BrokenAxesPiece, self).set_position(new_pos, 'active')
class BrokenAxisPlot(Plot):
"""
Represents a single broken-axis plot. Not embeddable into nested
PlotLayouts at the moment.
"""
def __init__(self,
break_points,
break_line_size = 0.015,
break_hspace = 0.05,
break_ratio = 0.5):
super(BrokenAxisPlot,self).__init__()
self.break_points = break_points
"""
A 2-tuple that defines the start and end points of the gap on the y-axis
"""
self.break_line_size = break_line_size
"""
The size of the broken axis lines
"""
self.break_hspace = break_hspace
"""
The amount of horizontal space to put between the break points
"""
self.break_ratio = break_ratio
"""
Defines how to divvy up space between the two broken parts of the plot
"""
self.tight = False
self.allows_tight = False
def subplot(self, fig, row, column, position, projection):
# Step 0, plot in normal space to build up base fig and stats
orig_ax = fig.add_subplot(row, column, position)
# Save legend and set it to None
orig_legend = self.legend
self.legend = None
handles = self.drawPlot(fig, orig_ax)
nticks = len(orig_ax.get_yticks())
if True:
for side in ['top','bottom','left','right']:
orig_ax.spines[side].set_visible(False)
orig_ax.grid(False)
orig_ax.cla()
orig_ax.xaxis.set_visible(False)
orig_ax.set_yticks([0])
orig_ax.set_yticklabels([" "])
hs = self.break_hspace * 1
def top_xform(l,b,w,h):
return (l,b + (h-b+hs) * self.break_ratio,w,h)
def bot_xform(l,b,w,h):
return (l,b,w,h-(h-b+hs) * (1-self.break_ratio))
# Hork
r = orig_ax.transAxes._boxout._bbox
ax2 = BrokenAxesPiece(fig, r, sharex=orig_ax, xform=bot_xform)
fig.add_axes(ax2)
handles = self.drawPlot(fig, ax2)
# Re-enable the legend
self.legend = orig_legend
ax = BrokenAxesPiece(fig, r, sharex=orig_ax, xform=top_xform)
fig.add_axes(ax)
handles = self.drawPlot(fig, ax)
# Set Limits
(y_bot, y_top) = ax.get_ylim()
ax2.set_ylim(y_bot, self.break_points[0])
ax.set_ylim(self.break_points[1], y_top)
# Re-tick things
numticks2 = (self.break_ratio * nticks * (1 / (1+hs))) - 1
numticks = ((1 - self.break_ratio) * nticks * (1/(1+hs)))
numticks2 = max(numticks2, 2)
numticks = max(numticks, 2)
ax2.yaxis.set_major_locator(mticker.LinearLocator(numticks=numticks2))
ax.yaxis.set_major_locator(mticker.LinearLocator(numticks=numticks))
ax2.set_xticks(orig_ax.get_xticks())
ax2.set_xticklabels([t.get_text() for t in orig_ax.get_xticklabels()])
ax.spines['bottom'].set_visible(False)
ax2.spines['top'].set_visible(False)
ax.xaxis.tick_top()
ax.tick_params(labeltop='off')
ax2.xaxis.tick_bottom()
orig_ax.set_ylabel(ax.get_ylabel())
ax.set_ylabel("")
ax2.set_ylabel("")
ax.set_xlabel("")
kwargs = dict(transform=ax.transAxes, color='k', clip_on=False)
ax.plot((-self.break_line_size,
self.break_line_size),
(-self.break_line_size/(1-self.break_ratio),
self.break_line_size/(1-self.break_ratio)),
**kwargs) # top-left
ax.plot((1-self.break_line_size,
1+self.break_line_size),
(-self.break_line_size/(1-self.break_ratio),
self.break_line_size/(1-self.break_ratio)),
**kwargs) # top-right
kwargs = dict(transform=ax2.transAxes, color='k', clip_on=False)
ax2.plot((-self.break_line_size,
self.break_line_size),
(1-self.break_line_size/(self.break_ratio),
1+self.break_line_size/(self.break_ratio)),
**kwargs) # bottom-left
ax2.plot((1-self.break_line_size,
1+self.break_line_size),
(1-self.break_line_size/(self.break_ratio),
1+self.break_line_size/(self.break_ratio)),
**kwargs) # bottom-right
ax2.set_xlim(*ax.get_xlim())
return handles