2

I have a lot of data that is correlated on the x axis but are all center around very different Y values. The data is also very long on the x so its hard to see details. I want to be able to set the x axis manually for the data set and then have the plot rescale the y axis itself based on the values of the data points that lie inside that manually set x axis.

Is this possible with matplotlib?

user3123955
  • 2,809
  • 6
  • 20
  • 21
  • if you use `ax.set_aspect('equal')` you get such behavior... – Saullo G. P. Castro Aug 27 '14 at 18:29
  • thanks for the response, could you expand on this a bit? do i need to get the axes first? How do you tell it that the x axis is the one to determine the y axis? – user3123955 Aug 27 '14 at 18:42
  • wont the set_aspect('equal') cause the y axis to be the same as the x axis? I dont want this behavior, the y axis has a much larger range than the x axis and on completely different values. I want to set the x axis limits and then have the y scale itself so i can easily see the data that lays within the x axis i set. Also i have no relation between the size of the x axis and the size of the y so i can't just set some aspect ratio – user3123955 Aug 27 '14 at 18:53
  • 1
    I am not aware of an existing option for this. You could hook into the matplotlib DrawEvent, check if the x-axis scale changed `ax.get_xlim()` and set the y-axis accordingly. – mdurant Aug 27 '14 at 19:06

1 Answers1

2

Based on the comment from mdurant I wrote some minimal example as a starting point for you:

import pylab as pl

pl.figure()
x = pl.rand(30)
x.sort()
y =  pl.sin(2 * pl.pi * x) + 0.1 * pl.randn(30)
pl.plot(x, y)

def ondraw(event):
    xlim = pl.gca().get_xlim()
    y_sel = y[pl.logical_and(x > xlim[0], x < xlim[1])]
    pl.gca().set_ylim((y_sel.min(), y_sel.max()))

pl.gcf().canvas.mpl_connect('draw_event', ondraw)

You might want to add more events to handle mouse movements etc.

Some comments:

  • I'm using pylab, since it provides useful functions for generating examle data. But you can import the matplotlib library as well.
  • The ondraw event handler gets the current x limits of the current axes gca(), selects y values with corresponding x coordinates within the x limits and sets new y limits determined by the minimum and maximum y value of selected coordinates.
  • With mpl_connect we attach the event handler to the current axes. Every time the axes is drawn - e.g. due to new plotting commands or manual user interaction - it calls ondraw to update the y limits.
Falko
  • 17,076
  • 13
  • 60
  • 105
  • Can you explain your code a little more? You seem to use pylab but i am using matplotlib. – user3123955 Aug 27 '14 at 21:38
  • @user3123955: Please have a look into the [matplotlib documentation on event handling](http://matplotlib.org/users/event_handling.html). PyLab is basically [a different interface](http://en.wikipedia.org/wiki/Matplotlib) to NumPy and matplotlib, often used by former Matlab users. You can use both. The commands are almost identical. – Falko Aug 28 '14 at 18:05