0

enter image description here

height_str=headers[1:]   #height format str
height=[float(i) for i in height_str]  #height format float -type:LIST

plt.yticks((height),height_str)  #height  y axis

Hello. I would like to display the values and labels in a better range on the plot. I use imshow for the graph. height is a list that contains 156 elements

Steren
  • 127
  • 2
  • 10

1 Answers1

0

The way you supplied the ticks, you're telling matplotlib to place a tick at every height value you gave it. You need to give it only the specific ticks you want to use if you're doing things this way.

import numpy as np
import matplotlib.pyplot as plt
from scipy.misc import face

# Generating my face heights
height = np.arange(0, 800)
height_str = list(map(str, height))
plt.imshow(face())
#plt.yticks(height,height_str)  # <- this is bad. Do this instead
plt.yticks(height[::50],height_str[::50]) # Gets every 50th entry

Otherwise, you could do this:

fig, ax = plt.subplots()
ax.imshow(face())
ax.set_yticks(height[::50])

Edit: To format, you can use ticker functions.

from matplotlib import ticker
ax.yaxis.set_major_formatter(ticker.StrMethodFormatter("{x:.2f}"))

From https://matplotlib.org/stable/gallery/ticks_and_spines/tick-formatters.html

K.Cl
  • 1,615
  • 2
  • 9
  • 18
  • how can i round the values written in the axis? for example I get a value like 0.8048742 – Steren Apr 13 '21 at 14:24
  • 1
    See my edit, I added a snippet on how to use the `StrMethodFormatter`. – K.Cl Apr 13 '21 at 14:36
  • Thank u soooo much!! may I ask you if you could solve why the plot is cut at the top? Ideas? – Steren Apr 13 '21 at 14:40
  • Probably because the height you provided is larger than the height of your image. In my example, the image has around 800 pixels of height. If you extend the `arange` to `1200`, you'll get a blank area similar to yours. – K.Cl Apr 13 '21 at 14:43
  • No. If you're certain of your values, try this: `plt.yticks(height[:n:50],height_str[:n:50])` and replace n with a value to cut your labels at the appropriate place. – K.Cl Apr 13 '21 at 14:50
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/231069/discussion-between-steren-and-k-cl). – Steren Apr 13 '21 at 14:54