Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert (or scale) axis values and redefine the tick frequency in matplotlib?

Tags:

I am displaying a jpg image (I rotate this by 90 degrees, if this is relevant) and of course the axes display the pixel coordinates. I would like to convert the axis so that instead of displaying the pixel number, it will display my unit of choice - be it radians, degrees, or in my case an astronomical coordinate. I know the conversion from pixel to (eg) degree. Here is a snippet of what my code looks like currently:

import matplotlib.pyplot as plt  import Image import matplotlib thumb = Image.open(self.image) thumb = thumb.rotate(90) dpi = plt.rcParams['figure.dpi'] figsize = thumb.size[0]/dpi, thumb.size[1]/dpi fig = plt.figure(figsize=figsize) plt.imshow(thumb, origin='lower',aspect='equal') plt.show() 

...so following on from this, can I take each value that matplotlib would print on the axis, and change/replace it with a string to output instead? I would want to do this for a specific coordinate format - eg, rather than an angle of 10.44 (degrees), I would like it to read 10 26' 24'' (ie, degrees, arcmins, arcsecs)

Finally on this theme, I'd want control over the tick frequency, on the plot. Matplotlib might print the axis value every 50 pixels, but I'd really want it every (for example) degree.

It sounds like I would like to define some kind of array with the pixel values and their converted values (degrees etc) that I want to be displayed, having control over the sampling frequency over the range xmin/xmax range.

Are there any matplotlib experts on Stack Overflow? If so, thanks very much in advance for your help! To make this a more learning experience, I'd really appreciate being prodded in the direction of tutorials etc on this kind of matplotlib problem. I've found myself getting very confused with axes, axis, figures, artists etc!

Cheers,

Dave

like image 850
Dave Avatar asked Jul 17 '09 15:07

Dave


People also ask

How do I change the number of ticks in MatPlotLib?

Locator_params() function that lets us change the tightness and number of ticks in the plots. This is made for customizing the subplots in matplotlib, where we need the ticks packed a little tighter and limited. So, we can use this function to control the number of ticks on the plots.

How do I change the axis scale in MatPlotLib?

MatPlotLib with Python To change the range of X and Y axes, we can use xlim() and ylim() methods.

How do I change the Y axis interval in MatPlotLib?

xlim() and matplotlib. pyplot. ylim() from the Matplotlib is a library in Python. This function in pyplot module of matplotlib library is used to get or set the x-limits/ y-limits of the current axes and returns the tuple of the new x-axis/y-axis limits.

How do I increase Yticks in MatPlotLib?

Setting Figure-Level Tick Frequency in Matplotlib You can use the xticks() and yticks() functions and pass in an array denoting the actual ticks. On the X-axis, this array starts on 0 and ends at the length of the x array. On the Y-axis, it starts at 0 and ends at the max value of y .


1 Answers

It looks like you're dealing with the matplotlib.pyplot interface, which means that you'll be able to bypass most of the dealing with artists, axes, and the like. You can control the values and labels of the tick marks by using the matplotlib.pyplot.xticks command, as follows:

tick_locs = [list of locations where you want your tick marks placed] tick_lbls = [list of corresponding labels for each of the tick marks] plt.xticks(tick_locs, tick_lbls) 

For your particular example, you'll have to compute what the tick marks are relative to the units (i.e. pixels) of your original plot (since you're using imshow) - you said you know how to do this, though.

I haven't dealt with images much, but you may be able to use a different plotting method (e.g. pcolor) that allows you to supply x and y information. That may give you a few more options for specifying the units of your image.

For tutorials, you would do well to look through the matplotlib gallery - find something you like, and read the code that produced it. One of the guys in our office recently bought a book on Python visualization - that may be worthwhile looking at.

The way that I generally think of all the various pieces is as follows:

  • A Figure is a container for all the Axes
  • An Axes is the space where what you draw (i.e. your plot) actually shows up
  • An Axis is the actual x and y axes
  • Artists? That's too deep in the interface for me: I've never had to worry about those yet, even though I rarely use the pyplot module in production plots.
like image 80
Tim Whitcomb Avatar answered Oct 21 '22 00:10

Tim Whitcomb