Change value shown by axes

1

I have an image in python that I graphic with the following code:

from PIL import Image
plt.figure()
im = Image.open('background.png')  
extent = Deg2meters([11.0,12.5],[54,54.75], ref).T
extent = np.delete(extent, np.s_[2], axis=1)
extentlist=[extent[0,0],extent[1,0],extent[0,1],extent[1,1]]
pylab.imshow(im, extent = extentlist, aspect=1.16)

The function Deg2meters only converts degrees (longitude and latitude) in meters from a certain reference (I need this, because later on I graph data that are in meters). This causes that in my axes the values appear in meters, but what I want is that they appreciate in degrees again. I can not change the values I plot to degrees, I need to plot in meters, but the axes show degrees, keeping the ratio clear.

The following image shows what I have on the left and what I want on the right:

I tried with plt.xticks without getting anything.

PS: I have a function that converts meters into degrees, that's not a problem.

    
asked by Juanca M 14.07.2017 в 09:22
source

1 answer

1

Since to reproduce your graph we need data that we do not have, I will give you an example that I think is similar to what you want and that you should be able to adapt without problems.

Imagine that we have certain data that represent temperature in degrees Celsius:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([10, 15, 20, 26, 30, 32, 35, 35.5, 35, 30, 28, 20, 12, 10])
plt.show()

Which shows us a simple graph where the y axis represents the degrees in Celsius scale:

Let's imagine that we want the scale of the axis and to be in degrees Fahrenheit but without modifying the original data (which are in Celsius scale) and with the possibility of adding new ones graphs whose data are in Celsius without first passing them to fahrenheit. In addition, we want the and axis to show only the marks corresponding to 53, 71 and 90 degrees fahrenheit.

To do this, we first use ax.set_yticks to specify the brands we want. We must remember that the graph is at all times in Celsius scale so that ax.set_yticks must pass the marks on that scale. For this we can help a function that passes from fahrenheits to celsius or do it manually.

Once this is done, we only need to format the axis so that it shows the labels in degrees Fahrenheit at all times, although the graph keeps the ratio and remains working in Celsius scale. For this we use matplotlib.ticker.FuncFormatter and a function that passes degrees celsius to fahrenheit.

import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter

def celsius_a_fahrenheit(c, *args):
    return c * (9 / 5) + 32

def fahrenheit_a_celsius(f):
    return (f - 32) / (9 / 5)

fig, ax = plt.subplots()
ax.plot([10, 15, 20, 26, 30, 32, 35, 35.5, 35, 30, 28, 20, 12, 10])

# Asignamos las marcas que queramos para el eje y (53, 71 y 90 grados fahrenheits)
yticks = [fahrenheit_a_celsius(s) for s in (53, 71, 90)]
ax.set_yticks(yticks)

# Formateamos el eje para que la escala mostrada sea grados fahrenheits.
formatter = FuncFormatter(celsius_a_fahrenheit)
plt.gca().yaxis.set_major_formatter(formatter)

# Podemos añadir nuevos datos en escala celsius sin convertirlos a fahrenheits.
ax.plot([2, 13, 21, 28, 17, 12 ,0])

plt.show()

With what we get:

Notice how after formatting the axis we created a new graph with data in Celsius. Despite showing the scale of the axis and in fahrenheit, the scale it uses to graph is celsius so you do not have to convert the new data to faherenheit.

As can be seen below on the left when the pointer passes through the graph automatically shows the values in fahrenheit scale.

If we want to do something similar with the axis x the process is the same.

  

Warning : code tested and valid for Python 2.7.13 and 3.6.1 with Matplotlib 2.0.2.

    
answered by 14.07.2017 / 13:59
source