I have a plot in matplotlib with multiple subplots(axes), and I want to annotate the points within the axes. However, subsequent axes overlay annotations from previous axes (eg annotation on subplot(4,4,1) goes under subplot(4,4,2)). I have set the annotation zorder nice and high, but to no avail :/
I've used a modified version of Joe Kington's awesome DataCursor for the annotations.
Any help would be greatly appreciated
Here's an example:
Dot Size. You can try to decrease marker size in your plot. This way they won't overlap and the patterns will be clearer.
Often you may use subplots to display multiple plots alongside each other in Matplotlib. Unfortunately, these subplots tend to overlap each other by default. The easiest way to resolve this issue is by using the Matplotlib tight_layout() function.
Use legend() method to avoid overlapping of labels and autopct. To display the figure, use show() method.
One way to do it is to pop the text created by annotate
out of the axes and add it to the figure. This way it will be displayed on top of all of the subplots.
As a quick example of the problem you're having:
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=5, ncols=5)
plt.setp(axes.flat, xticks=[], yticks=[], zorder=0)
ax = axes[0,0]
ax.annotate('Testing this out and seeing what happens', xy=(0.5, 0.5),
xytext=(1.1, .5), textcoords='axes fraction', zorder=100)
plt.show()
If we just pop the text object out of the axes and add it to the figure instead, it will be on top:
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=5, ncols=5)
plt.setp(axes.flat, xticks=[], yticks=[], zorder=0)
ax = axes[0,0]
ax.annotate('Testing this out and seeing what happens', xy=(0.5, 0.5),
xytext=(1.1, .5), textcoords='axes fraction', zorder=100)
fig.texts.append(ax.texts.pop())
plt.show()
You mentioned the DataCursor
snippet, and there you'd want to change the annotate
method:
def annotate(self, ax):
"""Draws and hides the annotation box for the given axis "ax"."""
annotation = ax.annotate(self.template, xy=(0, 0), ha='right',
xytext=self.offsets, textcoords='offset points', va='bottom',
bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.5),
arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0')
)
# Put the annotation in the figure instead of the axes so that it will be on
# top of other subplots.
ax.figure.texts.append(ax.texts.pop())
annotation.set_visible(False)
return annotation
I haven't tested the last bit, but it should work...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With