I am hesitant to call the following a bug. It might just be a
misunderstanding on my side. Anyhow...
I am plotting a normal line collection and an "axvline" collection on
the same Axes. The latter is a collection of lines with data
coordinates for x and axes coordinates for y, using a blended
transform. I find that the data limits are messed up by the axvline
collection, which unexpectedly sets the bottom y limit to 0. This
results in a badly scaled plot when adjusting the view with
autoscale_view. Interestingly, this problem goes away if the normal
line collection is viewed first via autoscale_view, if the normal line
collection is replaced by a normal plot command, or if the axvline
collection is replaced by a normal axvline command. The problem
appears if the two collections are added to the Axes without an
autoscale_view in between.
I ran the code below with matplotlib trunk (SVN r8646) on Mac OS 10.5
with TkAgg backend. The same behaviour appears in matplotlib 0.99.1.1.
Please let me know if I am doing something obviously stupid,
especially in the way I create the axvline collection. At the moment I
am working around the behaviour by inserting an autoscale_view between
the two collections, so it is not a major show-stopper.
### Start code snippet ###
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
# Create sine wave to plot (offset from zero)
t = np.arange(200)
x = 10 + np.cos(2 * np.pi * t / 20)
ax = plt.gca()
# Add main plot as a line collection containing one segment
segments = mpl.collections.LineCollection([zip(t, x)])
# Output: Bbox(array([[ 0., 9.], [ 199., 11.]]))
# Uncommenting the line below actually fixes the problem...
# Add break lines as a collection of axvlines
breaks = np.arange(0, 200, 20)
transFixedY = mpl.transforms.blended_transform_factory(ax.transData,
break_lines = mpl.collections.LineCollection([[(s, 0), (s, 1)] for s
in breaks], transform=transFixedY, colors='k', linewidths=0.5,
# Output: Bbox(array([[ 0., 0.], [ 199., 11.]]))
# Notice that y0 is now 0 instead of the expected 9
# Autoscaling now inserts a lot of space below the original plot
### End code snippet ###