0

I am using matplotlib to live plot some data from a current sensor and it sometimes picks odd scaling factors that make it very difficult to interpret e.g.

Odd matplotlib scaling

It often does plot it without the scaling factor e.g.

No scaling factor

which is preferable as it is much easier to read the plot without a scaling factor.

I am using the following to scale the axes every loop step:

ax.set_xlim(min(x),max(x))
ax.set_ylim(min(y),max(y)) 

Is there a way to force matplotlib to avoid plotting the y-axis as it is doing in the first picture?

I don't know what the y-limits will be ahead of time and they could span a large range so I can't fix them to some constant value. Also, as I am plotting an updating plot within a tkinter GUI, the figure only autoscales on the initialisation of the plot.

Here is some minimal code to show how I am plotting within tkinter:

### initialise graph ###
self.fig, self.ax = plt.subplots(
    1,1, 
    figsize=(6.5,4), 
    dpi=150,
    tight_layout = True
    )
self.line, = self.ax.plot([],[]) #empty graph

self.GUIFig = FigureCanvasTkAgg(self.fig, root)

x = []
y = []
    
while True:

    x.append(time_delta)
    y.append(current_sensor_reading)
        
    self.line.set_xdata(x)
    self.line.set_ydata(y)
        
    self.ax.set_xlim(min(x),max(x))
    self.ax.set_ylim(min(y),max(y))
        
    self.GUIFig.draw()
Pepsi-Joe
  • 447
  • 1
  • 10
  • Are you sure you want powers of ten? What would you say you had expected the limits to be in the image you shared? 0.01 and 0.1? What if the maximum and minimum happen to be powers of 10, you'd still end up with data right on the edge of the chart? – Grismar Jan 06 '22 at 03:25
  • I would prefer the y axis to not have any scaling factor. I'm not sure why it is even doing one here as the limits are 8.285 and 9.954. I don't mind data on the edge of the chart. – Pepsi-Joe Jan 06 '22 at 03:28
  • It needs *some* scaling factor. I'm pretty sure you don't want it to run from minus infinity to positive infinity with all your data squished on a central axis - some choice for a range has to be made. The question is, what range did you expect to see? You should probably provide an example of data you're plotting (and how you're plotting it), so the question makes more sense - we have no way of telling what your data actually looks like and whether this is a reasonable plot. – Grismar Jan 06 '22 at 03:33
  • @Grismar I have added some more context to the question. – Pepsi-Joe Jan 06 '22 at 03:49
  • 1
    @BigBen Yes that does answer my question! Thanks! – Pepsi-Joe Jan 06 '22 at 03:50
  • 1
    (Other good reading [here](https://stackoverflow.com/questions/28371674/prevent-scientific-notation-in-matplotlib-pyplot)). – BigBen Jan 06 '22 at 03:52

0 Answers0