Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove digits after decimal in axis ticks in matplotlib?

I have

tick_location = np.linspace(0.01, 10, num=10, endpoint=True)
ax2.set_xticks(tick_location)
tick_labels = np.around(1/new_tick_loc*1e4,decimals=-2)
ax2.set_xticklabels(tick_labels)

This (and some additional code) produces the following plot, but I'd like to remove the decimal point and the digit after it from the tick labels on the top x axis.

Using matplotlib.ticker.FormatStrFormatter(fmt) does not help

top x axis with decimal

like image 788
max29 Avatar asked Dec 10 '15 02:12

max29


People also ask

How do you remove decimal points on axis?

On the Format tab, in the Current Selection group, click Format Selection. Under Axis Options, Click Number, and then in the Category box, select the number format that you want. Tip If the number format you select uses decimal places, you can specify them in the Decimal places box.

How do I get rid of tick marks in Matplotlib?

To remove the ticks on the y-axis, tick_params() method has an attribute named left and we can set its value to False and pass it as a parameter inside the tick_params() function. It removes the tick on the y-axis.

How do you get rid of decimals in Python?

Type conversion in python helps to convert a decimal value number(floating number) to an integer. Thus converting float->int removes all decimals from a number.

How do I change the format of a tick in Matplotlib?

Tick formatters can be set in one of two ways, either by passing a str or function to set_major_formatter or set_minor_formatter , or by creating an instance of one of the various Formatter classes and providing that to set_major_formatter or set_minor_formatter .


1 Answers

Convert the numbers into ints:

ax2.set_xticklabels(tick_labels.astype(int))
like image 196
Mike Müller Avatar answered Oct 22 '22 20:10

Mike Müller