seaborn changing xticks from float to int

2024/9/20 0:28:42

I am plotting a graph with seaborn as sns and pylab as plt:

plt.figure(figsize=(10,10),)
sns.barplot(y = 'whatever_y', x = 'whatever_x' , data=mydata)
plt.xticks(fontsize=14, fontweight='bold')

The xticks are supposed to be 0, 1, 2, 3 but they are plotted like so: 0.0, 1.0, 2.0, 3.0

Does anyone know what I have to add to get them as integers? - (The data is a pandas dataframe) Thanks

Answer

You can do it with an axis formatter:

from  matplotlib.ticker import FuncFormatter

then, after your barplot line:

plt.gca().xaxis.set_major_formatter(FuncFormatter(lambda x, _: int(x)))
https://en.xdnf.cn/q/72546.html

Related Q&A

What are the use cases for a Python distribution?

Im developing a distribution for the Python package Im writing so I can post it on PyPI. Its my first time working with distutils, setuptools, distribute, pip, setup.py and all that and Im struggling a…

Recovering a file deleted with python

So, I deleted a file using python. I cant find it in my recycling bin. Is there a way I can undo it or something. Thanks in advance.EDIT: I used os.remove. I have tried Recuva, but it doesnt seem to fi…

Using Py_buffer and PyMemoryView_FromBuffer with different itemsizes

This question is related to a previous question I asked. Namely this one if anyone is interested. Basically, what I want to do is to expose a C array to Python using a Py_buffer wrapped in a memoryview…

selenium remotewebdriver with python - performance logging?

Im trying to get back some performance log info from a remote webdriver instance. Im using the Python Selenium bindings.From what I can see, this is information I should be able to get back. Think it m…

Python - replace unicode emojis with ASCII characters

I have an issue with one of my current weekend projects. I am writing a Python script that fetches some data from different sources and then spits everything out to an esc-pos printer. As you might ima…

How do I get my python object back from a QVariant in PyQt4?

I am creating a subclass of QAbstractItemModel to be displayed in an QTreeView.My index() and parent() function creates the QModelIndex using the QAbstractItemModel inherited function createIndex and p…

Django serializers vs rest_framework serializers

What is the difference between Django serializers vs rest_framework serializers? I making a webapp, where I want the API to be part of the primary app created by the project. Not creating a separate A…

Pandas replace non-zero values

I know I can replace all nan values with df.fillna(0) and replace a single value with df.replace(-,1), but how can I replace all non-zero values with a single value?

Pandas percentage change using group by

Suppose I have the following DataFrame: df = pd.DataFrame({city: [a, a, a, b, b, c, d, d, d], year: [2013, 2014, 2016, 2015, 2016, 2013, 2016, 2017, 2018],value: [10, 12, 16, 20, 21, 11, 15, 13, 16]})A…

Django cannot find my static files

I am relatively new to web dev. and I am trying to build my first web application. I have my static folder in project_root/static but for some reason, I keep getting 404s when I run the server:Not Foun…