pythonmatplotlibintegeraxes

Matplotlib: Mix integer and float ticks


For my pyplot axes, I want to use the following ticks:

ticks = [-5.0, -4, -3, -2, -1, -0.128]

(These strange values are calculated and will change dynamically.)

But there are 2 important details I want to realise: The first and the last value should be a float number to see the exact values at the beginning and the end. The values between shall only be integers to keep it readable.

To set the ticks, I did

axes = plt.subplot()
axes.set_xticks(ticks)

And I get what I want, but it looks bad because my integer values are still printed as float values with 3 decimals.

Ticks

I want to get ticks -5.000, -4, -3, -2, -1, -0.128 and not -5.000, -4.000, ....

Any idea what I can do to solve this? Thank you! :)


Solution

  • Thanks to ImportanceOfBeingErnest, I was able to solve it quickly. Thank you very much!

    For other people with the same problem:

    ticks = [-5.0, -4, -3, -2, -1, -0.128]
    tickLabels = map(str, ticks)
    
    axes = plt.subplot()
    axes.set_xticks(ticks)
    axes.set_xticklabels(tickLabels)