Issue
I need add secondary label to my y axios in matplotlib chat.
Solution
I have sample code I used already.
def annotate_yrange(self, ymin, ymax,
label=None, fontsize=12,
offset=-0.1,
width=-0.1,
text_kwargs={'rotation': 'horizontal'},
ax=None,
patch_kwargs={'facecolor': 'white'},
line_kwargs={'color': 'black'},
):
if ax is None:
ax = plt.gca()
trans = transforms.blended_transform_factory(
ax.transAxes, ax.transData)
rect = Rectangle((offset, ymin), width=width, height=ymax -
ymin, transform=trans, clip_on=False, **patch_kwargs)
ax.add_patch(rect)
min_delimiter = Line2D((offset+width, offset), (ymin, ymin),
transform=trans, clip_on=False, **line_kwargs, linewidth=.7)
max_delimiter = Line2D((offset+width, offset), (ymax, ymax),
transform=trans, clip_on=False, **line_kwargs, linewidth=.7)
mid_delimiter = Line2D((offset+width, offset + width), (ymin, ymax),
transform=trans, clip_on=False, **line_kwargs, linewidth=.7)
ax.add_artist(min_delimiter)
ax.add_artist(max_delimiter)
ax.add_artist(mid_delimiter)
# label
if label:
x = offset + 0.5 * width
y = ymin + 0.5 * (ymax - ymin)
ax.text(x, y, label, fontsize=fontsize, horizontalalignment='center', verticalalignment='center',
clip_on=False, transform=trans, **text_kwargs)
.........
width = -0.12
offsets = [0, -0.12]
for ii, (level, offset) in enumerate(zip((labelPos, mlabelPos), offsets)):
for jj, (ymin, ymax) in enumerate(level):
if ii == 0:
temp_labels[jj] = '\n'.join(temp_labels[jj][i:i+17]
for i in range(0, len(temp_labels[jj]), 17))
self.annotate_yrange(ymin, ymax, temp_labels[jj], fontsize=10, offset=offset, width=width, text_kwargs={'rotation': 0})
else:
temp_mlabels[jj] = '\n'.join(temp_mlabels[jj][i:i+17]
for i in range(0, len(temp_mlabels[jj]), 17))
self.annotate_yrange(ymin, ymax, temp_mlabels[jj], fontsize=10, offset=offset, width=width, text_kwargs={'rotation': 0})
Answered By - Forest 1
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.