开发者

Matplotlib autoscale

开发者 https://www.devze.com 2023-01-06 11:11 出处:网络
I need to get a plot that fits the data automatically using matplotlib. This is the code I was given:

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_o开发者_如何学编程n(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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号