Navneeth Krishna
Navneeth Krishna

Reputation: 11

How to show axis ticks corresponding to plotted datapoints in a seaborn plot?

I am using seaborn to plot some values which are numerical. But the each of those numbers correspond to a textual value and I want those textual values to be displayed on the axes. Like if the numerical values progress as 0, 5, 10, ..., 30; each of those encoded numbers must be linked to a textual description. How can I do this?

Upvotes: 1

Views: 179

Answers (1)

zabop
zabop

Reputation: 7852

Main Point:

Use:

ax = plt.gca()
ax.set_xticks([<the x components of your datapoints>]);
ax.set_yticks([<the y components of your datapoints>]);

More elaborate version below.


You can go back to matplotlib and it will do it for you.

Let's say you want to plot [0, 7, 14 ... 35] against [0, 2, 4, ... 10]. The two arrays can be created by:

stepy=7
stepx=2
[stepy*y for y in range(6)]

(returning [0, 7, 14, 21, 28, 35]) and [stepx*x for x in range(6)] (returning [0, 2, 4, 6, 8, 10]).

Plot these with seaborn:

sns.scatterplot([stepx*x for x in range(6)],[stepy*y for y in range(6)]).

Give current axis to matplotlib by ax = plt.gca(), finish using set_xticks and set_yticks:

ax.set_xticks([stepx*x for x in range(6)]);
ax.set_yticks([stepy*y for y in range(6)]);

The whole code together:

stepy=7
stepx=2

sns.scatterplot([stepx*x for x in range(6)],[stepy*y for y in range(6)])
ax = plt.gca()
ax.set_xticks([stepx*x for x in range(6)]);
ax.set_yticks([stepy*y for y in range(6)]);

Yielding the plot:

enter image description here


I changed the example in the OP because with those numbers to plot, the plots already behave as desired.

Upvotes: 1

Related Questions