जबकि Joe Kington निश्चित रूप से सबसे समझदार जवाब का प्रस्ताव जब वह यह है कि केवल आवश्यक डेटा सिफारिश की गई है:
आप डेटा के एक उपसमूह प्रदर्शित करना चाहते हैं, तो यह शायद सबसे आसान ही नहीं सबसेट प्लॉट करने के लिए है प्लॉट किया जाना चाहिए, ऐसी स्थितियां हैं जहां सभी डेटा प्लॉट करना सबसे अच्छा होगा और बस एक निश्चित खंड पर ज़ूम करें। इसके अतिरिक्त, "autoscale_y" फ़ंक्शन होना अच्छा लगेगा, जिसके लिए केवल अक्ष ऑब्जेक्ट की आवश्यकता होती है (यानी, here के उत्तर के विपरीत, जिसके लिए डेटा का प्रत्यक्ष उपयोग की आवश्यकता होती है।)
यहां एक ऐसा फ़ंक्शन है जो वाई-
def autoscale_y(ax,margin=0.1):
"""This function rescales the y-axis based on the data that is visible given the current xlim of the axis.
ax -- a matplotlib axes object
margin -- the fraction of the total height of the y-data to pad the upper and lower ylims"""
import numpy as np
def get_bottom_top(line):
xd = line.get_xdata()
yd = line.get_ydata()
lo,hi = ax.get_xlim()
y_displayed = yd[((xd>lo) & (xd<hi))]
h = np.max(y_displayed) - np.min(y_displayed)
bot = np.min(y_displayed)-margin*h
top = np.max(y_displayed)+margin*h
return bot,top
lines = ax.get_lines()
bot,top = np.inf, -np.inf
for line in lines:
new_bot, new_top = get_bottom_top(line)
if new_bot < bot: bot = new_bot
if new_top > top: top = new_top
ax.set_ylim(bot,top)
यह एक हैक के बारे में कुछ है, और शायद कई स्थितियों में काम नहीं करेगा, लेकिन एक सरल साजिश के लिए, यह अच्छी तरह से काम करता है: अक्ष डेटा दृश्यमान एक्स-क्षेत्र में है कि के आधार पर।
यहाँ इस समारोह का उपयोग कर एक सरल उदाहरण है:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-100,100,1000)
y = x**2 + np.cos(x)*100
fig,axs = plt.subplots(1,2,figsize=(8,5))
for ax in axs:
ax.plot(x,y)
ax.plot(x,y*2)
ax.plot(x,y*10)
ax.set_xlim(-10,10)
autoscale_y(axs[1])
axs[0].set_title('Rescaled x-axis')
axs[1].set_title('Rescaled x-axis\nand used "autoscale_y"')
plt.show()
मैं भी इस विचार था लेकिन मुझे नहीं पता था कि सबसेट बनाने अजगर :) – Pygmalion