2
votes

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

4
What is mydata? is it a numpy array or something else? - EdChum
You can change the dtype of the column using df['x_col_name'] = df['x_col_name'].astype(int) - EdChum
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
Yes you can manipulate them I think you need to use a formatter on the ticker but I'm not a matplotlib expert - EdChum
Well, thanks anyway. I will leave the question out there - maybe someone knows a handy trick to do that in matplotlib - Annamarie

4 Answers

6
votes

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)))
0
votes

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.

0
votes

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()
-1
votes

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.