views:

75

answers:

2

I need to get a plot that fits the data automatically using matplotlib. This is the code I was given:

import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
....
lines = LineCollection(mpl.line_holder, colors=mpl.colorholder , linestyle='solid')
plt.axes().add_collection(lines)
plt.axes().set_aspect('equal', 'datalim')
plt.draw()
plt.show()

This creates a plot, however the window is always the same (0-~.8) no matter what the data is, even if all of the data is outside that window. The resulting window has no ability to zoom out, only in, so this is a major problem. I can't find anywhere where any kind of sizing is set, nor can II find details on what defaults are. I need the window to automatically fit the data, but I can't find any function that does it (for some reason, autoscale_on(True) doesn't do it). The data is highly variable, so setting hard limits is not an option. How can i get this to display properly?

A: 

Have a look at Eli Bendersky's Website, specifically this post. The example at the bottom of the post can be downloaded. It allows you to set whether the x axis will follow the plot or will remain static while the y axis changes with the data.

A: 

Not sure if this what you wanted, but I can change it if this was not what you were looking for.

import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection

import pylab as p

fig = plt.figure()
pts1 = []
pts2 = []
for i in range(100):
    pts1.append([i,i])
    pts2.append([-i-3,-i])
lines = LineCollection([pts1,pts2], linestyles='solid')
subplt = fig.add_subplot(111,aspect='equal')
subplt.add_collection(lines)
subplt.autoscale_view(True,True,True)
p.show()

Hope that helps.

River
autoscale_view(True,True,True) fixed the issue. Now if I could just find an explanation of what it technically does in the horrible documentation.
Elliot
Should be somewhere in here: http://matplotlib.sourceforge.net/api/axes_api.html. Documentation isn't too good though.
River