Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib autoscale

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?

like image 773
Elliot Avatar asked Jul 09 '10 16:07

Elliot


2 Answers

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.

like image 124
River Avatar answered Sep 18 '22 15:09

River


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.

like image 22
bsisco Avatar answered Sep 20 '22 15:09

bsisco