Matplotlib 自动缩放 [英] Matplotlib autoscale
问题描述
我需要使用 matplotlib 获取自动拟合数据的图.这是我得到的代码:
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()
这会创建一个绘图,但是无论数据是什么,窗口总是相同的 (0-~.8),即使所有数据都在该窗口之外.生成的窗口无法缩小,只能放大,所以这是一个主要问题.我找不到任何设置任何大小的地方,也找不到有关默认值的详细信息.我需要窗口来自动适应数据,但我找不到任何执行此操作的函数(出于某种原因, autoscale_on(True) 没有执行此操作).数据变化很大,因此设置硬限制不是一种选择.我怎样才能让它正确显示?
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?
推荐答案
不确定这是否是您想要的,但如果这不是您想要的,我可以更改它.
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()
希望有所帮助.
这篇关于Matplotlib 自动缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!