[英]matplotlib autoscale(axis='y') after slicing plot with set_xlim()
作為演示,我正在繪制x ^ 0到x ^ 9,x值在10到20之間。
然后我正在切片那些圖像,以便我有9個切片:
x =(10到11),(11到12)等等到(18到19)
我希望我的圖像被裁剪,以便y值始終在每個切片中從上到下展開,但我得到的是自動縮放始終縮放到完整數據集而不是當前切片。
import matplotlib.pyplot as plt
import numpy as np
# create some test data
for i in range(10):
x = np.arange(10,20)
y = x**i
plt.plot(x,y,c='red',marker='.',ms=2)
# get all x values in chart and reduce to a sorted list set
xd = []
for n in range(len(plt.gca().get_lines())):
line = plt.gca().get_lines()[n]
xd.append((line.get_xdata()).tolist())
xd = [item for sublist in xd for item in sublist]
xd = sorted(list(set(xd)))
# attempt to plot slices of x with autoscaled y
ax = plt.gca()
for i in range(len(xd)-1):
ax.set_xlim([xd[i],xd[i+1]])
ax.axes.autoscale(enable=True,axis='y', tight=True)
plt.pause(1) #timing
#uncommenting the next line will create nine tiny (6kb) image files
#plt.savefig(('image_%s.png' % i), bbox_inches=0, dpi=48)
在我的實際應用中,我試圖以這種方式生成10萬個微小圖像作為隨機數據的數據庫。 對於每x,有2到200 y的值。 然后我使用OpenCV將新圖像匹配到歷史數據庫中的最佳匹配。
關鍵是y值在每個圖像中從上到下拉伸,以便OpenCV找到一個很好的匹配。
如果它有助於我的x值始終是int()類型和等間距
ETA:我試圖在這里實施一些解決方案,但沒有取得任何進展:
但至少我學會了:
自動縮放始終使用全范圍的數據,因此y軸按y數據的完整范圍進行縮放,而不僅僅是x限制內的范圍。
仍然沒有解決方案在這里工作
def autoscale_y()
由@DanHickstein提供
給我:
h = np.max(y_displayed) - np.min(y_displayed)
ValueError: zero-size array to reduction operation maximum which has no identity
從這些鏈接中,我不確定在我的for循環中在哪里實現@Joe Kington的掩碼解決方案。
我現在正在使用這里提出的@bernie解決方案來獲得Y值:
也許那時我可以set_ylim()手動給出那個X的最小和最大Y值?
如果有一種方法可以在定義的xlim中作為標准的matplotlib方法進行自動縮放,那么這將更加容易
我昨晚解決了我的問題,創建了一個字典,其中x為鍵,y為y值。
這是在數據由函數y = x ** i創建時發生的
本質上我正在創建字典結構偽代碼:
data[x0] = [x0y1,x0y2,x0y3....]
data[x1] = [x1y1,x1y2,x1y3....]
data[x2] = [x2y1,x2y2,x2y3....]
etc.
我可以稍后在任何給定的x處引用所有y值。 從那里,找到切片左側和右側的最小和最大y值,以手動設置ylim。 如果您的xlim切片超過一個x段寬,則必須為xlim中的每個相應x切片重復該過程。 在我的例子中,我的x切片恰好是一個段寬。
import matplotlib.pyplot as plt
import numpy as np
# move my range function out of my data creation loop
x = np.arange(10,20,1)
# create a dictionary of my data with x values as keys
data = {}
for i in range(len(x)):
data[x[i]]=[]
# create some test data
for i in range(10):
y = x**i
plt.plot(x,y,c='red',marker='.',ms=2)
# store my y data to my data dictionary as its created
xx = x[-len(y):]
for j in range(len(xx)):
data[xx[j]].append(y[j])
# get all x values in chart and reduce to a sorted list set
xd = []
for n in range(len(plt.gca().get_lines())):
line = plt.gca().get_lines()[n]
xd.append((line.get_xdata()).tolist())
xd = [item for sublist in xd for item in sublist]
xd = sorted(list(set(xd)))
# attempt to plot slices of x with autoscaled y
ax = plt.gca()
for i in range(len(xd)-1):
ax.set_xlim([xd[i],xd[i+1]])
# reference my min and max y values by left and right borders of x slice
ymin = min(min(data[xd[i]]), min(data[xd[i+1]]))
ymax = max(max(data[xd[i]]), max(data[xd[i+1]]))
# manually set y limits
ax.set_ylim([ymin,ymax])
#eliminate my autoscale call
#ax.axes.autoscale(enable=True,axis='y', tight=True)
plt.pause(1) #timing
現在繪制時,y會自動縮放到x切片,而不是整個數據集。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.