Repository: pyqtgraph/pyqtgraph

Issue:
Axis values
After zooming in/out a lot, numbers of axis values are so big that axis is moved. Calling auto scale after that do not restart axis position
In attachment there are  images that show use cases.

![moved axis](https://cloud.githubusercontent.com/assets/14001200/23157423/6ebb02d2-f81b-11e6-9217-b08b2af12dee.PNG)

![after autoscaling](https://cloud.githubusercontent.com/assets/14001200/23157463/a96b61c4-f81b-11e6-8379-0dbb32febae8.PNG)







Discussion:
PR #403 proposes a fix to solve that problem.

Vincent

Resolving patch:
diff --git a/pyqtgraph/graphicsItems/AxisItem.py b/pyqtgraph/graphicsItems/AxisItem.py
--- a/pyqtgraph/graphicsItems/AxisItem.py
+++ b/pyqtgraph/graphicsItems/AxisItem.py
@@ -56,6 +56,7 @@ def __init__(self, orientation, pen=None, textPen=None, linkView=None, parent=No
             'tickTextWidth': 30,  ## space reserved for tick text
             'tickTextHeight': 18,
             'autoExpandTextSpace': True,  ## automatically expand text space if needed
+            'autoReduceTextSpace': True,
             'tickFont': None,
             'stopAxisAtTick': (False, False),  ## whether axis is drawn to edge of box or to last tick
             'textFillLimits': [  ## how much of the axis to fill up with tick text, maximally.
@@ -125,6 +126,7 @@ def setStyle(self, **kwds):
         tickTextHeight      (int) Vertical space reserved for tick text in px
         autoExpandTextSpace (bool) Automatically expand text space if the tick
                             strings become too long.
+        autoReduceTextSpace (bool) Automatically shrink the axis if necessary 
         tickFont            (QFont or None) Determines the font used for tick
                             values. Use None for the default font.
         stopAxisAtTick      (tuple: (bool min, bool max)) If True, the axis
@@ -314,19 +316,26 @@ def _updateMaxTextSize(self, x):
         ## changed; we use this to decide whether the item needs to be resized
         ## to accomodate.
         if self.orientation in ['left', 'right']:
-            mx = max(self.textWidth, x)
-            if mx > self.textWidth or mx < self.textWidth-10:
-                self.textWidth = mx
-                if self.style['autoExpandTextSpace'] is True:
-                    self._updateWidth()
-                    #return True  ## size has changed
+            if self.style["autoReduceTextSpace"]:
+                if x > self.textWidth or x < self.textWidth - 10:
+                    self.textWidth = x
+            else:
+                mx = max(self.textWidth, x)
+                if mx > self.textWidth or mx < self.textWidth - 10:
+                    self.textWidth = mx
+            if self.style['autoExpandTextSpace']:
+                self._updateWidth()
+        
         else:
-            mx = max(self.textHeight, x)
-            if mx > self.textHeight or mx < self.textHeight-10:
-                self.textHeight = mx
-                if self.style['autoExpandTextSpace'] is True:
-                    self._updateHeight()
-                    #return True  ## size has changed
+            if self.style['autoReduceTextSpace']:
+                if x > self.textHeight or x < self.textHeight - 10:
+                    self.textHeight = x
+            else:
+                mx = max(self.textHeight, x)
+                if mx > self.textHeight or mx < self.textHeight - 10:
+                    self.textHeight = mx
+            if self.style['autoExpandTextSpace']:
+                self._updateHeight()
 
     def _adjustSize(self):
         if self.orientation in ['left', 'right']:
@@ -350,7 +359,7 @@ def _updateHeight(self):
             if self.fixedHeight is None:
                 if not self.style['showValues']:
                     h = 0
-                elif self.style['autoExpandTextSpace'] is True:
+                elif self.style['autoExpandTextSpace']:
                     h = self.textHeight
                 else:
                     h = self.style['tickTextHeight']
@@ -381,7 +390,7 @@ def _updateWidth(self):
             if self.fixedWidth is None:
                 if not self.style['showValues']:
                     w = 0
-                elif self.style['autoExpandTextSpace'] is True:
+                elif self.style['autoExpandTextSpace']:
                     w = self.textWidth
                 else:
                     w = self.style['tickTextWidth']
@@ -1001,6 +1010,7 @@ def generateDrawSpecs(self, p):
             #textHeight = self.style['tickTextHeight'] ## space allocated for horizontal text
 
         textSize2 = 0
+        lastTextSize2 = 0
         textRects = []
         textSpecs = []  ## list of draw
 
@@ -1062,6 +1072,8 @@ def generateDrawSpecs(self, p):
                         break
                 if finished:
                     break
+            
+            lastTextSize2 = textSize2
 
             #spacing, values = tickLevels[best]
             #strings = self.tickStrings(values, self.scale, spacing)
@@ -1097,7 +1109,7 @@ def generateDrawSpecs(self, p):
         profiler('compute text')
 
         ## update max text size if needed.
-        self._updateMaxTextSize(textSize2)
+        self._updateMaxTextSize(lastTextSize2)
 
         return (axisSpec, tickSpecs, textSpecs)
 


Test patch:
diff --git a/pyqtgraph/graphicsItems/tests/test_ErrorBarItem.py b/pyqtgraph/graphicsItems/tests/test_ErrorBarItem.py
--- a/pyqtgraph/graphicsItems/tests/test_ErrorBarItem.py
+++ b/pyqtgraph/graphicsItems/tests/test_ErrorBarItem.py
@@ -21,19 +21,19 @@ def test_ErrorBarItem_defer_data():
     app.processEvents()
     r_empty_ebi = plot.viewRect()
 
-    assert r_no_ebi == r_empty_ebi
+    assert r_no_ebi.height() == r_empty_ebi.height()
 
     err.setData(x=x, y=x, bottom=x, top=x)
     app.processEvents()
     r_ebi = plot.viewRect()
 
-    assert r_empty_ebi != r_ebi
+    assert r_ebi.height() > r_empty_ebi.height()
 
     # unset data, ErrorBarItem disappears and view rect goes back to original
     err.setData(x=None, y=None)
     app.processEvents()
     r_clear_ebi = plot.viewRect()
 
-    assert r_clear_ebi == r_no_ebi
+    assert r_clear_ebi.height() == r_empty_ebi.height()
 
     plot.close()