5

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

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Annamarie
  • 183
  • 1
  • 2
  • 13
  • What is `mydata`? is it a numpy array or something else? – EdChum May 19 '15 at 13:33
  • 3
    You can change the dtype of the column using `df['x_col_name'] = df['x_col_name'].astype(int)` – EdChum May 19 '15 at 13:36
  • Thanks that works - so this question remains unanswered or is there a way to manipulate the xticks from float to int in seaborn/pylab? – Annamarie May 19 '15 at 13:51
  • Yes you can manipulate them I think you need to use a formatter on the [`ticker`](http://matplotlib.org/api/ticker_api.html) but I'm not a matplotlib expert – EdChum May 19 '15 at 14:04
  • Well, thanks anyway. I will leave the question out there - maybe someone knows a handy trick to do that in matplotlib – Annamarie May 19 '15 at 14:10

5 Answers5

7

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)))
Glenn
  • 7,262
  • 1
  • 17
  • 23
1

The guideline shows how to manipulate the facetgrid: https://seaborn.pydata.org/tutorial/axis_grids.html

with sns.axes_style("white"):
     g = sns.FacetGrid(tips, row="sex", col="smoker", margin_titles=True, height=2.5)
g.map(sns.scatterplot, "total_bill", "tip", color="#334488")
g.set_axis_labels("Total bill (US Dollars)", "Tip")
g.set(xticks=[10, 30, 50], yticks=[2, 6, 10])
g.fig.subplots_adjust(wspace=.02, hspace=.02)

The row "g.set" takes the xticks argument. As an example, i had a pandas dataframe column with integers 1 to 18 shown as 1.0 etc. I solved it like so:

g.set(xticks=list(range(1,19)))

Hope this is useful.

Lemon
  • 11
  • 1
0

if you are using seaborn (also works in jupyter notebook), you can also do this.

from  matplotlib.ticker import FuncFormatter
ax = sns.barplot(x='x', y='y',hue='', data=data_set_pd)
ax.xaxis.set_major_formatter(FuncFormatter(lambda x, _: int(x)))
plt.show()
MD Mushfirat Mohaimin
  • 1,966
  • 3
  • 10
  • 22
Zachary
  • 179
  • 1
  • 7
0

it is easy, use MaxNLocator as follows:

import seaborn as sns
from seaborn import displot 
import matplotlib.pyplot as plt


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

y=[1, 0, 0, 1, 1, 1]
face_grid = sns.displot(y, color="aquamarine")
fig = face_grid.figure
ax = fig.gca()
ax.xaxis.set_major_locator(MaxNLocator(integer=True))
ax.yaxis.set_major_locator(MaxNLocator(integer=True))
ouba64
  • 156
  • 4
-1

Here is an intuitive way to fix the ticks with matplotlib:

import matplotlib.pyplot as plt
plt.scatter(x,y)
plt.xticks(np.arange(min(x), max(x)+1, 1))
plt.yticks(np.arange(min(y), max(y)+1, 1))

The code goes to the same cell (in case of jupyter notebook) right after the seaborn figure. It works since seaborn uses matplotlib backend.

Sokolokki
  • 833
  • 1
  • 9
  • 19
  • the question is about seaborn and consider providing explanation to your answer – Algorithman Oct 18 '20 at 05:13
  • 1
    Well, maybe it isn't "la crème de la crème" but Artur's stuff does the trick : x= df["myvar1"] plt_plot = sns.relplot(x=x, y="myvar2", kind="line", data=df, ci=None,aspect=2) plt.xticks(np.arange(min(x), max(x)+1, 1)) – Nando May 24 '21 at 08:52