Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make xticks evenly spaced despite their value?

Tags:

I am trying to generate a plot with x-axis being a geometric sequence while the y axis is a number between 0.0 and 1.0. My code looks like this:

form matplotlib import pyplot as plt
plt.xticks(X)
plt.plot(X,Y)
plt.show()

which generates a plot like this:

My plot

As you can see, I am explicitly setting the x-axis ticks to the ones belonging to the geometric sequence.

My question:Is it possible to make x-ticks evenly spaced despite their value, as the initial terms of the sequence are small, and crowded together. Kind of like logarithmic scale, which would be ideal if dealing with powers of a base, but not for a geometric sequence, I think, as is the case here.

like image 686
oczkoisse Avatar asked Sep 16 '16 22:09

oczkoisse


People also ask

How do you change the Xticks in a python plot?

To change the default x ticks, use set_xticks() function in Matplotlib.

How do you show all the Xticks?

Use xticks() method to show all the X-coordinates in the plot. Use yticks() method to show all the Y-coordinates in the plot. To display the figure, use show() method.

What is the use of Xticks () and Yticks () in plotting?

The xticks() and yticks() function takes a list object as argument. The elements in the list denote the positions on corresponding action where ticks will be displayed. This method will mark the data points at the given positions with ticks.

How do I get rid of Xticks in Python?

Matplotlib removes both labels and ticks by using xticks([]) and yticks([]) By using the method xticks() and yticks() you can disable the ticks and tick labels from both the x-axis and y-axis. In the above example, we use plt.


3 Answers

You can do it by plotting your variable as a function of the "natural" variable that parametrizes your curve. For example:

n = 12
a = np.arange(n)
x = 2**a
y = np.random.rand(n)

fig = plt.figure(1, figsize=(7,7))
ax1  = fig.add_subplot(211)
ax2  = fig.add_subplot(212)

ax1.plot(x,y)
ax1.xaxis.set_ticks(x)

ax2.plot(a, y) #we plot y as a function of a, which parametrizes x
ax2.xaxis.set_ticks(a) #set the ticks to be a
ax2.xaxis.set_ticklabels(x) # change the ticks' names to x

which produces:

enter image description here

like image 95
Alejandro Avatar answered Sep 18 '22 20:09

Alejandro


I had the same problem and spent several hours trying to find something appropriate. But it appears to be really easy and you do not need to make any parameterization or play with some x-ticks positions, etc.

The only thing you need to do is just to plot your x-values as str, not int: plot(x.astype('str'), y)

By modifying the code from the previous answer you will get:

n = 12
a = np.arange(n)
x = 2**a
y = np.random.rand(n)

fig = plt.figure(1, figsize=(7,7))
ax1  = fig.add_subplot(211)
ax2  = fig.add_subplot(212)

ax1.plot(x,y)
ax1.xaxis.set_ticks(x)

ax2.plot(x.astype('str'), y)

Result

like image 41
RedPers Avatar answered Sep 18 '22 20:09

RedPers


Seaborn has a bunch of categorical plot handling natively this kind of task.
Such as pointplot:

sns.pointplot(x="x", y="y", data=df, ax=ax)

Exemple

fig, [ax1, ax2] = plt.subplots(2, figsize=(7,7))

sns.lineplot(data=df, x="x", y="y", ax=ax1) #relational plot
sns.pointplot(data=df, x="x", y="y", ax=ax2) #categorical plot

output

like image 26
Antiez Avatar answered Sep 22 '22 20:09

Antiez